Closing JFrame with Button

ⅰ亾dé卋堺 提交于 2019-12-12 22:24:55

问题


I am currently using Eclipse's drag and Drop feature, I have one application window which comes with JFrame by default and is able to setVisible(false); but my other frames/panel/window I have created with JPanel and with extending JFrame.

Because of extend I am unable to setVisible(false or true); it has no effect at all on the window it still remains true.

My code :

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                LibrarianMenu frame = new LibrarianMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public LibrarianMenu() {
    setTitle("Librarian");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 385, 230);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    .
    .
    . so on

Here I am trying to execute my button:

 btnLogout.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    LibrarianMenu frame = new LibrarianMenu();
                    frame.setVisible(false);
                }
            });

Any solutions for it?


回答1:


This is happening because every time you press the button you create a new instance of that frame. Here is your code updated :

static LibrarianMenu frame ;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new LibrarianMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

And the logout button event should be like this :

btnLogout.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
            }
        });



回答2:


Because you're creating the frame inside the Runnable, its scope is limited to that of the runnable. Try declaring the variable outside of the runnable, then initializing it within the runnable, like so:

private JPanel contentPane;
private LibrarianMenu frame;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new LibrarianMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Then setvisible to false without declaring a new instance of LibrarianMenu:

btnLogout.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
           frame.setVisible(false);
     }
});


来源:https://stackoverflow.com/questions/36739800/closing-jframe-with-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!