首页 > 代码库 > Code Review

Code Review

  1 package niit.project4.view;
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 import javax.swing.*;
  5 import javax.swing.text.JTextComponent;
  6 
  7 public class Jsa extends JFrame {
  8     private JTextField username;
  9     private JTextField email;
 10     private JPasswordField password;
 11     private JLabel man;
 12 
 13     JLabel label = new JLabel("用户注册");
 14     CheckboxGroup sex = new CheckboxGroup();
 15 
 16     /**
 17      * Launch the application.
 18      */
 19     public static void main(String[] args) {
 20         Jsa frame = new Jsa();
 21         frame.setVisible(true);
 22     }
 23 
 24     /**
 25      * Create the frame.
 26      */
 27     public Jsa() {
 28         setTitle("用户注册");
 29         setBounds(100, 100, 414, 336);
 30         getContentPane().setLayout(null);
 31 
 32         label.setBounds(177, 10, 54, 15);
 33         getContentPane().add(label);
 34 
 35         JLabel label_1 = new JLabel("用户名");
 36         label_1.setHorizontalAlignment(SwingConstants.CENTER);
 37         label_1.setBounds(74, 43, 54, 15);
 38         getContentPane().add(label_1);
 39 
 40         JLabel label_2 = new JLabel("性  别");
 41         label_2.setHorizontalAlignment(SwingConstants.CENTER);
 42         label_2.setBounds(74, 78, 54, 15);
 43         getContentPane().add(label_2);
 44 
 45         JLabel label_3 = new JLabel("邮  箱");
 46         label_3.setHorizontalAlignment(SwingConstants.CENTER);
 47         label_3.setBounds(74, 115, 54, 15);
 48         getContentPane().add(label_3);
 49 
 50         JLabel label_4 = new JLabel("密 码");
 51         label_4.setHorizontalAlignment(SwingConstants.CENTER);
 52         label_4.setBounds(74, 151, 54, 15);
 53         getContentPane().add(label_4);
 54 
 55         JLabel label_5 = new JLabel("爱  好");
 56         label_5.setHorizontalAlignment(SwingConstants.CENTER);
 57         label_5.setBounds(74, 182, 54, 15);
 58         getContentPane().add(label_5);
 59 
 60         Checkbox man = new Checkbox("男", true, sex);
 61         man.setBounds(146, 74, 54, 23);
 62         getContentPane().add(man);
 63 
 64         Checkbox woman = new Checkbox("女", false, sex);
 65         woman.setBounds(235, 74, 71, 23);
 66         getContentPane().add(woman);
 67 
 68         username = new JTextField();
 69         username.setBounds(145, 40, 161, 21);
 70         getContentPane().add(username);
 71         username.setColumns(10);
 72 
 73         email = new JTextField();
 74         email.setBounds(145, 112, 161, 21);
 75         getContentPane().add(email);
 76         email.setColumns(10);
 77 
 78         password = new JPasswordField();
 79         password.setBounds(145, 148, 161, 21);
 80         getContentPane().add(password);
 81 
 82         JLabel label_6 = new JLabel("4-6\u4F4D");
 83         label_6.setBounds(316, 151, 54, 15);
 84         getContentPane().add(label_6);
 85 
 86         JComboBox comboBox = new JComboBox();
 87         comboBox.setModel(new DefaultComboBoxModel(new String[] { "运动", "音乐" }));
 88         comboBox.setToolTipText("");
 89         comboBox.setBounds(145, 179, 93, 21);
 90         getContentPane().add(comboBox);
 99 
100         JButton button = new JButton("清空");
101         button.addActionListener(new ActionListener() {
102             public void actionPerformed(ActionEvent arg0) {
103                 username.setText("");
104                 email.setText("");
105                 password.setText("");
106             }
107         });
108         button.setBounds(100, 251, 93, 23);
109         getContentPane().add(button);
110 
111         JButton button_1 = new JButton("提交");
112         button_1.addActionListener(new ActionListener() {
113             public void actionPerformed(ActionEvent arg0) {
114                 String name1 = username.getText();
115                 String password1 = new String(password.getPassword());
116                 String mail = email.getText();
117                 String role1 = (String) comboBox.getSelectedItem();
118                 String xb = null;
119                 if (man.getState() == true) {
120                     xb = "男";
121                 } else {
122                     xb = "女";
123                 }
124 
125                 if (name1 == null || name1.length() <= 0 || password1 == null || password1.length() <= 0
126                         || role1 == null || role1.length() <= 0 || mail == null || mail.length() <= 0) {
127                     JOptionPane.showMessageDialog(button_1, "请输入完整登入信息!");
128                 } else if (mail != null && mail.length() > 0 && mail.indexOf(‘@‘) == -1
129                         && mail.indexOf(‘.‘) <= mail.indexOf(‘@‘)) {
130                     JOptionPane.showMessageDialog(button_1, "电子邮件输入错误!");
131                 } else if (password1.length() < 4 || password1.length() > 6) {
132                     JOptionPane.showMessageDialog(button_1, "密码不合规范!");
133                 } else {
134                     int n = JOptionPane.showConfirmDialog(null,
135                             "用户名:" + name1 + "\n" + "性别:" + xb + "\n" + "电子邮件:" + mail + "\n" + "爱好:" + role1, "选择一个选项",
136                             JOptionPane.YES_NO_CANCEL_OPTION);
137 
138                     switch (n) {
139                     case 0:
140                         JOptionPane.showMessageDialog(button_1,
141                                 "用户名:" + name1 + "\n" + "性别:" + xb + "\n" + "电子邮件:" + mail + "\n" + "爱好:" + role1);
142                     }
143                 }
144 
145             }
146         });
147         button_1.setBounds(253, 251, 93, 23);
148         getContentPane().add(button_1);
149 
150     }
151 }
152 
153     
154     
自我评审 问题
1
代码比较杂乱,降低了可读性
2 import里尽量不要用通配符*
3 缺少注释,每个类写了什么没有明确注明
4 没有删除多于的代码,代码过于冗长

Code Review