用Java创建一个窗体,窗体里有以下组件:一个姓名标签,一个空白文本框,一个性别标签,两个单选按钮分别表示男和女,一个爱好标签,三个复选框分别表示体育,唱歌,美术以及一个文本域和一个保存按钮,一个添加按钮。要求在该窗体设置完信息后按下添加按钮能够将信息打印在文本域里,按下保存按钮时能够将信息保存到电脑的D盘下的out文本文档中
时间: 2024-03-27 19:41:57 浏览: 88
java创建窗体
好的,以下是使用Java Swing创建窗体并添加组件的代码,其中包括保存和添加按钮的事件处理程序,可以将信息打印在文本域中,并将信息保存到电脑的D盘下的out文本文档中:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyForm extends JFrame implements ActionListener {
private JLabel label_name, label_gender, label_hobby;
private JTextField field_name;
private JRadioButton radio_male, radio_female;
private ButtonGroup group_gender;
private JCheckBox check_sports, check_sing, check_art;
private JTextArea area_info;
private JButton button_save, button_add;
public MyForm() {
// 设置窗体标题和大小
setTitle("个人信息");
setSize(400, 400);
// 创建姓名标签和文本框,并添加到窗体上
label_name = new JLabel("姓名:");
field_name = new JTextField(20);
JPanel panel_name = new JPanel();
panel_name.setLayout(new FlowLayout());
panel_name.add(label_name);
panel_name.add(field_name);
add(panel_name, BorderLayout.NORTH);
// 创建性别标签和单选按钮,并添加到窗体上
label_gender = new JLabel("性别:");
radio_male = new JRadioButton("男");
radio_female = new JRadioButton("女");
group_gender = new ButtonGroup();
group_gender.add(radio_male);
group_gender.add(radio_female);
JPanel panel_gender = new JPanel();
panel_gender.setLayout(new FlowLayout());
panel_gender.add(label_gender);
panel_gender.add(radio_male);
panel_gender.add(radio_female);
add(panel_gender, BorderLayout.CENTER);
// 创建爱好标签和复选框,并添加到窗体上
label_hobby = new JLabel("爱好:");
check_sports = new JCheckBox("体育");
check_sing = new JCheckBox("唱歌");
check_art = new JCheckBox("美术");
JPanel panel_hobby = new JPanel();
panel_hobby.setLayout(new FlowLayout());
panel_hobby.add(label_hobby);
panel_hobby.add(check_sports);
panel_hobby.add(check_sing);
panel_hobby.add(check_art);
add(panel_hobby, BorderLayout.SOUTH);
// 创建文本域和添加、保存按钮,并添加到窗体上
area_info = new JTextArea(10, 30);
area_info.setEditable(false);
JScrollPane scroll_info = new JScrollPane(area_info);
JPanel panel_buttons = new JPanel();
panel_buttons.setLayout(new FlowLayout());
button_add = new JButton("添加");
button_save = new JButton("保存");
button_add.addActionListener(this);
button_save.addActionListener(this);
panel_buttons.add(button_add);
panel_buttons.add(button_save);
add(scroll_info, BorderLayout.WEST);
add(panel_buttons, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_add) {
String name = field_name.getText();
String gender = radio_male.isSelected() ? "男" : "女";
String hobby = "";
if (check_sports.isSelected()) {
hobby += "体育 ";
}
if (check_sing.isSelected()) {
hobby += "唱歌 ";
}
if (check_art.isSelected()) {
hobby += "美术 ";
}
String info = "姓名:" + name + "\n性别:" + gender + "\n爱好:" + hobby + "\n\n";
area_info.append(info);
field_name.setText("");
group_gender.clearSelection();
check_sports.setSelected(false);
check_sing.setSelected(false);
check_art.setSelected(false);
} else if (e.getSource() == button_save) {
try {
FileWriter writer = new FileWriter("D:/out.txt");
writer.write(area_info.getText());
writer.close();
JOptionPane.showMessageDialog(this, "信息保存成功!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "信息保存失败!");
}
}
}
public static void main(String[] args) {
MyForm form = new MyForm();
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setVisible(true);
}
}
```
运行该程序后,可以看到一个窗体,其中包含姓名标签、空白文本框、性别标签、两个单选按钮、爱好标签、三个复选框、文本域、添加按钮和保存按钮。
在填写完个人信息后,按下添加按钮,信息会以指定格式打印在文本域中,同时清空文本框和单选按钮、复选框的状态。在填写完多个个人信息后,按下保存按钮,信息会被保存到电脑的D盘下的out文本文档中。
阅读全文