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, "你确定要退出吗?", "消息提示",JOptionPane.YES_NO_OPTION);
//取消选择是 -1 ,确定是0 ,取消是1
System.out.println(n);
if(n==0) { //如果选择了确定
System.exit(0);//那么退出
}
}
}
java窗口关闭事件 。。。。this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
这句话其实是1.4以后才有的,它代替了以前的WindowListener中的windoClose方法,EXIT_ON_CLOSE的意思是,当前窗口在点击关闭的时候,会关闭主线程,OK?所以你的所有应用都被关闭了
那么.你就不要这么设置,你的窗口在设置的时候写成,DISPOSE_ON_CLOSE就可以了.DIPOSE就是仅仅释放窗体资源,而不会关闭主线程.
这个DISPOSE_ON_CLOSE静态字段的设置,代替你在windoClose写"this.dispose()"
JAVA关于窗体JFrame的关闭事件//设置关闭后直接退出
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
package Login;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
private JLabel jLabel_School_logo;
private JLabel jLabel_user_name;
private JLabel jLabel_passwd;
private JButton jButton_reg;
private JButton jButton_login;
private JPasswordField jPasswordField;
private JTextField jTextField_user_name;
private JLabel jLabel_title;
private int student_statue=0;
private JComboBox jComboBox1;
private int manager_statue=0;
/**
*
*/
/**
*
*/
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setTitle("教室管理系统登录");
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jLabel_School_logo = new JLabel();
推荐阅读
- 儿童益智视频亲子游戏中班的简单介绍
- mysql多个用户取一条记录独占,mysql 一个用户管理多个数据库
- 苹果电脑虚拟机xp,苹果电脑虚拟机下载
- chatgpt什么账号可以用,ChatGPt账号
- go语言string分割 go string split
- 包含蛙小侠公众号代运营的词条
- 宁波网站制作公司,宁波网站制作公司排名
- java源代码文件后缀 java文件后缀是
- 简要描述hbase架构,简述hbase存储架构