package Rong;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
public class TabbedPaneDemo extends JFrame {
private static final long serialVersionUID = 1L;
private String[] colorNames = { "red", "blue", "green", "black", "yellow",
"pink", "white" };
private Color[] colors = { Color.RED, Color.BLUE, Color.GREEN, Color.BLACK,
Color.YELLOW, Color.PINK, Color.WHITE };
// 頁籤在上端顯示
// private JTabbedPane tabs = new JTabbedPane(JTabbedPane.NORTH,
// JTabbedPane.SCROLL_TAB_LAYOUT);
// 頁籤在底部顯示
private JTabbedPane tabs = new
JTabbedPane(JTabbedPane.BOTTOM,JTabbedPane.SCROLL_TAB_LAYOUT);
private JTextField txt = new JTextField(20);
public TabbedPaneDemo(String title) {
super(title);
for (int i = 0; i < colors.length; i++) {
JPanel panel = new JPanel();
panel.setBackground(colors[i]);
tabs.addTab(colorNames[i], panel); // 加入一個頁面
}
tabs.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
txt.setText("Tab selected: " + tabs.getSelectedIndex());
}
});
Container contentPane = getContentPane();
contentPane.add(BorderLayout.SOUTH, txt);
contentPane.add(tabs);
setSize(400,300);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TabbedPaneDemo("頁籤");
}
}
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)