利用swing包中组件实现如下gui 学号 姓名 性别
时间: 2024-02-24 09:57:09 浏览: 108
姓名+学号.do
好的,以下是一个实现该GUI界面的例子:
```java
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
private JLabel studentIdLabel, nameLabel, genderLabel;
private JTextField studentIdField, nameField;
private JRadioButton maleRadioButton, femaleRadioButton;
private JButton okButton;
public MyFrame() {
setTitle("学生信息");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridLayout(3, 2));
studentIdLabel = new JLabel("学号");
nameLabel = new JLabel("姓名");
genderLabel = new JLabel("性别");
studentIdField = new JTextField();
nameField = new JTextField();
maleRadioButton = new JRadioButton("男");
femaleRadioButton = new JRadioButton("女");
okButton = new JButton("确定");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleRadioButton);
genderGroup.add(femaleRadioButton);
panel.add(studentIdLabel);
panel.add(studentIdField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(genderLabel);
JPanel genderPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
genderPanel.add(maleRadioButton);
genderPanel.add(femaleRadioButton);
panel.add(genderPanel);
add(panel, BorderLayout.CENTER);
add(okButton, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
```
这是一个学生信息界面,包括学号、姓名和性别三个信息的输入框和一个确定按钮。性别使用了单选按钮,男女分别对应不同的按钮。您可以根据自己的需求进行更改和扩展。
阅读全文