用Java语言 设计一个关于个人信息的gui界面,包含姓名,性别单选,电话号码,保存和返回按钮带界面布局
时间: 2024-04-04 14:31:00 浏览: 16
好的,我可以为您提供一个简单的Java GUI界面,用于输入和保存个人信息。以下是代码和界面布局:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PersonalInfoGUI extends JFrame implements ActionListener {
private JLabel nameLabel, genderLabel, phoneLabel;
private JTextField nameField, phoneField;
private JRadioButton maleButton, femaleButton;
private JButton saveButton, backButton;
public PersonalInfoGUI() {
// 设置窗口标题和大小
setTitle("个人信息");
setSize(400, 200);
// 创建标签和文本框
nameLabel = new JLabel("姓名:");
nameField = new JTextField(20);
genderLabel = new JLabel("性别:");
maleButton = new JRadioButton("男");
femaleButton = new JRadioButton("女");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleButton);
genderGroup.add(femaleButton);
phoneLabel = new JLabel("电话号码:");
phoneField = new JTextField(20);
// 创建保存和返回按钮
saveButton = new JButton("保存");
saveButton.addActionListener(this);
backButton = new JButton("返回");
backButton.addActionListener(this);
// 创建面板和布局
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(3, 2));
infoPanel.add(nameLabel);
infoPanel.add(nameField);
infoPanel.add(genderLabel);
infoPanel.add(maleButton);
infoPanel.add(new JLabel(""));
infoPanel.add(femaleButton);
infoPanel.add(phoneLabel);
infoPanel.add(phoneField);
infoPanel.add(saveButton);
infoPanel.add(backButton);
// 添加面板到窗口中心
add(infoPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton) {
// TODO: 保存个人信息
JOptionPane.showMessageDialog(this, "已保存个人信息。");
} else if (e.getSource() == backButton) {
// TODO: 返回上一页
JOptionPane.showMessageDialog(this, "返回上一页。");
}
}
public static void main(String[] args) {
PersonalInfoGUI gui = new PersonalInfoGUI();
gui.setVisible(true);
}
}
```
界面布局:
```
┌─────────────────┐
│ 姓名: │ □男 □女
│ │
│ 性别: │
│ │
│ 电话号码: │
│ │
│ 保存 │ 返回
└─────────────────┘
```
这是一个简单的GUI界面,包含了姓名、性别、电话号码等信息。用户可以选择男性或女性,然后点击保存按钮来保存个人信息,或者点击返回按钮返回上一页。希望这能对您有所帮助!