2013-11-02 140 views
1

我有一個GUI,如下所示。卡布局card 1面板中的選項卡式窗格默認顯示第一個選項卡。當我導航到card 2時,我想知道如何使按鈕在那裏導航到card 1選項卡3.我知道如何從getParent()使用getParent()從該按鈕進入card 1,但我不知道如何顯示特定選項卡我想從那裏。 (注:card 1card 2是兩個不同的JPanel類,親本是一個JFrame類)JAVA Swing - 從另一個卡面板卡面板上的選項卡式面板setSelectedIndex()

圖片:左側卡1與標籤面板,右側作爲卡2

enter image description here

下面是我的代碼:

主要的JFrame:

import java.awt.CardLayout; 

public class NewJFrame extends javax.swing.JFrame { 

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

    jbtCard1 = new javax.swing.JButton(); 
    jbtCard2 = new javax.swing.JButton(); 
    cardPane = new javax.swing.JPanel(); 
    jPanelCard11 = new CardLayoutTest.JPanelCard1(); 
    jPanelCard21 = new CardLayoutTest.JPanelCard2(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jbtCard1.setText("Card 1"); 
    jbtCard1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jbtCard1ActionPerformed(evt); 
     } 
    }); 

    jbtCard2.setText("Card 2"); 
    jbtCard2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jbtCard2ActionPerformed(evt); 
     } 
    }); 

    cardPane.setName(""); // NOI18N 
    cardPane.setLayout(new java.awt.CardLayout()); 
    cardPane.add(jPanelCard11, "card1"); 
    cardPane.add(jPanelCard21, "card2"); 

    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() 
      .addGap(87, 87, 87) 
      .addComponent(jbtCard1) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addComponent(jbtCard2) 
      .addGap(80, 80, 80)) 
     .addComponent(cardPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addComponent(cardPane, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jbtCard2) 
       .addComponent(jbtCard1)) 
      .addContainerGap()) 
    ); 

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

private void jbtCard1ActionPerformed(java.awt.event.ActionEvent evt) {           
    CardLayout card = (CardLayout) cardPane.getLayout(); 
    card.show(cardPane, "card1"); 
}           

private void jbtCard2ActionPerformed(java.awt.event.ActionEvent evt) {           
    CardLayout card = (CardLayout) cardPane.getLayout(); 
    card.show(cardPane, "card2"); 
}           


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.JPanel cardPane; 
private CardLayoutTest.JPanelCard1 jPanelCard11; 
private CardLayoutTest.JPanelCard2 jPanelCard21; 
private javax.swing.JButton jbtCard1; 
private javax.swing.JButton jbtCard2; 
// End of variables declaration     
} 

卡1個小組:

public class JPanelCard1 extends javax.swing.JPanel { 

public JPanelCard1() { 
    initComponents(); 
} 

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

    jTabbedPane1 = new javax.swing.JTabbedPane(); 
    jPanel1 = new javax.swing.JPanel(); 
    jLabel2 = new javax.swing.JLabel(); 
    jPanel2 = new javax.swing.JPanel(); 
    jLabel1 = new javax.swing.JLabel(); 
    jPanel3 = new javax.swing.JPanel(); 
    jLabel3 = new javax.swing.JLabel(); 

