Mutual review
1.Before review
//import java.awt.BorderLayout; //import java.awt.EventQueue; // //import javax.swing.JFrame; //import javax.swing.JPanel; //import javax.swing.border.EmptyBorder; //import javax.swing.JMenuBar; //import javax.swing.JMenu; //import javax.swing.JMenuItem; //import java.awt.Choice; //import java.awt.CardLayout; //import javax.swing.JSplitPane; //import javax.swing.JLabel; //import javax.swing.SwingConstants; //import javax.swing.JTextField; //import javax.swing.JRadioButton; //import javax.swing.JPasswordField; //import javax.swing.JComboBox; //import javax.swing.JCheckBox; //import javax.swing.JButton; //import javax.swing.DefaultComboBoxModel; //import java.awt.event.ActionListener; //import java.awt.event.ActionEvent; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.JTextComponent; public class Jsa extends JFrame { private JTextField username; private JTextField email; private JPasswordField password; private JLabel man; JLabel label = new JLabel("用户注册"); CheckboxGroup sex = new CheckboxGroup(); /** * Launch the application. */ public static void main(String[] args) { Jsa frame = new Jsa(); frame.setVisible(true); } /** * Create the frame. */ public Jsa() { setTitle("用户注册"); setBounds(100, 100, 414, 336); getContentPane().setLayout(null); label.setBounds(177, 10, 54, 15); getContentPane().add(label); JLabel label_1 = new JLabel("用户名"); label_1.setHorizontalAlignment(SwingConstants.CENTER); label_1.setBounds(74, 43, 54, 15); getContentPane().add(label_1); JLabel label_2 = new JLabel("性 别"); label_2.setHorizontalAlignment(SwingConstants.CENTER); label_2.setBounds(74, 78, 54, 15); getContentPane().add(label_2); JLabel label_3 = new JLabel("邮 箱"); label_3.setHorizontalAlignment(SwingConstants.CENTER); label_3.setBounds(74, 115, 54, 15); getContentPane().add(label_3); JLabel label_4 = new JLabel("密 码"); label_4.setHorizontalAlignment(SwingConstants.CENTER); label_4.setBounds(74, 151, 54, 15); getContentPane().add(label_4); JLabel label_5 = new JLabel("爱 好"); label_5.setHorizontalAlignment(SwingConstants.CENTER); label_5.setBounds(74, 182, 54, 15); getContentPane().add(label_5); Checkbox man = new Checkbox("男", true, sex); man.setBounds(146, 74, 54, 23); getContentPane().add(man); Checkbox woman = new Checkbox("女", false, sex); woman.setBounds(235, 74, 71, 23); getContentPane().add(woman); username = new JTextField(); username.setBounds(145, 40, 161, 21); getContentPane().add(username); username.setColumns(10); email = new JTextField(); email.setBounds(145, 112, 161, 21); getContentPane().add(email); email.setColumns(10); password = new JPasswordField(); password.setBounds(145, 148, 161, 21); getContentPane().add(password); JLabel label_6 = new JLabel("4-6\u4F4D"); label_6.setBounds(316, 151, 54, 15); getContentPane().add(label_6); JComboBox comboBox = new JComboBox(); comboBox.setModel(new DefaultComboBoxModel(new String[] { "运动", "音乐" })); comboBox.setToolTipText(""); comboBox.setBounds(145, 179, 93, 21); getContentPane().add(comboBox); // JCheckBox checkBox = new JCheckBox("运动"); // checkBox.setBounds(145, 218, 63, 23); // getContentPane().add(checkBox); // // JCheckBox checkBox_1 = new JCheckBox("音乐"); // checkBox_1.setBounds(243, 218, 63, 23); // getContentPane().add(checkBox_1); JButton button = new JButton("清空"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { username.setText(""); email.setText(""); password.setText(""); } }); button.setBounds(100, 251, 93, 23); getContentPane().add(button); JButton button_1 = new JButton("提交"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String name1 = username.getText(); String password1 = new String(password.getPassword()); String mail = email.getText(); String role1 = (String) comboBox.getSelectedItem(); String xb = null; if (man.getState() == true) { xb = "男"; } else { xb = "女"; } if (name1 == null || name1.length() <= 0 || password1 == null || password1.length() <= 0 || role1 == null || role1.length() <= 0 || mail == null || mail.length() <= 0) { JOptionPane.showMessageDialog(button_1, "请输入完整登入信息!"); } else if (mail != null && mail.length() > 0 && mail.indexOf('@') == -1 && mail.indexOf('.') <= mail.indexOf('@')) { JOptionPane.showMessageDialog(button_1, "电子邮件输入错误!"); } else if (password1.length() < 4 || password1.length() > 6) { JOptionPane.showMessageDialog(button_1, "密码不合规范!"); } else { int n = JOptionPane.showConfirmDialog(null, "用户名:" + name1 + "\n" + "性别:" + xb + "\n" + "电子邮件:" + mail + "\n" + "爱好:" + role1, "选择一个选项", JOptionPane.YES_NO_CANCEL_OPTION); switch (n) { case 0: JOptionPane.showMessageDialog(button_1, "用户名:" + name1 + "\n" + "性别:" + xb + "\n" + "电子邮件:" + mail + "\n" + "爱好:" + role1); } } } }); button_1.setBounds(253, 251, 93, 23); getContentPane().add(button_1); } }
2.After review
1.该部分代码有被注释了的代码没有删除,看上去很杂乱
2.代码编写不够规范,函数的命名没有意义和规范性,大括号的位置不对,行的长度以及代码缩进都存在了一些小问题
3.代码没有注释,没有解说
4.没有把功能之间的模块划分清楚,有的混在一起
5.运行后,提交信息显示的界面,按钮“No”和“取消”功能重复,可以删除其中一个
package tt; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.JTextComponent; //要完成一个用户注册的界面 public class UserRegistrationInterface extends JFrame { // 定义对象 private JTextField username; private JTextField email; private JPasswordField password; private JLabel man; JLabel label = new JLabel("用户注册"); CheckboxGroup sex = new CheckboxGroup(); /** * Launch the application. */ public static void main(String[] args) { UserRegistrationInterface frame = new UserRegistrationInterface(); frame.setVisible(true); } /** * Create the frame. */ public UserRegistrationInterface() { // 设置整个界面的大小 setBounds(100, 100, 414, 336); getContentPane().setLayout(null); // 设置界面标题 setTitle("用户注册"); label.setBounds(177, 10, 54, 15); getContentPane().add(label); // 设置界面该有的属性 // 设置用户名 JLabel label_1 = new JLabel("用户名"); label_1.setHorizontalAlignment(SwingConstants.CENTER); label_1.setBounds(74, 43, 54, 15); getContentPane().add(label_1); // 设置文本框用于输入用户名 username = new JTextField(); username.setBounds(145, 40, 161, 21); getContentPane().add(username); username.setColumns(10); // 设置性别 JLabel label_2 = new JLabel("性 别"); label_2.setHorizontalAlignment(SwingConstants.CENTER); label_2.setBounds(74, 78, 54, 15); getContentPane().add(label_2); // 利用单选按钮选择性别 Checkbox man = new Checkbox("男", true, sex); man.setBounds(146, 74, 54, 23); getContentPane().add(man); Checkbox woman = new Checkbox("女", false, sex); woman.setBounds(235, 74, 71, 23); getContentPane().add(woman); // 设置邮箱 JLabel label_3 = new JLabel("邮 箱"); label_3.setHorizontalAlignment(SwingConstants.CENTER); label_3.setBounds(74, 115, 54, 15); getContentPane().add(label_3); // 设置文本框输入邮箱地址 email = new JTextField(); email.setBounds(145, 112, 161, 21); getContentPane().add(email); email.setColumns(10); // 设置密码 JLabel label_4 = new JLabel("密 码"); label_4.setHorizontalAlignment(SwingConstants.CENTER); label_4.setBounds(74, 151, 54, 15); getContentPane().add(label_4); // 设置文本框用于输入密码 password = new JPasswordField(); password.setBounds(145, 148, 161, 21); getContentPane().add(password); // 设置文本框内的字符大小 JLabel label_6 = new JLabel("4-6\u4F4D"); label_6.setBounds(316, 151, 54, 15); getContentPane().add(label_6); // 设置爱好 JLabel label_5 = new JLabel("爱 好"); label_5.setHorizontalAlignment(SwingConstants.CENTER); label_5.setBounds(74, 182, 54, 15); getContentPane().add(label_5); // 用下拉框的格式设置有关爱好的种类 JComboBox comboBox = new JComboBox(); comboBox.setModel(new DefaultComboBoxModel(new String[] { "运动", "音乐" })); comboBox.setToolTipText(""); comboBox.setBounds(145, 179, 93, 21); getContentPane().add(comboBox); // 设置Button JButton button = new JButton("清空"); button.setBounds(100, 251, 93, 23); getContentPane().add(button); //点击清空按钮可以清空每个文本框的内容 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { username.setText(""); email.setText(""); password.setText(""); } }); JButton button_1 = new JButton("提交"); button_1.setBounds(253, 251, 93, 23); getContentPane().add(button_1); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String name1 = username.getText(); String password1 = new String(password.getPassword()); String mail = email.getText(); String role1 = (String) comboBox.getSelectedItem(); String xb = null; // if语句判断属性性别 if (man.getState() == true) { xb = "男"; } else { xb = "女"; } // if...else语句对某些属性相对应的文本框进行要求,不满足相关要求的不能提交成功,反之则成功 if (name1 == null || name1.length() <= 0 || password1 == null || password1.length() <= 0 || role1 == null || role1.length() <= 0 || mail == null || mail.length() <= 0) { JOptionPane.showMessageDialog(button_1, "请输入完整登入信息!"); } else if (mail != null && mail.length() > 0 && mail.indexOf('@') == -1 && mail.indexOf('.') <= mail.indexOf('@')) { JOptionPane.showMessageDialog(button_1, "电子邮件输入错误!"); } else if (password1.length() < 4 || password1.length() > 6) { JOptionPane.showMessageDialog(button_1, "密码不合规范!"); } else { //提交成功后界面,界面会显示出用户的姓名,性别,电子邮件,爱好,选择yes和no int n = JOptionPane.showConfirmDialog(null, "用户名:" + name1 + "\n" + "性别:" + xb + "\n" + "电子邮件:" + mail + "\n" + "爱好:" + role1, "选择一个选项", JOptionPane.YES_NO_OPTION); //选择yes后,界面会显示出用户的姓名,性别,电子邮件,爱好,可点击确定 switch (n) { case 0: JOptionPane.showMessageDialog(button_1, "用户名:" + name1 + "\n" + "性别:" + xb + "\n" + "电子邮件:" + mail + "\n" + "爱好:" + role1); } } } }); } }