Use JTabbedPane to create tabbed layout in Java
Description
The following code shows how to use JTabbedPane to create tabbed layout.
Example
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
/*from w ww .j a v a 2 s. com*/
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
static Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE,
Color.MAGENTA };
static void add(JTabbedPane tabbedPane, String label) {
int count = tabbedPane.getTabCount();
JButton button = new JButton(label);
button.setBackground(colors[count]);
tabbedPane.addTab(label, new ImageIcon("a.gif"), button, label);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Tabbed Pane Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
String titles[] = { "A", "B", "C", "D", "E", "F" };
for (int i = 0, n = titles.length; i < n; i++) {
add(tabbedPane, titles[i]);
}
frame.add(tabbedPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »