用Java创建一个窗体,窗体里有以下组件:一个姓名标签,一个空白文本框,一个性别标签,两个单选按钮分别表示男和女,一个年龄标签,一个能够选择年龄的可编辑的组合框,组合框既能选择年龄,也能自己手动输入年龄一个爱好标签,三个复选框分别表示体育,唱歌,美术以及一个文本域和一个保存按钮,一个添加按钮,一个读取按钮。要求在该窗体设置完信息后按下添加按钮能够将信息打印在文本域里,按下保存按钮时能够将信息保存到电脑的D盘下的out文本文档中,按下读取按钮时能够将电脑D盘下out文本文档的内容打印到文本框内,同时要求窗体运用boxlayout的布局方式将布局美化整齐
时间: 2024-03-27 21:41:56 浏览: 54
好的,我会为你解答这个问题。以下是Java代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyForm extends JFrame implements ActionListener {
private JLabel nameLabel, genderLabel, ageLabel, hobbyLabel;
private JTextField nameTextField;
private JRadioButton maleRadioButton, femaleRadioButton;
private JComboBox ageComboBox;
private JCheckBox sportCheckBox, singCheckBox, artCheckBox;
private JTextArea textArea;
private JButton saveButton, addButton, readButton;
public MyForm() {
// 设置窗口布局为BoxLayout
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
nameLabel = new JLabel("姓名:");
nameTextField = new JTextField(10);
genderLabel = new JLabel("性别:");
maleRadioButton = new JRadioButton("男");
femaleRadioButton = new JRadioButton("女");
ButtonGroup genderButtonGroup = new ButtonGroup();
genderButtonGroup.add(maleRadioButton);
genderButtonGroup.add(femaleRadioButton);
ageLabel = new JLabel("年龄:");
Integer[] ageList = new Integer[100];
for (int i = 1; i <= 100; i++) {
ageList[i-1] = i;
}
ageComboBox = new JComboBox(ageList);
ageComboBox.setEditable(true);
hobbyLabel = new JLabel("爱好:");
sportCheckBox = new JCheckBox("体育");
singCheckBox = new JCheckBox("唱歌");
artCheckBox = new JCheckBox("美术");
textArea = new JTextArea(10, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
addButton = new JButton("添加");
addButton.addActionListener(this);
saveButton = new JButton("保存");
saveButton.addActionListener(this);
readButton = new JButton("读取");
readButton.addActionListener(this);
JPanel namePanel = new JPanel();
namePanel.add(nameLabel);
namePanel.add(nameTextField);
add(namePanel);
JPanel genderPanel = new JPanel();
genderPanel.add(genderLabel);
genderPanel.add(maleRadioButton);
genderPanel.add(femaleRadioButton);
add(genderPanel);
JPanel agePanel = new JPanel();
agePanel.add(ageLabel);
agePanel.add(ageComboBox);
add(agePanel);
JPanel hobbyPanel = new JPanel();
hobbyPanel.add(hobbyLabel);
hobbyPanel.add(sportCheckBox);
hobbyPanel.add(singCheckBox);
hobbyPanel.add(artCheckBox);
add(hobbyPanel);
add(scrollPane);
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(saveButton);
buttonPanel.add(readButton);
add(buttonPanel);
setTitle("个人信息");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String gender = "";
if (maleRadioButton.isSelected()) {
gender = "男";
} else if (femaleRadioButton.isSelected()) {
gender = "女";
}
String age = ageComboBox.getSelectedItem().toString();
String hobby = "";
if (sportCheckBox.isSelected()) {
hobby += "体育 ";
}
if (singCheckBox.isSelected()) {
hobby += "唱歌 ";
}
if (artCheckBox.isSelected()) {
hobby += "美术 ";
}
String info = "姓名:" + nameTextField.getText() + ",性别:" + gender + ",年龄:" + age + ",爱好:" + hobby + "\n";
textArea.append(info);
} else if (e.getSource() == saveButton) {
try {
FileWriter fw = new FileWriter("D:\\out.txt", true);
fw.write(textArea.getText());
fw.close();
JOptionPane.showMessageDialog(null, "保存成功!");
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == readButton) {
try {
FileReader fr = new FileReader("D:\\out.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
textArea.append(line + "\n");
}
br.close();
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
MyForm form = new MyForm();
form.pack();
form.setVisible(true);
}
}
```
这段代码实现了一个窗体,包含了所有要求的组件,并且使用了BoxLayout布局,美化了布局。其中,添加按钮、保存按钮和读取按钮使用了一个ActionListener来监听点击事件,实现了在文本域中显示添加的信息,保存信息到文本文件以及读取文本文件中的信息。
阅读全文