2016-11-05 4 views
0
durch eine Klasse mit dem Klick auf eine Schaltfläche erweitert wird
import javax.swing.JFrame; 

public class Choose extends javax.swing.JFrame { 

/** 
* Creates new form NewJFrame 
*/ 
public Choose() { 
    initComponents(); 
} 


@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    LoginButton = new javax.swing.JButton(); 
    Create = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setTitle("Family Entertainment Center"); 

    LoginButton.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N 
    LoginButton.setText("Login"); 
    LoginButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      LoginButtonActionPerformed(evt); 
     } 
    }); 

    Create.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N 
    Create.setText("Create Account"); 
    Create.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      CreateActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(34, 34, 34) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(Create, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(LoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addContainerGap(39, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addContainerGap(68, Short.MAX_VALUE) 
      .addComponent(Create, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(49, 49, 49) 
      .addComponent(LoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(79, 79, 79)) 
    ); 

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

public static void CreateActionPerformed(java.awt.event.ActionEvent evt) {           
    JFrame frame = new CreateLogin(); 
    frame.setVisible(true); 
}           

public static void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    JFrame frame = new LoginFrame(); 
    frame.setVisible(true); 
} 

Im Aktions Ereignis-Listener oben ich diese JFrame ausblenden möchten, während es die anderen jFrames schafft. Ich habe versucht, das Jframe zu instanziieren und zu verstecken, aber das erstellt eine neue Instanz. Ich habe auch das "this.setVisible (false)" versucht, das auch nicht funktioniert.Java verstecken JFrame, die

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Metal 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 ("Metal".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(Choose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Choose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Choose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Choose.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 Choose().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
public static javax.swing.JButton LoginButton; 
public static javax.swing.JButton Create; 
// End of variables declaration     

}

+1

1) Siehe [Die Verwendung mehrerer JFrames, gute/schlechte Praxis?] (Http://stackoverflow.com/q/9554636/418556) 2) Siehe [Erkennung/Behebung für die hängende, nahe Klammer eines Codeblocks ] (http://meta.stackexchange.com/q/251795/155831) für ein Problem, das ich nicht länger stören könnte. 3) Bitte lerne die übliche Java-Nomenklatur (Benennungskonventionen - z. B. 'EachWordUpperCaseClass',' firstWordLowerCaseMethod() ',' firstWordLowerCaseAttribute', es sei denn, es ist ein 'UPPER_CASE_CONSTANT') und benutze es konsistent. –

Antwort

1

Nicht sicher, ob dies die beste Lösung ist, aber wenn man nicht need them static tun, sollte es funktioniert das static Feld von Ihren CreateActionPerformed() und LoginButtonActionPerformed() Methoden zu entfernen. Sie können dann setVisible(false) anrufen, weil Sie jetzt innerhalb Ihrer Choose Instanz anrufen.

public void CreateActionPerformed(java.awt.event.ActionEvent evt) { 
    JFrame frame = new CreateLogin(); 
    frame.setVisible(true); 
    setVisible(false); 
} 

sollten Sie in der Lage sein static von Ihrem JButtons als auch zu entfernen.

Hoffe, das hilft. :)

Bearbeiten: Siehe Kommentar von Andrew.