Peer code review

源代码如下:

  1 package jpg;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.EventQueue;
  5 
  6 import javax.swing.JFrame;
  7 import javax.swing.JPanel;
  8 import javax.swing.border.EmptyBorder;
  9 import javax.swing.JLabel;
 10 import javax.swing.JOptionPane;
 11 import javax.swing.JTextField;
 12 import javax.swing.JButton;
 13 import javax.swing.JComboBox;
 14 import javax.swing.DefaultComboBoxModel;
 15 import java.awt.event.ActionListener;
 16 import java.awt.event.ActionEvent;
 17 import javax.swing.ImageIcon;
 18 import javax.swing.JPasswordField;
 19 
 20 public class Day extends JFrame {
 21 
 22     private JPanel contentPane;
 23     private JTextField textField;
 24     private JPasswordField passwordField;
 25 
 26     public static void main(String[] args) {
 27         
 28                     Day frame = new Day();
 29                     frame.setVisible(true);
 30             
 31     }
 32 
 33     
 34     public Day() {
 35         
 36         setTitle("用户登录");
 37         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 38         setBounds(100, 100, 450, 300);
 39         
 40         contentPane = new JPanel();
 41         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 42         setContentPane(contentPane);
 43         contentPane.setLayout(null);
 44         this.setResizable(false);
 45         
 46         JLabel label = new JLabel("用户名");
 47         label.setBounds(39, 45, 54, 15);
 48         contentPane.add(label);
 49         
 50         JComboBox comboBox = new JComboBox();
 51         comboBox.setModel(new DefaultComboBoxModel(new String[] {"u7BA1u7406u5458   ", "u6536u94F6u5458"}));
 52         comboBox.setBounds(126, 121, 173, 21);
 53         contentPane.add(comboBox);
 54         
 55         
 56         textField = new JTextField();
 57         textField.setBounds(126, 42, 173, 21);
 58         contentPane.add(textField);
 59         textField.setColumns(30);
 60         
 61         JButton button = new JButton("确定");
 62         button.addActionListener(new ActionListener() {
 63             public void actionPerformed(ActionEvent e) {
 64                 
 65                 
 66                 String userName=textField.getText();
 67                 
 68                 
 69                 String passWord=String.valueOf( passwordField.getPassword());
 70                 
 71                 String role=(String)comboBox.getSelectedItem();
 72                 
 73                 if(userName==null||userName.length()<=0||passWord==null||passWord.length()<=0||role==null||role.length()<=0){
 74                     JOptionPane.showMessageDialog(contentPane,"请输入完整登录信息!");
 75                 }
 76                 else if(userName.equals("SMMS")&&passWord.equals("123456")&&role.equals("管理员"))
 77                 
 78                 JOptionPane.showMessageDialog(contentPane,"欢迎登录SMMS超市购物系统!");
 79                 
 80                 else
 81                     JOptionPane.showMessageDialog(contentPane,"输入信息错误,请重新输入!");
 82             }});
 83         button.setBounds(142, 194, 77, 23);
 84         contentPane.add(button);
 85         
 86         JLabel label_1 = new JLabel("密码");
 87         label_1.setBounds(39, 88, 54, 15);
 88         contentPane.add(label_1);
 89         
 90         JLabel label_2 =        new JLabel("角色");
 91         label_2.setBounds(39, 124, 54, 15);
 92         contentPane.add(label_2);
 93         
 94         passwordField = new JPasswordField();
 95         passwordField.setBounds(126, 85, 173, 21);
 96         contentPane.add(passwordField);
 97         
 98         JButton button_1 = new JButton("取消");
 99         button_1.setBounds(245, 194, 93, 23);
100         contentPane.add(button_1);
101         
102         JLabel label_3 = new JLabel("");
103         label_3.setIcon(new ImageIcon("C:\Users\lenovo.Lenovo-PC\Pictures\butterfly.jpg"));
104         label_3.setBounds(0, 0, 434, 261);
105         contentPane.add(label_3);
106     }
107 }

