1. 程式人生 > >JTabbedPaneTest選項卡的佈局方式

JTabbedPaneTest選項卡的佈局方式

import java.awt.BorderLayout;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JTabbedPaneTest extends JFrame {

 public JTabbedPaneTest()
 {
  super();
  setTitle("表格");
  setBounds(100,100,350,150);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);//TOP,BOTTOM,LEFT,RIGHT
  final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
  //tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); //設定選項卡的佈局方式。滾動
  tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT); //設定選項卡的佈局方式。
  tabbedPane.addChangeListener(new ChangeListener(){
   public void stateChanged(ChangeEvent e){
    int selectedIndex = tabbedPane.getSelectedIndex();  //獲得選中的選項卡索引
    String title = tabbedPane.getTitleAt(selectedIndex); //獲得選項卡標籤
    System.out.println(title);
   }
  });
  getContentPane().add(tabbedPane,BorderLayout.CENTER);
  URL resource = JTabbedPaneTest.class.getResource("imageButton.gif");
  ImageIcon imageIcon = new ImageIcon(resource);
  final JLabel tabLabelA = new JLabel();
  tabLabelA.setText("選項卡A");
  tabbedPane.addTab("選項卡A", imageIcon,tabLabelA,"點選檢視選項卡A");
  final JLabel tabLabelB = new JLabel();
  tabLabelB.setText("選項卡B");
  tabbedPane.addTab("選項卡B", imageIcon,tabLabelB,"點選檢視選項卡B");
  final JLabel tabLabelC = new JLabel();
  tabLabelC.setText("選項卡C");
  tabbedPane.addTab("選項卡C", imageIcon,tabLabelC,"點選檢視選項卡C");
  tabbedPane.setSelectedIndex(2);  //設定預設選中的
  tabbedPane.setEnabledAt(0,false);   //設定索引0的面板不可用
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  JTabbedPaneTest jTabbedPaneTest = new JTabbedPaneTest();
  jTabbedPaneTest.setVisible(true);
 }

}