JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.insets = new Insets(10, 10, 10, 10); JLabel usernameLabel = new JLabel("用户名:"); constraints.gridx = 0; constraints.gridy = 0; panel.add(usernameLabel, constraints); usernameField = new JTextField(20); constraints.gridx = 1; constraints.gridy = 0; panel.add(usernameField, constraints); JLabel passwordLabel = new JLabel("密码:"); constraints.gridx = 0; constraints.gridy = 1; panel.add(passwordLabel, constraints); passwordField = new JPasswordField(20); constraints.gridx = 1; constraints.gridy = 1; panel.add(passwordField, constraints); JLabel confirmPasswordLabel = new JLabel("确认密码:"); constraints.gridx = 0; constraints.gridy = 2; panel.add(confirmPasswordLabel, constraints); confirmPasswordField = new JPasswordField(20); constraints.gridx = 1; constraints.gridy = 2; panel.add(confirmPasswordField, constraints); JLabel phoneLabel = new JLabel("手机号:"); constraints.gridx = 0; constraints.gridy = 3; panel.add(phoneLabel, constraints); phoneField = new JTextField(20); constraints.gridx = 1; constraints.gridy = 3; panel.add(phoneField, constraints); JLabel genderLabel = new JLabel("性别:"); constraints.gridx = 0; constraints.gridy = 4; panel.add(genderLabel, constraints); maleButton = new JRadioButton("男"); femaleButton = new JRadioButton("女"); ButtonGroup genderGroup = new ButtonGroup(); genderGroup.add(maleButton); genderGroup.add(femaleButton); JPanel genderPanel = new JPanel(new FlowLayout()); genderPanel.add(maleButton); genderPanel.add(femaleButton); constraints.gridx = 1; constraints.gridy = 4; panel.add(genderPanel, constraints); registerButton = new JButton("注册"); constraints.gridx = 1; constraints.gridy = 5; panel.add(registerButton, constraints);提取成一个方法
时间: 2024-02-02 17:04:51 浏览: 78
Java绘图和JPanel容器.pdf
可以将上述代码提取成一个方法,如下所示:
```java
public JPanel createRegistrationPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(10, 10, 10, 10);
JLabel usernameLabel = new JLabel("用户名:");
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(usernameLabel, constraints);
usernameField = new JTextField(20);
constraints.gridx = 1;
constraints.gridy = 0;
panel.add(usernameField, constraints);
JLabel passwordLabel = new JLabel("密码:");
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(passwordLabel, constraints);
passwordField = new JPasswordField(20);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(passwordField, constraints);
JLabel confirmPasswordLabel = new JLabel("确认密码:");
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(confirmPasswordLabel, constraints);
confirmPasswordField = new JPasswordField(20);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(confirmPasswordField, constraints);
JLabel phoneLabel = new JLabel("手机号:");
constraints.gridx = 0;
constraints.gridy = 3;
panel.add(phoneLabel, constraints);
phoneField = new JTextField(20);
constraints.gridx = 1;
constraints.gridy = 3;
panel.add(phoneField, constraints);
JLabel genderLabel = new JLabel("性别:");
constraints.gridx = 0;
constraints.gridy = 4;
panel.add(genderLabel, constraints);
maleButton = new JRadioButton("男");
femaleButton = new JRadioButton("女");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleButton);
genderGroup.add(femaleButton);
JPanel genderPanel = new JPanel(new FlowLayout());
genderPanel.add(maleButton);
genderPanel.add(femaleButton);
constraints.gridx = 1;
constraints.gridy = 4;
panel.add(genderPanel, constraints);
registerButton = new JButton("注册");
constraints.gridx = 1;
constraints.gridy = 5;
panel.add(registerButton, constraints);
return panel;
}
```
阅读全文