2014-01-26 73 views
2

Im新的Java,需要一點幫助。這就是說我確定我的代碼是sl。。我使用NetBeans作爲GUI構建器,並且正在學習過程中的擺動。我在這裏有一個SSCC,但是因爲我使用NetBeans的時間相當長。添加選項卡替換選項卡(Java)

public class tabtest extends javax.swing.JFrame { 

public tabtest() { 
    initComponents(); 
} 

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

    addTabDialog = new javax.swing.JDialog(); 
    titleTextBox = new javax.swing.JTextField(); 
    jLabel1 = new javax.swing.JLabel(); 
    addTabConfirmButton = new javax.swing.JButton(); 
    jLabel2 = new javax.swing.JLabel(); 
    tabPane = new javax.swing.JTabbedPane(); 
    addTabButton = new javax.swing.JButton(); 

    addTabDialog.setBounds(new java.awt.Rectangle(0, 0, 400, 400)); 

    titleTextBox.setText("New Tab"); 

    jLabel1.setText("Tab Title"); 

    addTabConfirmButton.setText("Confirm"); 
    addTabConfirmButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      addTabConfirmButtonActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout addTabDialogLayout = new javax.swing.GroupLayout(addTabDialog.getContentPane()); 
    addTabDialog.getContentPane().setLayout(addTabDialogLayout); 
    addTabDialogLayout.setHorizontalGroup(
     addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(addTabDialogLayout.createSequentialGroup() 
      .addGap(23, 23, 23) 
      .addComponent(jLabel1) 
      .addGap(18, 18, 18) 
      .addGroup(addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(addTabConfirmButton) 
       .addComponent(titleTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addContainerGap(211, Short.MAX_VALUE)) 
    ); 
    addTabDialogLayout.setVerticalGroup(
     addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(addTabDialogLayout.createSequentialGroup() 
      .addGap(77, 77, 77) 
      .addGroup(addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(titleTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel1)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE) 
      .addComponent(addTabConfirmButton) 
      .addGap(84, 84, 84)) 
    ); 

    jLabel2.setText("This is a new Tab"); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    addTabButton.setText("add new tab"); 
    addTabButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      addTabButtonActionPerformed(evt); 
     } 
    }); 
    tabPane.addTab("+", addTabButton); 

    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(40, 40, 40) 
      .addComponent(tabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(34, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(89, 89, 89) 
      .addComponent(tabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(33, Short.MAX_VALUE)) 
    ); 

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

private void addTabButtonActionPerformed(java.awt.event.ActionEvent evt) {            
    addTabDialog.setLocationRelativeTo(null); 
    addTabDialog.setVisible(true); 
}            

private void addTabConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {              

    tabPane.addTab(titleTextBox.getText(),jLabel2); 
    tabPane.setSelectedIndex(tabPane.getTabCount()-1); 
    addTabDialog.setVisible(false); 

}             

public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new tabtest().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify      
private javax.swing.JButton addTabButton; 
private javax.swing.JButton addTabConfirmButton; 
private javax.swing.JDialog addTabDialog; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JTabbedPane tabPane; 
private javax.swing.JTextField titleTextBox; 
// End of variables declaration     

我需要幫助的主要部分是這樣的部分,剩下的只是構建GUI

private void addTabButtonActionPerformed(java.awt.event.ActionEvent evt){            
    addTabDialog.setLocationRelativeTo(null); 
    addTabDialog.setVisible(true); 
}            

private void addTabConfirmButtonActionPerformed(java.awt.event.ActionEvent evt){              

    tabPane.addTab(titleTextBox.getText(),jLabel2); 
    tabPane.setSelectedIndex(tabPane.getTabCount()-1); 
    addTabDialog.setVisible(false); 

} 

抱歉的代碼牆... 不管怎麼說,如果你運行該代碼你將會看到在嘗試添加2個選項卡後,第二個選項卡會替換您嘗試添加的第一個選項卡。我究竟做錯了什麼?我確定它很簡單,但任何幫助將非常感激!謝謝!

回答

0

通過再次添加一個帶有該組件的新選項卡,實際上並未添加新選項卡objet。如果將代碼更改爲如下所示

private void addTabConfirmButtonActionPerformed(
     java.awt.event.ActionEvent evt) { 

    tabPane.addTab(titleTextBox.getText(), new JLabel("This is my new tab")); 
    tabPane.setSelectedIndex(tabPane.getTabCount() - 1); 
    addTabDialog.setVisible(false); 

} 

它應該按預期工作。

原因是,第二個參數Component是您要顯示的內容。因爲您不斷添加相同的組件(讀取:不是組件的COPY),庫只會將輸入的新標題與ONE JLabel關聯起來,並繪製TabbedPane以反映這種情況。

+0

非常感謝。這很有道理。所以如果我想每次都顯示相同的東西(在我的特定程序中它是一個JTabbedPane),我將不得不在addTab方法中創建它?如果我有一個,並且每次都複製一份呢? – Sup3rb0wlz

+0

在TabbedPane的內部,可能有某種有序的映射用於String - > Component。因爲Java的排序是圍繞對象的引用傳遞的,所以如果你創建了一個副本(或者實例化了一個新的組件),它將是一個單獨的對象,並且可以按預期工作。 實例化新對象可能是更好的路線。 – Mike