单击鼠标关闭 JFrame
嘿伙计们,我希望我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想知道您添加了 MouseListener 的 JFrame contentPane 是否与正在显示的 JFrame 不同,特别是因为我看到您在 createFrame 方法中创建了一个新的 JFrame。您知道 MouseListener 只有添加到可视化组件中才会起作用,对吧?
建议:
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:
这就是我所做的。
Here's what I did.