如何聚焦JtabbedPane创建选项卡

时间:2014-11-22 07:22:19

标签: java jtabbedpane autofocus

我想Focus最近打开了一个JtabbedPane的Tab。在我的应用程序中,我创建任意数量的选项卡It Focus on First选项卡,最近不焦点自动创建Tab。例如:当我创建First选项卡时焦点在第一个选项卡上,然后我创建另一个选项卡焦点转移到下一个创建选项卡自动不在第一个选项卡中。请检查一次。谢谢。

我的代码:

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 个答案:

答案 0 :(得分:1)

您遇到的问题与焦点无关,因为JTabbedPane已经是应用程序中的焦点组件,而您需要的是更改组件的选定索引。 以下代码就是这样,将其添加到createActionPerformed方法的末尾:

tabbedPane.setSelectedIndex(i-1);