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

发布于 2024-11-03 08:42:22 字数 1753 浏览 0 评论 0原文

有两个框架,当您单击第一个框架上的按钮时,它会打开第二个框架。在第二帧上,我试图创建一个按钮,按下该按钮会关闭 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();
                            ^

注意:我删除了与以下内容无关的位或不相关的代码 :问题。)

提前感谢任何人可以给我的帮助。

There are two frames and when you click on a button on the first frame it opens the second frame. On the second frame I'm trying to create a button which when pressed closes the JFrame without terminating the program but I'm having no luck. This is the code that I'm trying to use for the second frame, which without the button compiles fine:

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();
                    }
            }

}

While compiling I get the following error message:

cannot find symbol
symbol  : method dispose()
location: class ExitButtonListener
                            dispose();
                            ^

(Note: I've removed bits or irrelevant code that has nothing to do with the question.)

Thanks in advance for help anybody can give me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

饮湿 2024-11-10 08:42:22

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

If you want to dispose the Time_First_Depot frame, then you have to call dispose() on the actual active instance of that class; so the event handler needs access to that instance. There are all kinds of ways you could do this. One way: pass "this" to the ExitButtonListener's constructor; have the constructor accept a JFrame argument and store it in a member variable, then call dispose() through that member.

情深如许 2024-11-10 08:42:22

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();
        }
    }
}

The method dispose is a method of your JFrame. Therefore I suggest to pass over a reference to your 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();
        }
    }
}
以酷 2024-11-10 08:42:22

有两个框架,当您单击第一个框架上的按钮时,它将打开第二个框架。在第二帧上,我尝试创建一个按钮,按下该按钮会关闭 JFrame 而不会终止..

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

There are two frames and when you click on a button on the first frame it opens the second frame. On the second frame I'm trying to create a button which when pressed closes the JFrame without terminating ..

If you can swap the 2nd JFrame for a JOptionPane (or modal JDialog) most of the problems will be solved.

屌丝范 2024-11-10 08:42:22

您应该考虑触发 DefaultCloseOperation ,它只会触发与按下 JFrame 右上角的红色 x 相同的过程。

然后,处理将由 JFrame 本身或者更确切地说是已经指定的来完成:

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

您可以通过多种方式实现这一点,如此问题的答案中所述。

可能性一:您将 WindowEvent 排入队列,从而伪造 CloseOperation:

public void actionPerformed (Event e) {
    WindowEvent wev = new WindowEvent(
         SwingUtilities.windowForComponent((Component) e.getSource()),
         WindowEvent.WINDOW_CLOSING
    );
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}

可能性二:您处置 Window,而不将事件排入队列,但因此跳过您指定的所有 EventListener:

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

You should consider triggering the DefaultCloseOperation that would do nothing else than trigger the same process as if you were pressign the red x in the top right corner of your JFrame.

The disposal would then be done by the JFrame itself or rather the already specified:

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

You can accomplish that, as outlined in this question's answers in multiple ways.

Possiblity one: You enqueue a WindowEvent, thereby faking a CloseOperation:

public void actionPerformed (Event e) {
    WindowEvent wev = new WindowEvent(
         SwingUtilities.windowForComponent((Component) e.getSource()),
         WindowEvent.WINDOW_CLOSING
    );
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}

Possibility two: You dispose of the Window, without enqueueing an event, but thereby skip all EventListeners you specified:

public void actionPerformed (Event e) {
     JFrame rootWindow = ((JFrame) SwingUtilities.getRoot((Component) e.getSource()));
     rootWindow.dispose();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文