声明StudentJFrame
时间: 2024-01-05 17:04:11 浏览: 27
好的,我可以帮你声明一个名为StudentJFrame的框架类,以下是一个可能的实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentJFrame extends JFrame {
private JComboBox<String> departmentComboBox;
private JComboBox<String> majorComboBox;
private JTextField idTextField;
// 其他成员变量
public StudentJFrame() {
// 设置窗口标题和大小
setTitle("学生信息管理系统");
setSize(400, 300);
setLocationRelativeTo(null);
// 创建学号、系、专业三个标签和对应的组件
JLabel idLabel = new JLabel("学号:");
idTextField = new JTextField();
idTextField.setEditable(false);
JLabel departmentLabel = new JLabel("系:");
departmentComboBox = new JComboBox<>(new String[] {"计算机科学与技术", "软件工程", "信息安全", "网络工程"});
JLabel majorLabel = new JLabel("专业:");
majorComboBox = new JComboBox<>(new String[] {"", "移动应用开发", "云计算与大数据技术", "人工智能"});
// 创建保存和取消按钮
JButton saveButton = new JButton("保存");
JButton cancelButton = new JButton("取消");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 保存学生信息
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 取消并清空输入
}
});
// 创建一个网格布局,将组件添加到窗口中
JPanel panel = new JPanel(new GridLayout(4, 2, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(idLabel);
panel.add(idTextField);
panel.add(departmentLabel);
panel.add(departmentComboBox);
panel.add(majorLabel);
panel.add(majorComboBox);
panel.add(saveButton);
panel.add(cancelButton);
add(panel);
// 监听系和专业选择框的变化,自动生成学号
departmentComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generateId();
}
});
majorComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generateId();
}
});
}
private void generateId() {
String department = departmentComboBox.getSelectedItem().toString();
String major = majorComboBox.getSelectedItem().toString();
String id = department + major;
// TODO: 根据系和专业生成学号
idTextField.setText(id);
}
// 其他方法
}
```
在这个实现中,我们创建了一个包含学号、系、专业三个标签和对应的组件的窗口,使用了网格布局将它们组织起来。我们还为系和专业选择框添加了监听器,当用户选择不同的系或专业时,自动生成对应的学号。
这只是一个简单的框架,你可以根据需求添加其他组件和功能。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)