JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        RegisterStudent panel_1 = new RegisterStudent();
        panel_1.setVisible(true);
    }
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);


有没有一种方法,如果一个特定的窗口已经打开,就不能再次打开?
因为,我不希望用户多次单击按钮,从而导致打开几个具有相同内容的窗口?

最佳答案

在鼠标侦听器块之外创建panel_1变量,并将其初始化为null。单击鼠标后,检查panel_1是否为空,如果为null,则创建它。

final RegisterStudent panel_1 = new RegisterStudent();
JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        panel_1.setVisible(true);
    }
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);

10-07 12:41