经审查发现以下问题:

1.代码格式不规范

 26     public static void main(String[] args) {
 27         
 28                     Day frame = new Day();
 29                     frame.setVisible(true);
 30             
 31     }
 32 
 33     
 34     public Day() {
 35         
 36         setTitle("用户登录");
 37         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 38         setBounds(100, 100, 450, 300);
 90         JLabel label_2 =        new JLabel("角色");
 91         label_2.setBounds(39, 124, 54, 15);
 92         contentPane.add(label_2);

修改后如下:

1     public static void main(String[] args) {
2         Day frame = new Day();
3         frame.setVisible(true);
4     }
5 
6     public Day() {
7         setTitle("用户登录");
8         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9         setBounds(100, 100, 450, 300);
 90         JLabel label_2 =new JLabel("角色");
 91         label_2.setBounds(39, 124, 54, 15);
 92         contentPane.add(label_2);

2.在复杂的条件表达式中,没有用括号清楚的表达逻辑的优先级

 62         button.addActionListener(new ActionListener() {
 63             public void actionPerformed(ActionEvent e) {
 64                 
 65                 
 66                 String userName=textField.getText();
 67                 
 68                 
 69                 String passWord=String.valueOf( passwordField.getPassword());
 70                 
 71                 String role=(String)comboBox.getSelectedItem();
 72                 
 73                 if(userName==null||userName.length()<=0||passWord==null||passWord.length()<=0||role==null||role.length()<=0){
 74                     JOptionPane.showMessageDialog(contentPane,"请输入完整登录信息!");
 75                 }
 76                 else if(userName.equals("SMMS")&&passWord.equals("123456")&&role.equals("管理员"))
 77                 
 78                 JOptionPane.showMessageDialog(contentPane,"欢迎登录SMMS超市购物系统!");
 79                 
 80                 else
 81                     JOptionPane.showMessageDialog(contentPane,"输入信息错误,请重新输入!");
 82             }});

修改后如下:

 1         button.addActionListener(new ActionListener() {
 2             public void actionPerformed(ActionEvent e) {
 3 
 4                 String userName = textField.getText();
 5 
 6                 String passWord = String.valueOf(passwordField.getPassword());
 7 
 8                 String role = (String) comboBox.getSelectedItem();
 9 
10                 if (userName == null || userName.length() <= 0 || passWord == null || passWord.length() <= 0
11                         || role == null || role.length() <= 0) {
12 
13                     JOptionPane.showMessageDialog(contentPane, "请输入完整登录信息!");
14 
15                 } else if (userName.equals("SMMS") && passWord.equals("123456") && role.equals("管理员"))
16 
17                     JOptionPane.showMessageDialog(contentPane, "欢迎登录SMMS超市购物系统!");
18 
19                   else
20                     JOptionPane.showMessageDialog(contentPane, "输入信息错误,请重新输入!");
21 
22             }
23         });

3.没有明确的注释代码的作用

 34     public Day() {
 35         
 36         setTitle("用户登录");
 37         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 38         setBounds(100, 100, 450, 300);
 39         
 40         contentPane = new JPanel();
 41         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 42         setContentPane(contentPane);
 43         contentPane.setLayout(null);
 44         this.setResizable(false);
 71                 String role=(String)comboBox.getSelectedItem();

修改后如下:

 34     public Day() {
 35         // 窗体属性-给你的范围
 36         setTitle("用户登录");
 37         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 38         setBounds(100, 100, 450, 300);
 39         // 内容模板
 40         contentPane = new JPanel();
 41         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 42         setContentPane(contentPane);// 重要
 43         contentPane.setLayout(null);// 绝对布局
 44         this.setResizable(false);// 面板的尺寸
 71               String role = (String) comboBox.getSelectedItem();// 强制类型的转换

以上为个人意见,欢迎补充。

原文地址:https://www.cnblogs.com/ly97/p/6600481.html