1. 程式人生 > >Java實現JTabbedPane選項卡的切換

Java實現JTabbedPane選項卡的切換

1、獲取JTabbedPane的選項卡切換的事件

tabs.addChangeListener(new   Test_tabs_changeAdapter(this));   

  void   tabs_stateChanged(ChangeEvent   e)   {   
          if   (   (   (JTabbedPane)   e.getSource()).getSelectedIndex()   ==   0)   {   
          }   
          else   if   (   (   (JTabbedPane)   e.getSource()).getSelectedIndex()   ==   1
) { } else if ( ( (JTabbedPane) e.getSource()).getSelectedIndex() == 2) { } else if ( ( (JTabbedPane) e.getSource()).getSelectedIndex() == 3) { } else if ( ( (JTabbedPane) e.getSource()).getSelectedIndex() == 4
) { } } class Test_tabs_changeAdapter implements javax.swing.event.ChangeListener { Test adaptee; Test_tabs_changeAdapter(Test adaptee) { this.adaptee = adaptee; } public void stateChanged(ChangeEvent e) { adaptee.tabs_stateChanged(e); } }

2、通過一個按鈕控制jtabbedpane的tab的切換

yourButton.addActionListener(new   ActionListener(){   
        public   void   actionPerformed(ActionEvent   e)   {   
              if(e.getSource().....)   {   
                      yourTabbedPane.setSelectedIndex(<whatever>);   
                      or   
                      yourTabbedPane.setSelectedComponent(<whatever>);   
              }   
        }
});   
3private JTabbedPane getJTabbedPane() {
        jTabbedPane = new JTabbedPane();
        jScrollPane = new JScrollPane();
        jDesktopPane = new JDesktopPane();
        jDesktopPane2 = new JDesktopPane();
        jTabbedPane.addTab("Pane甲", null, jScrollPane, null);
        jTabbedPane.addTab("Pane乙", null, jDesktopPane, null);                        
        jTabbedPane.addTab("Pane丙", null, jDesktopPane,null);
}

如何能在jScrollPane里加一個按鈕,點選後介面就轉移到頁面jDesktopPane2?

jTabbedPane.setSelectedComponent(jDesktopPane2);