วันเสาร์ที่ 2 มีนาคม พ.ศ. 2556

LAB 4_TabbedPaneTest

TabbedPaneTest(LAB 4)

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 

class TabbedPaneTest extends JFrame{ 
 private static final int DEFAULT_WIDIH=400; 
 private static final int DEFAULY_HEIGHT=300; 
 JTabbedPane tab; 
 TabbedPaneTest(){ 
  setTitle("TabbedPaneTest"); 
  setSize(DEFAULT_WIDIH,DEFAULY_HEIGHT); 
  tab=new JTabbedPane(); 
  ImageIcon icon=new ImageIcon("tab.gif"); 


  tab.addTab("Tab1",icon,null,"select tab1"); 
  tab.addTab("Tab2",icon,null,"select tab2"); 
  tab.addTab("Tab3",icon,null); 
  tab.addTab("Tab4",icon,null); 
  tab.addTab("Tab5",null); 
  tab.addTab("Tab6",null); 
  tab.addTab(null,null); 
  tab.addTab(null,null); 

  tab.setToolTipTextAt(2,"select tab3"); 
  tab.setToolTipTextAt(3,"select tab4"); 
  tab.setToolTipTextAt(4,"select tab5"); 
  tab.setToolTipTextAt(5,"select tab6"); 
  tab.setToolTipTextAt(6,"select tab7"); 
  tab.setToolTipTextAt(7,"select tab8"); 

  tab.setIconAt(4,icon); 
  tab.setIconAt(5,icon); 
  tab.setIconAt(6,icon); 
  tab.setIconAt(7,icon); 

  tab.setTitleAt(6,"tab7"); 
  tab.setTitleAt(7,"tab8"); 
  tab.addChangeListener(new ChangeListener(){ 
   public void stateChanged(ChangeEvent e){ 
    int i = tab.getSelectedIndex(); 
    System.out.println(tab.indexOfTab("Tab7")); 
    showTab(i); 
   } 
  }); 
  showTab(0); 
  add(tab,"Center"); 

  JPanel panel=new JPanel(); 
  JRadioButton wrapTab=new JRadioButton("Wrap tabs"); 
  wrapTab.addActionListener(new ActionListener(){ 
   public void actionPerformed(ActionEvent e){ 
    tab.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT); 
   } 
  }); 
  wrapTab.setSelected(true); 
  panel.add(wrapTab); 
  JRadioButton scrollTab=new JRadioButton("Scroll tabs"); 
  scrollTab.addActionListener(new ActionListener(){ 
   public void actionPerformed(ActionEvent e){ 
    tab.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 
   } 
  }); 
  panel.add(scrollTab); 
  ButtonGroup group=new ButtonGroup(); 
  group.add(wrapTab); 
  group.add(scrollTab); 
  add(panel,BorderLayout.SOUTH); 
 } 
 public void showTab(int i){ 
  String t=tab.getTitleAt(i); 
  JPanel p1=new JPanel(); 
  JLabel label=new JLabel("Looking at"+t); 
  p1.add(label); 
  tab.setComponentAt(i,p1); 
 } 
 public static void main(String[]args){ 
  JFrame frame=new TabbedPaneTest(); 
  frame.addWindowListener(new WindowAdapter(){ 
   public void windowClosing(WindowEvent e){ 
    System.exit(0); 
   } 
  }); 
  frame.setVisible(true); 
 } 
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น