简体   繁体   English

如何关闭jframe面板或窗口

[英]How to close jframe panel or window

I have 2 frames/windows, I have Exit button on window 2, an from window 1 I launch window 2 and then exit it ie setVisible(false); 我有2个框架/窗口,在窗口2上有“退出”按钮,从窗口1开始,我启动窗口2,然后退出它,即setVisible(false);

When I execute window 2 I can easily click button exit and hide the current window, however when I launch window 2 from window 1, and then click exit button I get NullPointerException Error. 当我执行窗口2时,我可以轻松地单击按钮退出并隐藏当前窗口,但是当我从窗口1中启动窗口2时,然后单击退出按钮,我会收到NullPointerException错误。 then I instantiated it in the beginning with static and this error was gone, however the window 2 is not being closed/hidden its still there with no effect of button. 然后我以static实例化了它,并且这个错误消失了,但是窗口2没有关闭/隐藏,它仍然没有按钮的作用。

Window 1 code: 窗口1的代码:

package com.my.jlms;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LibrarianMenu extends JFrame {

private JPanel contentPane;
private static LibrarianMenu frame;


/**
 * Launch the application.
 */
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();
            }
        }
    });
}

/**
 * Create the frame.
 */
public LibrarianMenu() {
    setTitle("Librarian");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 385, 230);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnPasswd = new JButton("Change Pass");
    btnPasswd.setBounds(202, 76, 146, 39);
    contentPane.add(btnPasswd);


    btnPasswd.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ChangePwd framee = new ChangePwd();
                    framee.setVisible(true);
                }
            });

}

}

Window 2 Code: 窗口2代码:

package com.my.jlms;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JButton;

public class ChangePwd extends JFrame {

private JPanel contentPane;
private static ChangePwd frame = new ChangePwd();;

private JButton btnExit;

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

/**
 * Create the frame.
 */
public ChangePwd() {
    setResizable(false);
    setTitle("Password!");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 266, 154);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    btnExit = new JButton("Exit");
    btnExit.setBounds(20, 80, 89, 30);
    contentPane.add(btnExit);


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

} }

Is there a solution I can set window 2 to hide ? 我可以将窗口2设置为隐藏吗?

The problem here is that you are creating your frame, as your class, and not on the object frame , but you hide the frame which represents the object frame . 这里的问题是,您正在创建框架(作为类),而不是在对象frame ,但是隐藏了代表对象frame

Change this line (in your actionListener's actionPerformed() method): 更改此行(在您的actionListener的actionPerformed()方法中):

frame.setVisible(false);

to: 至:

setVisible(false);

You can use dispose function for the purpose. 您可以为此使用处理功能。 see how dispose works. 看看如何处置。

If you want to close a JFrame, you could use the dispose() method. 如果要关闭JFrame,则可以使用dispose()方法。

Example: 例:

public void actionPerformer(ActionEvent e)
{ 
  if(e.getSource().equals(closeFrameButton)
     {
        dispose();  //This will close the current JFrame
      }
}

NOTE: this is different to System.exit(0); 注意:这不同于System.exit(0); . Using this will close the Java virtual machine. 使用此方法将关闭Java虚拟机。 if you just want to close the frame, use dispose() 如果您只想关闭框架,请使用dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM