Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swing 用按钮关闭JFrame而不终止程序_Swing_Jframe - Fatal编程技术网

Swing 用按钮关闭JFrame而不终止程序

Swing 用按钮关闭JFrame而不终止程序,swing,jframe,Swing,Jframe,有两个帧,当您单击第一帧上的按钮时,将打开第二帧。在第二帧中,我试图创建一个按钮,当按下该按钮时,关闭JFrame而不终止程序,但我没有运气。这是我尝试在第二帧中使用的代码,没有按钮可以很好地编译: class Time_First_Depot extends JFrame { Time_First_Depot() { Container c = getContentPane(); \\ creates content pane c.setL

有两个帧,当您单击第一帧上的按钮时,将打开第二帧。在第二帧中,我试图创建一个按钮,当按下该按钮时,关闭JFrame而不终止程序,但我没有运气。这是我尝试在第二帧中使用的代码,没有按钮可以很好地编译:

class Time_First_Depot extends JFrame
{   
    Time_First_Depot()
    {
        Container c = getContentPane(); \\ creates content pane
        c.setLayout ( null );

        Color b = new Color(100,200,255); \\ set colour of JFrame 
        c.setBackground( b );

        JButton exit = new JButton("EXIT"); \\creats button
        exit.addActionListener(new ExitButtonListener()); \\ adds listener to button

        exit.setForeground(Color.BLUE); \\edits buton
        exit.setFont(new Font("Time", Font.BOLD, 12));

        c.add(exit);\\adds button

        exit.setBounds(250, 375, 90, 30);\\ sets location of button

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450);  // set position and size
        this.setResizable(false);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        this.setTitle("Time");
        this.setVisible(true);
        this.setResizable(false);
    }
}

class ExitButtonListener implements ActionListener
{
    ExitButtonListener(){}
            public void actionPerformed(ActionEvent e)
            {
                    if (e.getActionCommand().equals("EXIT"))
                    {

                            dispose();
                    }
            }
}

编译时,我收到以下错误消息:

cannot find symbol
symbol  : method dispose()
location: class ExitButtonListener
                            dispose();
                            ^
(注意:我删除了与问题无关的位或无关代码。)


提前感谢任何人都能给我的帮助。

如果您想处理Time\u First\u Depot帧,那么您必须对该类的实际活动实例调用dispose();因此,事件处理程序需要访问该实例。有各种各样的方法可以做到这一点。单向:将“this”传递给ExitButtonListener的构造函数;让构造函数接受JFrame参数并将其存储在成员变量中,然后通过该成员调用dispose()。

方法
dispose
JFrame
的一种方法。因此,我建议将引用传递给您的
ActionListener

class ExitButtonListener implements ActionListener {
    private final JFrame frame;

    ExitButtonListener(JFrame frame) {
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("EXIT")) {
            frame.dispose();
        }
    }
}
有两个帧,当您单击第一帧上的按钮时,将打开第二帧。在第二帧中,我试图创建一个按钮,按下该按钮时,JFrame将关闭而不终止


如果您可以将第二个
JFrame
交换为
JOptionPane
(或模态
JDialog
)大多数问题都会得到解决。

你应该考虑触发<代码> Debug关闭操作> /代码>,除了触发你在JFrAMP的右上角中的红色X之外,没有别的方法可以触发相同的进程。

然后,处理将由JFrame本身或已经指定的:

您可以通过多种方式实现这一点,如中所述

可能性一:您将WindowEvent排队,从而假装关闭操作:

public void actionPerformed (Event e) {
    WindowEvent wev = new WindowEvent(
         SwingUtilities.windowForComponent((Component) e.getSource()),
         WindowEvent.WINDOW_CLOSING
    );
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
可能性二:在不将事件排队的情况下处理窗口,但因此跳过指定的所有EventListener:

public void actionPerformed (Event e) {
     JFrame rootWindow = ((JFrame) SwingUtilities.getRoot((Component) e.getSource()));
     rootWindow.dispose();
}

谢谢你的帮助,有了它我成功地做到了。只是需要有人以我能理解的方式向我解释。我想我会提出一个可能帮助其他人的解决方案:jbuttonexit=newjbutton(“exit”);addActionListener(新ActionListener(){public void actionPerformed(ActionEvent e){dispose();}});你的代码看起来很像霍华德答案中的代码。为什么不将其标记为“正确”?帧的传递是不必要的,可能会导致并发问题,具体取决于处理发生的位置。此外,您可以使用
e.getSource()
结合
SwingUtilities.getRoot()
c.setLayout(null)来获取帧(颤抖)预期GUI会出现问题,直到通过使用布局解决问题为止。
public void actionPerformed (Event e) {
     JFrame rootWindow = ((JFrame) SwingUtilities.getRoot((Component) e.getSource()));
     rootWindow.dispose();
}