okbtn.addActionListener(e -> { String name = nameField.getText(); String sex = malebtn.isSelected() ? "男" : "女"; String department = (String) departmentField.getSelectedItem(); ArrayList<String> hobby = new ArrayList<>(); if (hobby1.isSelected()) hobby.add("做饭"); if (hobby2.isSelected()) hobby.add("阅读"); if (hobby3.isSelected()) hobby.add("睡觉"); String phone = phoneField.getText(); String resume = resumeField.getText(); String infoName = "my name\t\t" + name; String infoSex = "sex\t\t" + sex; String infoDepartment = "department\t" + department; String infohobby = "hobby\t\t" + hobby; String infophone = "phoneNo\t\t" + phone; String inforesume = "resume\n" + resume; String all = infoName + "\n" + infoSex + "\n" + infoDepartment + "\n" + infohobby + "\n" + infophone + "\n" + inforesume; JLabel information = new JLabel(); scrollPane.add(information); panel_3.add(information); information.setText(all); System.out.println(all); });优化代码,使得在文本框输出
时间: 2024-01-30 11:04:31 浏览: 67
第4章事件监听器--java.ppt
all 变量的内容。修改后的代码如下:
```
okbtn.addActionListener(e -> {
String name = nameField.getText();
String sex = malebtn.isSelected() ? "男" : "女";
String department = (String) departmentField.getSelectedItem();
ArrayList<String> hobby = new ArrayList<>();
if (hobby1.isSelected())
hobby.add("做饭");
if (hobby2.isSelected())
hobby.add("阅读");
if (hobby3.isSelected())
hobby.add("睡觉");
String phone = phoneField.getText();
String resume = resumeField.getText();
String infoName = "my name\t\t" + name;
String infoSex = "sex\t\t" + sex;
String infoDepartment = "department\t" + department;
String infohobby = "hobby\t\t" + hobby;
String infophone = "phoneNo\t\t" + phone;
String inforesume = "resume\n" + resume;
String all = infoName + "\n" + infoSex + "\n" + infoDepartment + "\n" + infohobby + "\n" + infophone + "\n"
+ inforesume;
textArea.setText(all); // 将 all 的内容输出到文本框
});
```
阅读全文