Java 帧关闭命令\u变量
所以,我的第一篇文章 我学习Java。我有个问题。我想在尝试关闭窗口0帧时打开一个弹出窗口 所以,我插入这个。来自教师的示例代码。 请检查一下 来了Java 帧关闭命令\u变量,java,Java,所以,我的第一篇文章 我学习Java。我有个问题。我想在尝试关闭窗口0帧时打开一个弹出窗口 所以,我插入这个。来自教师的示例代码。 请检查一下 来了 private void formWindowClosing(java.awt.event.WindowEvent evt) { int reply = JOptionPane.showConfirmDialog(this, "Really close?"
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int reply = JOptionPane.showConfirmDialog(this, "Really close?", "Close?", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
this.dispose();
if (reply == JOptionPane.NO_OPTION)
this.setVisible(true);
}
}
它什么也不做。没有弹出窗口提供所需内容您的方法很可能永远不会被调用。您需要使用WindowListener。下面是如何设置它的:
JFrame mainFrame = new JFrame(); //that's your frame
mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) //enable windowlistener handling
mainFrame.addWindowListener(new WindowListener() { //you need to add a windowlistener
@Override
public void windowClosing(WindowEvent e) {
formWindowClosing(e); //call your method
}
@Override
public void windowOpened(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowActivated(WindowEvent e) {}
@Override
public void windowDeactivated(WindowEvent e) {}
});
当程序请求退出时,这将调用关闭方法。我使用NetBeans平台。 因此,我的代码如下:
package my.exercise21;
导入javax.swing.JOptionPane
/**
*
*@作者管理员
*/
公共类Exercise21扩展了javax.swing.JFrame{
/**
* Creates new form Exercise21
*/
public Exercise21() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
jMenu1.setText("File");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("Exit");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
// TODO add your handling code here:
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int reply = JOptionPane.showConfirmDialog(this, "Really close?", "Close?", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
this.dispose();
if (reply == JOptionPane.NO_OPTION)
this.setVisible(true);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Exercise21.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new Exercise21().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
// End of variables declaration
/**
*创建新表单练习21
*/
公众锻炼21(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jMenuBar1=newjavax.swing.JMenuBar();
jMenu1=newjavax.swing.JMenu();
jMenuItem1=newjavax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DO\u NOTHING\u ON\u CLOSE);
setText(“文件”);
setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X,java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText(“退出”);
jMenuItem1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jMenuItem1执行的操作(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,400,短。最大值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,279,短。最大值)
);
包装();
}//
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt){
处置();
//TODO在此处添加您的处理代码:
}
私有void formWindowClosing(java.awt.event.WindowEvent evt){
int reply=JOptionPane.showConfirmDialog(此选项为“真的关闭?”,“关闭?”,JOptionPane.YES\u NO\u选项);
if(reply==JOptionPane.YES\u选项){
这个。dispose();
if(reply==JOptionPane.NO_选项)
此.setVisible(true);
}
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}catch(ClassNotFoundException |实例化Exception | IllegalacessException | javax.swing.UnsupportedLookAndFeelException ex){
getLogger(Exercise21.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
//
/*创建并显示表单*/
java.awt.EventQueue.invokeLater(()->{
新的Exercise21().setVisible(true);
});
}
//变量声明-不修改
私有javax.swing.JMenu jMenu1;
私有javax.swing.JMenuBar jMenuBar1;
私有javax.swing.JMenuItem jMenuItem1;
//变量结束声明
}我想我的问题在于没有调用JOptionPane库我刚刚解决了这个问题。感谢您的参与和您的灵感
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package my.exercise21;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("Exercise 2.1");
setCursor(new java.awt.Cursor(java.awt.Cursor.NW_RESIZE_CURSOR));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jMenu1.setText("File");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("Exit");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void formWindowClosed(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int r = JOptionPane.showConfirmDialog(this, "Really close?", "Close?", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.YES_OPTION) {
dispose(); // This closes the program
if (r == JOptionPane.YES_OPTION)
setVisible(false); // This just hides the window but program keeps running.
}// TODO add your handling code here:
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
dispose(); // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
// End of variables declaration
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包我的练习21;
导入javax.swing.JOptionPane;
/**
*
*@作者管理员
*/
公共类NewJFrame扩展了javax.swing.JFrame{
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jMenuBar1=newjavax.swing.JMenuBar();
jMenu1=newjavax.swing.JMenu();
jMenuItem1=newjavax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DO\u NOTHING\u ON\u CLOSE);
设定标题(“练习2.1”);
setCursor(新java.awt.Cursor(java.awt.Cursor.NW_RESIZE_Cursor));
addWindowListener(新java.awt.event.WindowAdapter(){
公共无效windowClosed(java.awt.event.WindowEvent evt){
窗体窗口关闭(evt);
}
公共无效窗口关闭(java.awt.event.WindowEvent evt){
窗体窗口关闭(evt);
}
});
setText(“文件”);
setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X,java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText(“退出”);
jMenuItem1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jMenuItem1ActionPer