java AWT组件出来的窗口关不掉 怎么回事 能运行 点击右上角的X就是关不掉
代码如下importjava.awt.*;publicclassFrameExextendsFrame{FrameEx(){tihis.setTitle("标题");th...
代码如下
import java.awt.*;
public class FrameEx extends Frame{
FrameEx(){
tihis.setTitle("标题");
this.setBounds(100,100,250,250);
this.setVisible(true);
}
public static void main(String [] args){
new FrameEx();
}
} 展开
import java.awt.*;
public class FrameEx extends Frame{
FrameEx(){
tihis.setTitle("标题");
this.setBounds(100,100,250,250);
this.setVisible(true);
}
public static void main(String [] args){
new FrameEx();
}
} 展开
5个回答
展开全部
你确定你的代码能运行?错误也太多了吧?
帮你改了一下,目测是因为你继承自Frame但没有设置关闭的监听器
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test extends Frame{
private static final long serialVersionUID = 1L;
public Test(){
this.setTitle("标题");
this.setBounds(100,100,250,250);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String [] args){
new Test();
}
}
帮你改了一下,目测是因为你继承自Frame但没有设置关闭的监听器
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test extends Frame{
private static final long serialVersionUID = 1L;
public Test(){
this.setTitle("标题");
this.setBounds(100,100,250,250);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String [] args){
new Test();
}
}
展开全部
添加一个监听器,重写windowClosing方法。才能关闭窗口。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Frame的api文档里面
Frames are capable of generating the following types of WindowEvents:
WINDOW_CLOSING:
If the program doesn't explicitly hide or dispose the window while processing this event, the window close operation is canceled.
你可以在构建函数里面添加个监听,如下:
this.addWindowListener(new WindowListener(){
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
});
Frames are capable of generating the following types of WindowEvents:
WINDOW_CLOSING:
If the program doesn't explicitly hide or dispose the window while processing this event, the window close operation is canceled.
你可以在构建函数里面添加个监听,如下:
this.addWindowListener(new WindowListener(){
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用Swing包就可以啊,awt包就好要写个监听方法滴
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
setVisible(true); //设置是否可见
setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗体
setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗体
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询