通过单击内部 JPanel 来处理 JFrame [英] Disposing JFrame by clicking from an inner JPanel
本文介绍了通过单击内部 JPanel 来处理 JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过单击一个按钮来处理我的 JFrame,该按钮位于一个 JPanel 上,该 JPanel 位于我想要关闭的 JFrame 上.
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.
我试图在 JFrame 类上创建一个静态方法,但是我的 IDE 告诉我这不会发生.
I tried to make a static method on the JFrame class, but ofcourse my IDE told me that wasn't going to happen.
有人想解决办法吗?
谢谢!
推荐答案
试试这个:
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);
}
});
}
}
这篇关于通过单击内部 JPanel 来处理 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文