我想点击按键后关闭这个界面,该怎么做
解决方案 »
- java创建网络file
- 谁有jdk-6u4-windows-i586-p ,给我下载地址或者传给我
- 为何执行“java version”总报告java.lang.noclassdeffounderror错误?
- 菜鸟问题!!!关于键盘事件的疑问
- 一段C# 的程序,请各位高手看下,把对应的java程序写下,谢谢了
- 【JAVA】如何判断文件是否可以访问?
- SWT Designer 竟然可以实现这样的半透明效果?百思不得其解!
- 难道CSDN上就没有人能回答这个问题吗?
- 有没有人能简要的讲讲canvas
- 关于 new一个java子类需先实例化父类的思考
- 数组中如何判断默认为null的字符串
- 如何实现点击窗口其它部位使JTextField变成JLabel
这个方法可以
那就setvisiable(false)吧
setVisible(false);只是隐藏
Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be ed as undisplayable.
The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed 还是dispose()好 以前没注意就一直用的setVisible(false);
虽然视觉效果是一样的 但没有释放资源 不太好
public void windowClosing(WindowEvent e)
{
System.exit(-1);
}
正解!如果说使用了setVisible(),那么你就仅仅是把界面给隐藏了,但是依旧使用你的CPU
当你使用的是dispose()时,它会释放内存。两者的差距还是很大的
System.exit(-1);/**非正常退出*/
setVisible()在frame当中也有出现过
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class T { public static void main(String[] args) {
final Frame frame1 = new Frame("1");
final Frame frame2 = new Frame("2");
Frame frame3 = new Frame("3");
frame1.setBounds(0, 0, 200, 200);
frame2.setBounds(200, 0, 200, 200);
frame3.setBounds(400, 0, 200, 200);
frame1.setVisible(true);
frame2.setVisible(true);
frame3.setVisible(true);
frame1.addWindowListener(new WindowAdapter() {
@Override
// 关闭当前窗口。
public void windowClosing(WindowEvent e) {
frame1.dispose();
}
});
frame2.addWindowListener(new WindowAdapter() {
@Override
// 隐藏当前窗口。
public void windowClosing(WindowEvent e) {
frame2.setVisible(false);
}
});
frame3.addWindowListener(new WindowAdapter() {
@Override
// 系统退出。
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}}