    jLabel2.setText("card 1 tab 1 "); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGap(101, 101, 101) 
      .addComponent(jLabel2) 
      .addContainerGap(233, Short.MAX_VALUE)) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGap(62, 62, 62) 
      .addComponent(jLabel2) 
      .addContainerGap(138, Short.MAX_VALUE)) 
    ); 

    jTabbedPane1.addTab("tab1", jPanel1); 

    jLabel1.setText("card 1 tab 2"); 

    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(73, 73, 73) 
      .addComponent(jLabel1) 
      .addContainerGap(264, Short.MAX_VALUE)) 
    ); 
    jPanel2Layout.setVerticalGroup(
     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel2Layout.createSequentialGroup() 
      .addGap(92, 92, 92) 
      .addComponent(jLabel1) 
      .addContainerGap(108, Short.MAX_VALUE)) 
    ); 

    jTabbedPane1.addTab("tab2", jPanel2); 

    jLabel3.setText("card 1 tab 3"); 

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); 
    jPanel3.setLayout(jPanel3Layout); 
    jPanel3Layout.setHorizontalGroup(
     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel3Layout.createSequentialGroup() 
      .addGap(140, 140, 140) 
      .addComponent(jLabel3) 
      .addContainerGap(197, Short.MAX_VALUE)) 
    ); 
    jPanel3Layout.setVerticalGroup(
     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel3Layout.createSequentialGroup() 
      .addGap(71, 71, 71) 
      .addComponent(jLabel3) 
      .addContainerGap(129, Short.MAX_VALUE)) 
    ); 

    jTabbedPane1.addTab("tab3", jPanel3); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jTabbedPane1) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(0, 58, Short.MAX_VALUE)) 
    ); 
}// </editor-fold>       
// Variables declaration - do not modify      
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JLabel jLabel3; 
private javax.swing.JPanel jPanel1; 
private javax.swing.JPanel jPanel2; 
private javax.swing.JPanel jPanel3; 
private javax.swing.JTabbedPane jTabbedPane1; 
// End of variables declaration     
} 

卡2小組:

import java.awt.CardLayout; 
import javax.swing.JPanel; 

public class JPanelCard2 extends javax.swing.JPanel { 

public JPanelCard2() { 
    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() { 

    jButton1 = new javax.swing.JButton(); 
    jLabel1 = new javax.swing.JLabel(); 

    jButton1.setText("go to Card 1 tab 3"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jLabel1.setText("Card 2"); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(261, 261, 261) 
        .addComponent(jButton1)) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(52, 52, 52) 
        .addComponent(jLabel1))) 
      .addContainerGap(18, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(48, 48, 48) 
      .addComponent(jLabel1) 
      .addGap(75, 75, 75) 
      .addComponent(jButton1) 
      .addContainerGap(140, Short.MAX_VALUE)) 
    ); 
}// </editor-fold>       

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    //show card 1 from here 
    CardLayout card = (CardLayout) this.getParent().getLayout(); 
    card.show((JPanel) this.getParent(), "card1"); 
    //card1 shown, but how to show specific tab in the tab pane from here? 
}           

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

回答

1

我認爲你正在尋找JTabbedPane.html.setSelectedIndex(int)

以下是不是最優雅的解決方案,但它應該工作,而不需要大量的重構。

在NewJFrame添加一個方法:

public CardLayoutTest.JPanelCard1 getJPanelCard11() { 
    return jPanelCard11; 
} 

在JPanelCard1添加一個方法:

public void setTab(int index) { 
    jTabbedPane1.setSelectedIndex(index); 
} 

調用這些在JPanelCard2:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    //show card 1 from here 
    CardLayout card = (CardLayout) this.getParent().getLayout(); 
    card.show((JPanel) this.getParent(), "card1"); 

    //card1 shown, show specific tab in the tab pane from here: 

    NewJFrame parent = (NewJFrame) this.getParent(); 
    ((JPanelCard1) parent.getJPanelCard11()).setTab(1); 
} 
+0

它的工作原理。但爲什麼它不是優雅的方式?是因爲這種方式緊密耦合? –

+1

好問題;這麼晚纔回復很抱歉。誠然,這裏有一些相當緊密的耦合。您可能希望更明確地對動作/命令進行建模,甚至可以在選項卡,卡和應用程序級範圍內進行建模。如果你有很多卡片,標籤等,你也許能夠抽象出一些結構和解決方案。這些考慮超出了這個問題的範圍。 – Will

+0

我看到了。謝謝。 –

相關問題