2014-11-22 75 views
0

我想聚焦最近打開的JtabbedPane標籤。在我的應用程序中,我創建了任何數字標籤它只關注第一個標籤,最近沒有關注創建標籤自動創建。例如:當我創建First選項卡關注第一個選項卡,然後創建另一個選項卡焦點傳輸到下一個創建選項卡自動不在第一個選項卡中。請檢查一次。謝謝。如何聚焦JtabbedPane創建標籤

我的代碼:

public class TabbedPaneFocus extends javax.swing.JFrame { 

JTextArea tx; 
int i=0; 
JTabbedPane tabbedPane; 

public TabbedPaneFocus() { 

    initComponents(); 
    tabbedPane=new CloseButtonTabbedPane(); 
    add(tabbedPane); 
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE) 
    ); 

} 

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

    jMenuBar1 = new javax.swing.JMenuBar(); 
    jMenu1 = new javax.swing.JMenu(); 
    create = new javax.swing.JMenuItem(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jMenu1.setText("File"); 

    create.setText("Create"); 
    create.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      createActionPerformed(evt); 
     } 
    }); 
    jMenu1.add(create); 

    jMenuBar1.add(jMenu1); 

    setJMenuBar(jMenuBar1); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 512, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 366, Short.MAX_VALUE) 
    ); 

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

private void createActionPerformed(java.awt.event.ActionEvent evt) {          
    try{ 
     i++; 
     tx = new JTextArea(); 
     tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13)); 
     JScrollPane scrollpane=new JScrollPane(tx); 
     tabbedPane.add(scrollpane); 
     tabbedPane.addTab("Doc"+i, scrollpane); 
     tabbedPane.setFocusable(true); 
    } 
    catch(ArrayIndexOutOfBoundsException aio){ 
    } 
}          

public static void main(String args[]) { 
    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(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new TabbedPaneFocus().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JMenuItem create; 
private javax.swing.JMenu jMenu1; 
private javax.swing.JMenuBar jMenuBar1; 
// End of variables declaration     
} 

回答

1

您有不相關的重點,因爲JTabbedPane已經在你需要,而哪些應用程序的聚焦組件的問題是要改變組件的選定索引。 這下面這段代碼做到了這一點,把它添加到您的createActionPerformed方法的末尾:

tabbedPane.setSelectedIndex(i-1);