Tiếp theo loạt bài về Lập trình JAVA Desktop từ A tới Z - phần 4. Xem thêm các phần trước ở bên dưới.
Nội dung file mã hóa Frame
EncryptFrame.java
Khai báo và nhập các gói cần thiết.
Nội dung file GridBagLayoutEx.java
Nội dung file mã hóa Frame
EncryptFrame.java
Khai báo và nhập các gói cần thiết.
package com.pissoft.session1; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField;Khai báo các thuộc tính và hàm thành viên trong lớp EncryptFrame
public class EncryptFrame extends JFrame{ private static final long serialVersionUID = -4229685511765327513L; private JLabel lblSource = new JLabel("Source: ", JLabel.RIGHT); private JTextField txtSource = new JTextField(); private JLabel lblAlgorithm = new JLabel("Algorithm: ", JLabel.RIGHT); private JComboBox cbAlgorithm = new JComboBox(new String[]{"SHA-1","MD2", "MD5"}); private JLabel lblDest = new JLabel("Dest: ", JLabel.RIGHT); private JTextField txtDest = new JTextField(); private JButton btnEncrypt = new JButton("Encrypt"); private JButton btnCheck = new JButton("Check"); public EncryptFrame(){ super("Encrypt Demo"); initFrame(); buidGUI(); registerListener(); setVisible(true); } private void initFrame(){ //Put frame at the center of the Desktop int frameX = (Toolkit.getDefaultToolkit().getScreenSize().width- getWidth()) / 2; int frameY = (Toolkit.getDefaultToolkit().getScreenSize().height- getHeight()) / 2; this.setLocation(frameX, frameY); //Require app end when frame closed this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void buidGUI(){ GridBagLayoutEx layout = new GridBagLayoutEx(); setLayout(layout); add(lblSource, layout.createFixedConstraints()); add(txtSource, layout.createHorzGlueConstraints()); layout.newRow(); add(lblAlgorithm, layout.createFixedConstraints()); add(cbAlgorithm, layout.createHorzGlueConstraints()); layout.newRow(); add(lblDest, layout.createFixedConstraints()); add(txtDest, layout.createHorzGlueConstraints()); layout.newRow(); add(Box.createVerticalGlue(), layout.createMergeAllGlueConstraints()); layout.newRow(); JPanel pnlController = new JPanel(); add(pnlController, layout.createMergeAllHorzGlueConstraints()); pnlController.setLayout(new BoxLayout(pnlController,BoxLayout.X_AXIS)); pnlController.add(Box.createHorizontalGlue()); pnlController.add(btnEncrypt); pnlController.add(Box.createHorizontalStrut(4)); pnlController.add(btnCheck); pack(); } private void registerListener(){ btnEncrypt.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String dest = encrypt(txtSource.getText(),(String)cbAlgorithm.getSelectedItem()); txtDest.setText(dest); } }); btnCheck.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { Boolean isSuccessed = check(txtDest.getText(),txtSource.getText(), (String)cbAlgorithm.getSelectedItem()); String message[]={"Fail", "Successful"}; JOptionPane.showMessageDialog(EncryptFrame.this,message[isSuccessed?1:0]); } }); } public String encrypt(String source, String algorithm){ MessageDigest digester=null; try { digester = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } if(digester==null) return null; byte dest[] = digester.digest(source.getBytes()); return new String(dest); } public boolean check(String secret, String source, String algorithm){ return secret.equals(encrypt(source, algorithm)); } }
Nội dung file GridBagLayoutEx.java
package com.pissoft.session1; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; public class GridBagLayoutEx extends GridBagLayout { /** */ private static final long serialVersionUID = 556538646538435067L; private int row=0; private int column=0; public void newRow(){ row++; column=0; } public void newColumn(){ column++; } /** * Create fixed both side constraints * @param pLeftBoundary * @return */ public GridBagConstraints createFixedConstraints(boolean pLeftBoundary) { return createFixedConstraints(pLeftBoundary, 1); } /** * Create fixed both side constraints * @param pLeftBoundary * @param pGridWidth * @return */ public GridBagConstraints createFixedConstraints(boolean pLeftBoundary, int pGridWidth) { return createFixedConstraints(pLeftBoundary, pGridWidth, 1); } /** * Create fixed both side constraints * @param pLeftBoundary boolean * @param pGgridWidth int * @param pGridHeight int * @return GridBagConstraints */ public GridBagConstraints createFixedConstraints(boolean pLeftBoundary, int pGridWidth, int pGridHeight) {GridBagConstraints lGridBagConstraints = new GridBagConstraints(row,column, pGridWidth, pGridHeight, 0, 0, GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL, pLeftBoundary==true? new Insets(1,0, 1, 2):new Insets(1, 12, 1, 2), 0, 0); column+=pGridWidth; return lGridBagConstraints; } /** * Create fixed both side constraints * @return GridBagConstraints */ public GridBagConstraints createFixedConstraints() { return createFixedConstraints( 1); } /** * Create fixed both side constraints * @param pGridWidth int * @return GridBagConstraints */ public GridBagConstraints createFixedConstraints(int pGridWidth) { return createFixedConstraints(pGridWidth, 1); } /* * Create fixed both side constraints * @param pGridWidth int * @param pGridHeight int * @return GridBagConstraints */ public GridBagConstraints createFixedConstraints(int pGridWidth,int pGridHeight) { GridBagConstraints lGridBagConstraints = newGridBagConstraints(column, row, pGridWidth, pGridHeight, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 12, 1, 2), 0, 0); column+=pGridWidth; return lGridBagConstraints; } /** * Create horizontal glue constraints * @return GridBagConstraints */ public GridBagConstraints createHorzGlueConstraints() { return createHorzGlueConstraints(1); } /** * Create horizontal glue constraints * @param pGridWidth int * @return GridBagConstraints */ public GridBagConstraints createHorzGlueConstraints(int pGridWidth) { return createHorzGlueConstraints(pGridWidth, 1); } /** * Create horizontal glue constraints * @param pGridWidth int * @param pGridHeight int * @return GridBagConstraints */ public GridBagConstraints createHorzGlueConstraints(int pGridWidth, int pGridHeight) { return createHorzGlueConstraints(pGridWidth, pGridHeight, 1.0); } /** * Create horizontal glue constraints * @param pGridWidth int * @param pGridHeight int * @param pWeightX double * @return GridBagConstraints */ public GridBagConstraints createHorzGlueConstraints(int pGridWidth, int pGridHeight, double pWeightX) { GridBagConstraints lGridBagConstraints = newGridBagConstraints(column, row, pGridWidth, pGridHeight, pWeightX,0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,new Insets(1, 0, 1, 0), 0, 0); column+=pGridWidth; return lGridBagConstraints; }
No comments:
Post a Comment