JAVA-Swing- Code

  1 public class MainFrame {
  2 
  3     private JPanel mainPanel;
  4 
  5     private JLabel passWordLable;
  6 
  7     private JPasswordField passwordField;
  8 
  9     private JLabel keyFileLable;
 10 
 11     private JTextField keyFileField;
 12 
 13     private JButton fileChooserButton;
 14 
 15     private JLabel resultLabel;
 16 
 17     private JTextArea resultTextField;
 18 
 19     private JButton genrateKeysButton;
 20 
 21     private JButton encryptButton;
 22 
 23     public static void main(String args[]) {
 24         new MainFrame();
 25 
 26     }
 27 
 28     public MainFrame() {
 29         initView();
 30         initLinsteners();
 31     }
 32 
 33     private void initView() {
 34         Font lablefont = new Font(Font.SANS_SERIF, Font.PLAIN, 18);
 35         Font buttonFont = new Font("黑体", Font.PLAIN, 15);
 36         JFrame jf = new JFrame("Date Analysis Encryption Tool"); // 实例化一个JFrame对象
 37         jf.setVisible(true); // 设置窗体可视
 38         jf.setSize(600, 300); // 设置窗体大小
 39         jf.setFont(buttonFont);
 40         jf.setLocationRelativeTo(null);
 41         jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 42         jf.setResizable(false);
 43 
 44         mainPanel = new JPanel();
 45 
 46         passWordLable = new JLabel("Password:");
 47         passWordLable.setFont(lablefont);
 48         passWordLable.setPreferredSize(new Dimension(120, 40));
 49 
 50         mainPanel.add(passWordLable);
 51 
 52         passwordField = new JPasswordField();
 53         passwordField.setFont(lablefont);
 54         passwordField.setPreferredSize(new Dimension(400, 40));
 55         mainPanel.add(passwordField);
 56 
 57         keyFileLable = new JLabel("Key File:");
 58         keyFileLable.setFont(lablefont);
 59         keyFileLable.setPreferredSize(new Dimension(120, 40));
 60         mainPanel.add(keyFileLable);
 61 
 62         keyFileField = new JTextField();
 63         keyFileField.setFont(lablefont);
 64         keyFileField.setEditable(false);
 65         keyFileField.setPreferredSize(new Dimension(295, 40));
 66         mainPanel.add(keyFileField);
 67 
 68         fileChooserButton = new JButton("Choose key...");
 69         fileChooserButton.setFont(buttonFont);
 70         fileChooserButton.setPreferredSize(new Dimension(100, 40));
 71         fileChooserButton.setContentAreaFilled(false);
 72         fileChooserButton.setMargin(new Insets(0, 0, 0, 0));
 73         fileChooserButton.setBorder(BorderFactory.createEtchedBorder());
 74 
 75         mainPanel.add(fileChooserButton);
 76 
 77         resultLabel = new JLabel("Result:");
 78         resultLabel.setFont(lablefont);
 79         resultLabel.setPreferredSize(new Dimension(120, 40));
 80 
 81         mainPanel.add(resultLabel);
 82 
 83         resultTextField = new JTextArea(50, 100);
 84         resultTextField.setLineWrap(true);
 85         JScrollPane scrollPane = new JScrollPane(resultTextField);
 86         scrollPane.setPreferredSize(new Dimension(400, 100));
 87         mainPanel.add(scrollPane);
 88 
 89         JPanel bottomButtonPanel = new JPanel();
 90         bottomButtonPanel.setPreferredSize(new Dimension(535, 60));
 91         bottomButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
 92 
 93         genrateKeysButton = new JButton("Generate Keys");
 94         genrateKeysButton.setFont(buttonFont);
 95         genrateKeysButton.setPreferredSize(new Dimension(150, 40));
 96         genrateKeysButton.setContentAreaFilled(false);
 97         genrateKeysButton.setMargin(new Insets(0, 0, 0, 0));
 98         genrateKeysButton.setBorder(BorderFactory.createEtchedBorder());
 99 
100         bottomButtonPanel.add(genrateKeysButton);
101 
102         encryptButton = new JButton("Encrypt");
103         encryptButton.setFont(buttonFont);
104         encryptButton.setPreferredSize(new Dimension(150, 40));
105         encryptButton.setContentAreaFilled(true);
106         encryptButton.setMargin(new Insets(0, 0, 0, 0));
107         encryptButton.setBorder(BorderFactory.createEtchedBorder());
108 
109         bottomButtonPanel.add(encryptButton);
110         mainPanel.add(bottomButtonPanel);
111 
112         jf.setContentPane(mainPanel);
113         jf.setVisible(true);
114     }
115 
116     private void initLinsteners() {
117         fileChooserButton.addActionListener(new FileChooserListener());
118         genrateKeysButton.addActionListener(new GenerateKeysListener());
119         encryptButton.addActionListener(new EncryptListener());
120     }
121 
122     private class FileChooserListener implements ActionListener {
123         @Override
124         public void actionPerformed(ActionEvent e) {
125             JFileChooser jfc = new JFileChooser();
126             jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
127             jfc.setFileFilter(new FileNameExtensionFilter("Secerty Key Files", "key"));
128             jfc.showDialog(new JLabel(), "选择");
129             File file = jfc.getSelectedFile();
130             if (file.isFile()) {
131                 keyFileField.setText(jfc.getSelectedFile().getAbsolutePath());
132             }
133         }
134     }
135 
136     private class GenerateKeysListener implements ActionListener {
137         @Override
138         public void actionPerformed(ActionEvent e) {
139 
140         }
141     }
142 
143     private class EncryptListener implements ActionListener {
144         @Override
145         public void actionPerformed(ActionEvent e) {
146             String password = new String(passwordField.getPassword());
147             if (password == null || password.trim().equals("")) {
148                 JOptionPane.showMessageDialog(null, "Please input your password", "Error", JOptionPane.ERROR_MESSAGE);
149                 return;
150             }
151             System.out.println("Password======>" + password);
152             String keyFilePath = keyFileField.getText();
153             if (keyFilePath == null || keyFilePath.trim().equals("")) {
154                 JOptionPane.showMessageDialog(null, "Please select the key file", "Error", JOptionPane.ERROR_MESSAGE);
155                 return;
156             }
157             System.out.println("keyFilePath======>" + keyFilePath);
158         }
159     }
160 }

 

上一篇:Java程序员注意——审查Java代码的六种常见错误


下一篇:疯狂java讲义第四章补充习题第6题答案