2016-07-06 48 views
-1

所以,我的第一篇文章。帧关闭命令_variable

我学习Java。我遇到了一个问题。我想在尝试关闭窗口0帧时打开pop.up窗口。

所以,我插入这个。来自教师的示例代码。 检查出来,请 这里有云

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); 
      } 
    } 

它什么都不做。没有弹出窗口,给我什么要求

+2

你确保函数被调用?找到这个的简单方法是在函数的开头放置一个System.out.println。 –

+1

您预计何时会调用此方法?为了更快地获得更好的帮助,请发布[MCVE](http://stackoverflow.com/help/mcve) – copeg

+0

重复? :[Java Swing为EXIT_ON_CLOSE添加Action侦听器](http://stackoverflow.com/questions/16295942/java-swing-adding-action-listener-for-exit-on-close) –

回答

1

你的方法很可能不会被称为。您需要使用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) {} 

    }); 

这将调用你的关闭方法,当你的程序请求退出。

+0

我真的不知道在哪里添加这个,我试过但没有成功。对缺乏知识感到抱歉。我可能会再试一次,如果不在这里确认,它已经工作了,请帮助我。 – PeteriO

+0

在创建JFrame后添加'setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)'。在那之后添加'addWindowListener(new WindowListener(){...});'。 (可能在一个构造函数中,你有这样一行:'mainFrame = new JFrame(“Title”);/*添加代码* /'。 – Shiro

0

我使用NetBeans平台。 所以我的代码如下:

package my.exercise21; 

进口javax.swing.JOptionPane中;

/** * * @author管理员 */ 公共类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     

}

0

我想我的问题就出在不调用的JOptionPane库

0

我刚刚解决了这个问题。感谢您的参与和您的善意。

/* 
* 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     

}