通过从内部 JPanel 中单击来处置 JFrame

发布于 2024-08-23 04:56:53 字数 143 浏览 2 评论 0原文

我试图通过单击位于 JPanel 上的按钮来处置我的 JFrame,该按钮位于我想要关闭的 JFrame 上。

我尝试在 JFrame 类上创建一个静态方法,但是我的 IDE 告诉我这不会发生。

有人想到解决办法吗?

谢谢!

I'm trying to dispose my JFrame by clicking a button, located on a JPanel that is placed on the JFrame that I want to close.

I tried to make a static method on the JFrame class, but ofcourse my IDE told me that wasn't going to happen.

Anyone thinking of a solution?

Thanks!

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

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

发布评论

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

评论(2

孤芳又自赏 2024-08-30 04:56:54

试试这个:

public class DisposeJFrame extends JFrame{
    JPanel panel = new JPanel();
    JButton button = new JButton("Dispose JFrame");

    public DisposeJFrame(){
        super();
        setTitle("Hi");
        panel.add(button);
        add(panel);
        pack();

        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });
    }

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                DisposeJFrame jf = new DisposeJFrame();
                    jf.setVisible(true);
            }
        });
    }
}

Try this:

public class DisposeJFrame extends JFrame{
    JPanel panel = new JPanel();
    JButton button = new JButton("Dispose JFrame");

    public DisposeJFrame(){
        super();
        setTitle("Hi");
        panel.add(button);
        add(panel);
        pack();

        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });
    }

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                DisposeJFrame jf = new DisposeJFrame();
                    jf.setVisible(true);
            }
        });
    }
}
我很OK 2024-08-30 04:56:54

做这样的事情:

JButton closeFrameButton = new JButton("Close");
closeFrameButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        ((Window) getRootPane().getParent()).dispose();
    }
});

Do something like this:

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