2016-06-16 10 views
0

最初のjpanelでユーザーの入力があり、2番目のjpanelに渡したいとします。ボタンをクリックした後、新しいJpanelを開くにはどうすればいいですか? Disposeは最初のjpanelで動作していますが、2番目のjpanelは表示されていません。Javaの新しいJPanelが表示されない

まずJPanelの

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {            
    String name =txtName.getText(); 
    String address= txtAddress.getText(); 
    int port= Integer.parseInt(txtPort.getText()); 
    login(name,address, port); 



}            

private void login(String name , String address, int port){ 
    dispose(); 
    new Client(name,address,port); 

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

ログイン機能

new Client(name,address,port).setVisible(true); 

で第二のJPanel

public class Client extends javax.swing.JFrame { 

/** 
* Creates new form Client 
*/ 

private String name,address; 
private int port; 

public Client() 
{ 
    initComponents(); 
} 

public Client(String name,String address, int port) { 
    this.name=name; 
    this.address=address; 
    this.port=port; 


} 
    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(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Client.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 Client().setVisible(true); 
     } 
    }); 
} 

答えて

0

public Client(String name,String address, int port) { 
    this.name=name; 
    this.address=address; 
    this.port=port; 
    initcomponents(); 

} 
を次のようにクライアントのコンストラクタを更新します。
+0

ありがとうございます。JPanelを開きます。しかし、それは2番目のjパネルを表示していません。空白のjPanelのみが表示されます。 – hiranya

+0

ありがとうございます。できます。 – hiranya

関連する問題