import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
/**
* <p>Title: 選項卡演示</p>
* <p>Description: 這裡是一個選項卡演示,點擊不同的卡片,顯示的內容不同</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: TabbedPaneDemo.java</p>
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
super(new GridLayout(1, 1));
ImageIcon icon = createImageIcon("images/middle.gif");
JTabbedPane tabbedPane = new JTabbedPane();
Component panel1 = makeTextPanel("#第一個卡片#");
tabbedPane.addTab("One", icon, panel1,
"第一個卡片提示信息!");
tabbedPane.setSelectedIndex(0);
Component panel2 = makeTextPanel("##第二個卡片##");
tabbedPane.addTab("Two", icon, panel2,
"第二個卡片提示信息!");
Component panel3 = makeTextPanel("###第三個卡片###");
tabbedPane.addTab("Three", icon, panel3,
"第三個卡片提示信息!");
Component panel4 = makeTextPanel("####第四個卡片####");
tabbedPane.addTab("Four", icon, panel4,
"第四個卡片提示信息!");
//將選項卡添加到panl中
add(tabbedPane);
}
/**
*<br>方法說明:添加信息到選項卡中
*<br>輸入參數:String text 顯示的信息內容
*<br>返回類型:Component 成員對象
*/
protected Component makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
/**
*<br>方法說明:獲得圖片
*<br>輸入參數:String path 圖片的路徑
*<br>返回類型:ImageIcon 圖片對象
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn´t find file: " + path);
return null;
}
}
public static void main(String[] args) {
//使用Swing窗體描述
JFrame.setDefaultLookAndFeelDecorated(true);
//創建窗體
JFrame frame = new JFrame("TabbedPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TabbedPaneDemo(),
BorderLayout.CENTER);
//顯示窗體
frame.setSize(400, 200);
frame.setVisible(true);
}
}