2016-07-06 49 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     

}