private JButton 学生,系统管理员,宿舍管理员;代码中有三个选择按钮,选择完如何关闭这个new 选择身份()方法java代码
时间: 2024-03-08 16:46:08 浏览: 30
可以在选择身份之后,调用 `dispose()` 方法来关闭当前的窗口。`dispose()` 方法会释放窗口占用的所有资源。可以在按钮的点击事件中调用这个方法。例如:
```
学生.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理学生身份的代码
dispose(); // 关闭当前窗口
}
});
系统管理员.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理系统管理员身份的代码
dispose(); // 关闭当前窗口
}
});
宿舍管理员.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理宿舍管理员身份的代码
dispose(); // 关闭当前窗口
}
});
```
这样,当用户点击任意一个按钮时,会执行相应的代码并关闭选择身份的窗口。
相关问题
private JTextArea textArea = new JTextArea(); private JScrollPane scrollPane = new JScrollPane(textArea); private JFileChooser fileChooser = new JFileChooser(); private String currentFile = "Java文本编辑器"; private boolean saved = true; private JToolBar toolBar = new JToolBar();//工具栏设置 private JButton saveButton = new JButton("保存");//设置保存按钮 private JButton cutButton = new JButton("剪切");//设置剪切按钮 private JButton copyButton = new JButton("复制");//设置复制按钮 private JButton pasteButton = new JButton("粘贴");//设置粘贴按钮 private JButton findButton = new JButton("查找");//设置查找按钮 private JPanel statusBar = new JPanel();//设置状态栏 private JLabel statusLabel = new JLabel("准备中");
这段代码是一个简单的Java文本编辑器的实现,其中包含了以下组件:
1. JTextArea:文本区域,用于显示和编辑文本。
2. JScrollPane:滚动条,用于实现文本区域的滚动。
3. JFileChooser:文件选择器,用于选择和打开文件。
4. JToolBar:工具栏,用于显示和操作编辑器的工具。
5. JButton:按钮,用于实现工具栏中的各种操作,如保存、剪切、复制、粘贴、查找等。
6. JPanel:面板,用于实现状态栏。
7. JLabel:标签,用于显示状态栏中的状态信息。
这个编辑器的主要功能是打开、编辑和保存文本文件,同时还提供了一些常用的文本操作功能,如剪切、复制、粘贴和查找等。状态栏可以显示当前编辑器的状态信息,如是否已保存等。
package guanlixitong; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.util.Collection; public class StudentManage { public void StudentMainInterface() { //创建一个窗口,并设置窗口名称为”登录” JFrame jFrame = new JFrame("学生管理系统"); //设置窗口大小 jFrame.setSize(1400, 900); //设置相对位置:屏幕中间 jFrame.setLocationRelativeTo(null); JButton adminButton = new JButton("宿舍管理员管理"); JButton studentButton = new JButton("学生信息管理"); JButton buildingButton = new JButton("宿舍楼信息管理"); JButton dormButton = new JButton("宿舍信息管理"); JButton checkInButton = new JButton("学生住宿管理"); JButton absentButton = new JButton("学生缺勤管理"); // 将按钮添加到窗口中 JPanel panel = new JPanel(new GridLayout(6, 1, 10, 10)); panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); panel.add(adminButton); panel.add(studentButton); panel.add(buildingButton); panel.add(dormButton); panel.add(checkInButton); panel.add(absentButton); getContentPane().add(panel); adminButton.addActionListener((ActionListener) this); studentButton.addActionListener((ActionListener) this); buildingButton.addActionListener((ActionListener) this); dormButton.addActionListener((ActionListener) this); checkInButton.addActionListener((ActionListener) this); absentButton.addActionListener((ActionListener) this); //确保使用窗口关闭按钮,能够正常退出,结束进程! jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //禁止对窗口大小进行缩放处理 jFrame.setResizable(false); //设置可见 jFrame.setVisible(true); } }
这段代码看起来是一个学生管理系统的界面代码,其中包括了六个按钮,分别是宿舍管理员管理、学生信息管理、宿舍楼信息管理、宿舍信息管理、学生住宿管理以及学生缺勤管理。每个按钮都添加了相应的监听器,点击按钮后会执行相应的操作。其中设置了窗口大小、相对位置、关闭按钮等。但是此处代码中的getContentPane()方法没有被定义,可能需要进行一些修改。
阅读全文
相关推荐












