2012-05-13 14 views
2

ボタンをクリックした後に別のスイングGUIを開くと、ここにボタンがあるメインGUIが表示されません。別のスイングGUIでボタンをクリックした後に別のスイングGUIを開く

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package javaapplication1; 
import static java.lang.System.out; 
/** 
* 
* @author Matthew 
*/ 
public class NewFrame extends java.awt.Frame { 
/** 
* Creates new form NewFrame 
*/ 
public NewFrame() { 
    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. 
*/ 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    jButton1 = new javax.swing.JButton(); 
    jButton2 = new javax.swing.JButton(); 

    jButton1.setText("jButton1"); 

    addWindowListener(new java.awt.event.WindowAdapter() { 
     public void windowClosing(java.awt.event.WindowEvent evt) { 
      exitForm(evt); 
     } 
    }); 

    jButton2.setText("jButton2"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 
    jButton2.addKeyListener(new java.awt.event.KeyAdapter() { 
     public void keyPressed(java.awt.event.KeyEvent evt) { 
      form(evt); 
     } 
    }); 
    add(jButton2, java.awt.BorderLayout.CENTER); 

    pack(); 
}// </editor-fold> 

/** 
* Exit the Application 
*/ 
private void exitForm(java.awt.event.WindowEvent evt) {       
    System.exit(0); 
}       

private void form(java.awt.event.KeyEvent evt) { 

} 

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
    out.println("print something"); 
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      new NewFrame().setVisible(true); 
     } 

    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
// End of variables declaration 
} 

と、ここで私は私がボタンをクリックしたときに開くダイアログGUIたいファイル:あなたがに親ウィンドウに渡し、それを必要とするとき

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package javaapplication1; 

/** 
* 
* @author Matthew 
*/ 
public class NewJDialog extends javax.swing.JDialog { 
/** 
* Creates new form NewJDialog 
*/ 
public NewJDialog(java.awt.Frame parent, boolean modal) { 
    super(parent, modal); 
    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() { 

    jLabel1 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

    jLabel1.setText("IT WORKS"); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addContainerGap(72, Short.MAX_VALUE) 
      .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap()) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(128, 128, 128) 
      .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(97, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold> 

/** 
* @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(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* 
    * Create and display the dialog 
    */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     public void run() { 
      NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true); 
      dialog.addWindowListener(new java.awt.event.WindowAdapter() { 

       @Override 
       public void windowClosing(java.awt.event.WindowEvent e) { 
        System.exit(0); 
       } 
      }); 
      dialog.setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JLabel jLabel1; 
// End of variables declaration 
} 

答えて

2

一つの方法は、NewJDialogを作成することですがそのコンストラクタを開き、setVisible(true)で可視に設定します。しかし、元のウィンドウを古いAWTライブラリの一部であるFrameにするのではなく、Swingライブラリの同等のクラスであるJFrameにする必要があります。

このアドバイスについてご不明な点がありましたら、お気軽にお問い合わせください。

関連する問題