java中关闭窗体的代码 java关闭窗口的语句

java关闭窗体的六种方法 前段时间集中精力写java中关闭窗体的代码了两篇论文 很久没写博文java中关闭窗体的代码了 现在继续了
使用JFramejava中关闭窗体的代码的enableEvents和processWindowEvent
//Frame java
import java awt *;
import java awt event *;
【java中关闭窗体的代码 java关闭窗口的语句】 import javax swing *;
public class Frame extends JFrame {
public Frame () {
enableEvents(AWTEvent WINDOW_EVENT_MASK);
this setSize(new Dimension( ));
this setTitle( Frame );
}
protected void processWindowEvent(WindowEvent e) {
super processWindowEvent(e);
if (e getID() == WindowEvent WINDOW_CLOSING) {
System exit( );
}
}
}
直接实现WindowListener接口
//Frame java
import java awt *;
import java awt event *;
public class Frame extends Frame implements WindowListener {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(this);
}
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
public void windowOpened(WindowEvent windowEvent) {}
public void windowClosed(WindowEvent windowEvent) {}
public void windowIconified(WindowEvent windowEvent) {}
public void windowDeiconified(WindowEvent windowEvent) {}
public void windowActivated(WindowEvent windowEvent) {}
public void windowDeactivated(WindowEvent windowEvent) {}
}
直接继承窗体适配器WindowAdapter
//Frame java
import java awt *;
import java awt event *;
public class Frame extendsWindowAdapter {
public Frame () {
Frame f=new Frame();
f setSize(new Dimension( ));
f setTitle( Frame );
f addWindowListener(this);
f setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
}
间接继承窗体适配器WindowAdapter
//Frame java
import java awt *;
import java awt event *;
public class Frame extendsFrame {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(new winAdapter());
this setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
class winAdapter extends WindowAdapter{
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
}
间接实现WindowListener接口
//Frame java
import java awt *;
import java awt event *;
public class Frame extendsFrame {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(new winEventHandle());
this setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
class winEventHandle implements WindowListener {
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
public void windowOpened(WindowEvent windowEvent) {}
public void windowClosed(WindowEvent windowEvent) {}
public void windowIconified(WindowEvent windowEvent) {}
public void windowDeiconified(WindowEvent windowEvent) {}
public void windowActivated(WindowEvent windowEvent) {}
public void windowDeactivated(WindowEvent windowEvent) {}
}
使用Inner Class
//Frame java
import java awt *;
import java awt event *;
public class Frame {
public Frame (){
Frame f=new Frame();
f addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System exit( );
}
});
f setSize(new Dimension( ));
f setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
Jframe的关闭方法
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame的关闭方法如下
this addWindowListener(new java awt event WindowAdapter() {
public void windowClosing(java awt event WindowEvent e) {
System exit( );
}
lishixinzhi/Article/program/Java/hx/201311/27073
java程序关闭窗口代码import java.applet.*;
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JFrame;
import java.awt.event.*;
public class FirstFrame extends Frame {
public static void main(String args[]) {
FirstFrame fr = new FirstFrame("First contianer!");
fr.setSize(240, 240);
//继承JFrame的关闭窗口代码
//fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//继承Frame的
fr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);//退出系统
}
});
fr.setVisible(true);
}
public FirstFrame(String str) {
super(str);
}
}
Java jframe中如何实现窗口的关闭效果图
参考代码和注释如下
import java.awt.event.*;
import javax.swing.*;
public class DemoFrame extends JFrame{
JButton jbExit;
public DemoFrame() {
jbExit = new JButton("退出");
//当点击退出 按钮时候java中关闭窗体的代码的响应器
jbExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doExit();//退出时候java中关闭窗体的代码的方法
}
});
JPanel jp = new JPanel();
jp.add(jbExit);
add(jp);
setTitle("窗口");// 窗口标题
setSize(380, 185);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//通常添加这行代码,点击窗口右下角java中关闭窗体的代码的关闭时会结束程序
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//右下角的关闭,不主动采取任何行动
//当点击窗口右上角的关闭按钮时候的响应器
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
doExit();
}
});
}
// main方法
public static void main(String[] args) {
new DemoFrame().setVisible(true);
}
//退出时候的选择
private void doExit() {
int n = JOptionPane.showConfirmDialog(null, "java中关闭窗体的代码你确定要退出吗?", "消息提示",JOptionPane.YES_NO_OPTION);
//取消选择是 -1 ,确定是0 ,取消是1
System.out.println(n);
if(n==0) { //如果选择java中关闭窗体的代码了确定
System.exit(0);//那么退出
}
}
}
JAVA中怎么关闭一个窗口java关闭窗口可以使用dispose(),只是该窗体在内存中所占有的资源得到了释放,而整个程序并没有因此而退出,如果整个程序要退出,在以java.awt中的frame为控件时,需手动添加:System.exit();
而在以javax.Swing中的jFrame为控件时,一般不需要再写相应的事件,默认点击窗体上的小叉时,是隐藏 , 当然你可更改其参数 。
java中关闭窗体的代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java关闭窗口的语句、java中关闭窗体的代码的信息别忘了在本站进行查找喔 。

    推荐阅读