java java中的面板退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5707603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:24:32 来源:igfitidea点击:
panel exit in java
提问by user713744
Frame fr;
...
fr.setDefaultCloseOperation(Frame.???)
What should I write instead of "???" to close the frame?
我应该写什么而不是“???” 关闭框架?
回答by Bart Kiers
I think you meant to use JFrame
:
我认为你打算使用JFrame
:
JFrame fr;
...
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And with a (old) AWT Frame, you'd do something like this to close it:
对于(旧的)AWT 框架,您可以执行以下操作来关闭它:
final Frame frame = new Frame("Frame test");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
frame.setVisible(true);
回答by Vik Gamov
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
There is code example
有代码示例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
JFrame frame;
public Test() {
JButton button = new JButton("exit");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
JPanel panel = new JPanel();
panel.add(button);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocation(200,200);
frame.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}