2015-02-11 60 views
0

比方說,我有「登錄」和「註冊」框架,有上登錄一個按鈕叫「註冊」,其中規定的「登陸」的知名度爲false,並打開新的「註冊「架起至此一切都OK了(登錄框消失,註冊出現幀) 這是我對於聽衆:搖擺:關閉一個框架,並在同一時刻打開另一個

btnRegister.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       Register reg = new Register(that, connect); 
      } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     } 
    }); 

註冊的構造函數中它會變成登錄的知名度假,」那是登錄類(即= this)和連接是用於數據庫。 現在,當我嘗試關閉「註冊」框架並返回「登錄」後嘗試嘗試時,問題就出現了 - 註冊關閉,但登錄不出現。我試圖通過這兩個簡單的語句

login.setVisibility(true); // I have saved 'Login' frame into this login variable 
that.dispatchEvent(new WindowEvent(that,WindowEvent.WINDOW_CLOSING)); 

而不是僅僅改變可視性我試圖創造「登陸」的新實例做,但是,這並不正常工作。

login = new Login(); 
login.setVisible(true); 
that.dispatchEvent(new WindowEvent(that, WindowEvent.WINDOW_CLOSING)); 

像這樣的「登錄」框架出現像幾毫秒空白,沒有內部組件。

+2

參見[多個JFrames,好/壞習慣的用?(http://stackoverflow.com/q/9554636/418556)這聽起來像模態對話框的情況下.. – 2015-02-11 09:59:46

+0

我會建議使用'JPanel',而不是使用整個不同'JFrame's – Sarz 2015-02-11 10:26:46

+0

什麼有關Dispose()?對於下一幀當前幀和調用setVisible(真)? – Anptk 2015-02-11 11:17:06

回答

1

這就是我用JPanel可視屬性實現它的方式。

基本思路用戶登錄的

  1. 展架/寄存器組是適當的大小說 (200,400)狀的形式,
  2. 在裏面添加兩個JPanels(登錄和註冊),大小(200,400 ),並隱藏任何一個說註冊。
  3. 你的框架看起來只有一種形式。
  4. 當用戶單擊從登錄面板註冊按鈕,你必須做兩件事情

loginPanel.setVisible(假); and registerPanel.serVisible(true);

下面是完整的代碼。 (NetBeans的)-JFrameForm

public class UserFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form UserFrame 
    */ 
    public UserFrame() { 
     initComponents(); 
     jPanel2.setVisible(false); 
     this.setSize(200, 324); 
    } 

    /** 
    * 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() { 

     jPanel1 = new javax.swing.JPanel(); 
     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 
     jLabel1 = new javax.swing.JLabel(); 
     jPanel2 = new javax.swing.JPanel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jButton3 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setBackground(new java.awt.Color(255, 255, 255)); 

     jButton1.setText("Login"); 

     jButton2.setText("Register"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     jLabel1.setText("Login"); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(jLabel1) 
        .addComponent(jButton1)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jButton2) 
       .addContainerGap(38, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
       .addGap(42, 42, 42) 
       .addComponent(jLabel1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton1) 
        .addComponent(jButton2)) 
       .addContainerGap()) 
     ); 

     jLabel2.setText("Register"); 

     jButton3.setText("Done"); 
     jButton3.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton3ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(50, 50, 50) 
       .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(jButton3) 
        .addComponent(jLabel2)) 
       .addContainerGap(103, Short.MAX_VALUE)) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(55, 55, 55) 
       .addComponent(jLabel2) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 221, Short.MAX_VALUE) 
       .addComponent(jButton3) 
       .addContainerGap()) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 

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

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {           
     jPanel1.setVisible(true); 
     jPanel2.setVisible(false); 
    }           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
     jPanel1.setVisible(false); 
     jPanel2.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 ex) { 
      java.util.logging.Logger.getLogger(UserFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(UserFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(UserFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(UserFrame.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 UserFrame().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JButton jButton3; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    // End of variables declaration     
} 
+1

這是我通過JPanel可視屬性實現的。 == CardLayout – mKorbel 2015-02-11 11:21:59

+0

這是來自NetBeans JFrameForm的一個簡單例子 – Sarz 2015-02-11 11:25:36

+0

Pure CardLayout http://www.tutorialspoint.com/swing/swing_cardlayout.htm – Sarz 2015-02-11 11:47:41