用java软件功能要求: 1. 要求在文本框中输入有关信息,单击“添加”按钮,在下面的多行文本域中显示输入的的信 息(如姓名文本框、性别用单选按钮、年龄组合框、爱好复选框); 2. 单击“读取”按钮将文本文件中的信息显示到多行文本域中。 3. 单击“保存”按钮将把多行文本域中的内容保存到文本文件中,并退出窗口。
时间: 2024-03-24 15:39:09 浏览: 67
好的,根据您的要求,我可以为您提供以下Java代码示例,实现您的功能要求:
```java
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class UserInfoGUI {
private JFrame frame;
private JTextField nameField;
private JRadioButton maleRadioButton;
private JRadioButton femaleRadioButton;
private JComboBox<Integer> ageComboBox;
private JCheckBox sportCheckBox;
private JCheckBox musicCheckBox;
private JCheckBox travelCheckBox;
private JTextArea infoArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserInfoGUI window = new UserInfoGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UserInfoGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel nameLabel = new JLabel("姓名:");
nameLabel.setBounds(10, 10, 54, 15);
frame.getContentPane().add(nameLabel);
nameField = new JTextField();
nameField.setBounds(74, 7, 150, 21);
frame.getContentPane().add(nameField);
nameField.setColumns(10);
JLabel genderLabel = new JLabel("性别:");
genderLabel.setBounds(10, 40, 54, 15);
frame.getContentPane().add(genderLabel);
maleRadioButton = new JRadioButton("男");
maleRadioButton.setSelected(true);
maleRadioButton.setBounds(74, 36, 50, 23);
frame.getContentPane().add(maleRadioButton);
femaleRadioButton = new JRadioButton("女");
femaleRadioButton.setBounds(126, 36, 50, 23);
frame.getContentPane().add(femaleRadioButton);
ageComboBox = new JComboBox<Integer>(new Integer[] { 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 });
ageComboBox.setBounds(74, 68, 80, 21);
frame.getContentPane().add(ageComboBox);
JLabel ageLabel = new JLabel("年龄:");
ageLabel.setBounds(10, 71, 54, 15);
frame.getContentPane().add(ageLabel);
sportCheckBox = new JCheckBox("运动");
sportCheckBox.setBounds(10, 105, 60, 23);
frame.getContentPane().add(sportCheckBox);
musicCheckBox = new JCheckBox("音乐");
musicCheckBox.setBounds(74, 105, 60, 23);
frame.getContentPane().add(musicCheckBox);
travelCheckBox = new JCheckBox("旅行");
travelCheckBox.setBounds(138, 105, 60, 23);
frame.getContentPane().add(travelCheckBox);
JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String gender = maleRadioButton.isSelected() ? "男" : "女";
int age = (int) ageComboBox.getSelectedItem();
String hobby = "";
if (sportCheckBox.isSelected()) {
hobby += "运动 ";
}
if (musicCheckBox.isSelected()) {
hobby += "音乐 ";
}
if (travelCheckBox.isSelected()) {
hobby += "旅行";
}
String info = name + "," + gender + "," + age + "岁,爱好:" + hobby;
infoArea.append(info + "\n");
}
});
addButton.setBounds(10, 141, 93, 23);
frame.getContentPane().add(addButton);
JButton readButton = new JButton("读取");
readButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedReader reader = new BufferedReader(new FileReader("userinfo.txt"));
String line;
while ((line = reader.readLine()) != null) {
infoArea.append(line + "\n");
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
readButton.setBounds(113, 141, 93, 23);
frame.getContentPane().add(readButton);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("userinfo.txt"));
writer.write(infoArea.getText());
writer.close();
frame.dispose();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
saveButton.setBounds(216, 141, 93, 23);
frame.getContentPane().add(saveButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 174, 414, 77);
frame.getContentPane().add(scrollPane);
infoArea = new JTextArea();
scrollPane.setViewportView(infoArea);
}
}
```
这段代码使用了Java Swing库来创建一个图形用户界面,包含了您要求的文本框、单选按钮、组合框、复选框和多行文本域。在代码中,单击“添加”按钮将会把输入的信息格式化后追加到多行文本域中;单击“读取”按钮将会从文本文件中读取内容并显示到多行文本域中;单击“保存”按钮将会把多行文本域中的内容保存到文本文件中,并关闭窗口。
阅读全文