单击鼠标关闭 JFrame

发布于 2024-11-07 09:44:23 字数 1708 浏览 3 评论 0原文

嘿伙计们,我希望我的 JFrame 在单击鼠标时关闭。我有代码可以做到这一点,但它不起作用...

public void closeFrame ()
{

    jPanel.removeAll ();
    String x = "";
    JLabel exit = new JLabel ("Click Anywhere To Close!", JLabel.CENTER);
    exit.setPreferredSize (new Dimension (700, 100));
    jPanel.add (exit);
    jPanel.revalidate ();

    MouseListener ml = new MouseAdapter ()
    {
        public void mouseClicked (MouseEvent event)
        {
            //Put JFrame close code here
            dispose ();
            System.exit (0);
        }
    }
    ;
    addMouseListener (ml);
    validate (); //This hasn't made a difference :(

}

该类扩展了 JFrame 并且它不实现 MouseListener。尽管如此,该代码不起作用。

这就是 JFrame 的初始化的样子(希望有帮助)...

public void createFrame ()
{
    new JFrame (":) :)");
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setVisible (true);
    //Some Initialization for jPanel variable
    getContentPane().add(jPanel);
    pack();
}

我怎样才能使这段代码工作&如果我不能,是否有其他方法可以通过鼠标单击关闭?

---编辑---

感谢 Hovercraft Full Of Eels

MouseListener 必须添加到 JPanel

//WORKING CODE
public void closeFrame ()
{

    jPanel.removeAll ();
    String x = "";
    JLabel exit = new JLabel ("Click Anywhere To Close!", JLabel.CENTER);
    exit.setPreferredSize (new Dimension (700, 100));
    jPanel.add (exit);
    //jPanel.revalidate ();

    MouseListener ml = new MouseAdapter ()
    {
        public void mouseClicked (MouseEvent event)
        {
            //Put JFrame close code here
            dispose ();
            System.exit (0);
        }
    }
    ;
    jPanel.addMouseListener (ml);
    jPanel.revalidate ();

}

Hey guys, I want my JFrame to close on a mouse click. I have code to do it, but it isn't working...

public void closeFrame ()
{

    jPanel.removeAll ();
    String x = "";
    JLabel exit = new JLabel ("Click Anywhere To Close!", JLabel.CENTER);
    exit.setPreferredSize (new Dimension (700, 100));
    jPanel.add (exit);
    jPanel.revalidate ();

    MouseListener ml = new MouseAdapter ()
    {
        public void mouseClicked (MouseEvent event)
        {
            //Put JFrame close code here
            dispose ();
            System.exit (0);
        }
    }
    ;
    addMouseListener (ml);
    validate (); //This hasn't made a difference :(

}

The class extends JFrame and it DOESNT implement MouseListener. Nonetheless, the code doesn't work.

This is what the initialization of the JFrame looks like (hope it helps)...

public void createFrame ()
{
    new JFrame (":) :)");
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setVisible (true);
    //Some Initialization for jPanel variable
    getContentPane().add(jPanel);
    pack();
}

How can I make this code work & if I can't, is there an alternate way of closing on mouse click?

---EDIT---

Thanks goes to Hovercraft Full Of Eels

The MouseListener must be added to the JPanel

//WORKING CODE
public void closeFrame ()
{

    jPanel.removeAll ();
    String x = "";
    JLabel exit = new JLabel ("Click Anywhere To Close!", JLabel.CENTER);
    exit.setPreferredSize (new Dimension (700, 100));
    jPanel.add (exit);
    //jPanel.revalidate ();

    MouseListener ml = new MouseAdapter ()
    {
        public void mouseClicked (MouseEvent event)
        {
            //Put JFrame close code here
            dispose ();
            System.exit (0);
        }
    }
    ;
    jPanel.addMouseListener (ml);
    jPanel.revalidate ();

}

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

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

发布评论

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

评论(2

南街九尾狐 2024-11-14 09:44:23

我想知道您添加了 MouseListener 的 JFrame contentPane 是否与正在显示的 JFrame 不同,特别是因为我看到您在 createFrame 方法中创建了一个新的 JFrame。您知道 MouseListener 只有添加到可视化组件中才会起作用,对吧?

建议:

  • 不要让您的类扩展 JFrame
  • 将 MouseListener 添加到您要添加到可视化 JFrame 的 contentPane 的 JPanel。
  • 为您的类提供一个 JFrame 变量,并让它引用您在 createFrame 方法中创建的 JFrame。然后您可以按照您认为合适的方式处置此 JFrame。
  • 不要忘记在可视化的 JFrame 上调用 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 。

I have to wonder if the JFrame contentPane that you've added the MouseListener to is not the same as the JFrame that's being displayed, especially since I see you creating a new JFrame in your createFrame method. You understand that the MouseListener will only work if added to a visualized component, right?

Suggestions:

  • don't have your class extend JFrame
  • Add your MouseListener to the JPanel that you're adding to the visualized JFrame's contentPane.
  • Give your class a JFrame variable and have it refer to the JFrame that you're creating in the createFrame method. Then you may dispose of this JFrame as you see fit.
  • Don't forget to call the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) on the visualized JFrame.
伴梦长久 2024-11-14 09:44:23

这就是我所做的。

btnClose = new JButton("Close");
btnClose.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
        dispose();
    }
});
btnClose.setBounds(253, 190, 75, 25);
contentPane.add(btnClose);
}

Here's what I did.

btnClose = new JButton("Close");
btnClose.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
        dispose();
    }
});
btnClose.setBounds(253, 190, 75, 25);
contentPane.add(btnClose);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文