my code review

原代码:

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

修改意见:

1.代码不符合编程规范,大括号位置不对,行的长度、缩进也不对

2.代码没有尽可能的模块化,很零散

3.不是所有的代码都简单易懂,有些自己定义的变量的名称随便瞎取名字,别人看不懂

原文地址:https://www.cnblogs.com/u1118733/p/6603312.html