01 package cn.rolia.learning.swing; 02 import java.awt.FlowLayout; 03 import java.awt.GridLayout; 04 import java.awt.BorderLayout; 05 import java.awt.Color; 06 import java.awt.Dimension; 07 //import javax.swing.BoxLayout; 08 import javax.swing.JTabbedPane; 09 import javax.swing.JPanel; 10 import javax.swing.JFrame; 11 import javax.swing.JButton; 12 import javax.swing.ImageIcon; 13 import javax.swing.UIManager; 14 15 class TabPane extends JTabbedPane 16 { 17 /** 18 * 19 */ 20 private static final long serialVersionUID = 1L; 21 JPanel panel1; 22 JPanel panel2; 23 JPanel panel3; 24 JPanel panel4; 25 JPanel panel5; 26 27 public TabPane () 28 { 29 //super (a, b); 30 panel1 = new JPanel (); 31 panel1.add(new JButton()); 32 panel2 = new JPanel (new BorderLayout()); 33 panel3 = new JPanel (true); 34 panel4 = new JPanel (new GridLayout(2,3)); 35 panel5 = new JPanel (new FlowLayout(FlowLayout.LEFT, 1, 0)); 36 //以上是JPanel的四种构造方法 37 //窗体布局器有四种设置: 38 //new BorderLayout () or new BorderLayout (int hgap, int vgap) 39 //new GridLayout () or new GridLayout (int rows, int cols) new GridLayout (int rows, int cols, int hgap, int vgap) 40 //new FlowLayout () or new FlowLayout (int align) or new FlowLayout (int align, int hgap, int vgap) 41 //new BoxLayout (Container target, int axis) 42 43 //public JTabbedPane (int tabPlacement, int tabLayoutPolicy) 44 //tabPlacement:JTabbedPane.TOP, JTabbedPane.BOTTOM, JTabbedPane.LEFT, or JTabbedPane.RIGHT. 45 //ltabLayoutPolicy:JTabbedPane.WRAP_TAB_LAYOUT or JTabbedPane.SCROLL_TAB_LAYOUT. 46 //其他两种JTabbedPane的构造方法: 47 //JTabbedPane () 48 //JTabbedPane (int tabPlacement) 49 add (panel1, 0); 50 setBackgroundAt (0, Color.cyan); 51 setForegroundAt (0,Color.red); 52 53 add (panel2, this , 1); 54 setEnabledAt (1, false); 55 setTitleAt (1,"panel one"); 56 57 addTab ("panel3", panel3); 58 setToolTipTextAt (2, "ToolTip3"); 59 60 addTab ("panel4", new ImageIcon("QQ.png"), panel4, "ToolTip4"); 61 62 addTab ("panel5", panel5); 63 64 setPreferredSize (new Dimension (500,200)); 65 setTabPlacement (JTabbedPane.BOTTOM);//设置标签的位置 66 setTabLayoutPolicy (JTabbedPane.SCROLL_TAB_LAYOUT); 67 } 68 } 69 70 public class JTabbedPaneDemo 71 { 72 public static void main(String[] args) 73 { 74 try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}//显示风格 75 TabPane tabPane = new TabPane (); 76 JFrame frame = new JFrame ("JTabbedPaneDemo"); 77 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 78 frame.setVisible (true);//默认为false 79 frame.setContentPane(tabPane); 80 frame.pack(); 81 } 82 }
|