import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.*;
public class TabExample extends JFrame {
private JTabbedPane tabPane = new JTabbedPane();
private JPanel tab1 = new JPanel();
private JPanel tab2 = new JPanel();
public TabExample() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationByPlatform(true);
this.setSize(500, 300);
tab1.setLayout(new BorderLayout());
tab1.add(new JColoredPanel(), BorderLayout.CENTER);
tab1.add(new JColoredPanel(), BorderLayout.NORTH);
tab1.add(new JColoredPanel(), BorderLayout.EAST);
tab1.add(new JColoredPanel(), BorderLayout.SOUTH);
tab1.add(new JColoredPanel(), BorderLayout.WEST);
tab2.setLayout(new GridLayout(2, 3));
tab2.add(new JColoredPanel());
tab2.add(new JColoredPanel());
tab2.add(new JColoredPanel());
tab2.add(new JColoredPanel());
tab2.add(new JColoredPanel());
tab2.add(new JColoredPanel());
tabPane.addTab("tab1", tab1);
tabPane.addTab("tab2", tab2);
this.add(tabPane);
this.setVisible(true);
}
public static void main(String[] args) {
new TabExample();
}
class JColoredPanel extends JPanel {
public JColoredPanel() {
int r = (int) (Math.random() * 3);
r = r == 0 ? 3 : r;
System.out.println(r);
switch (r) {
case 1:
this.setBackground(Color.BLUE);
break;
case 2:
this.setBackground(Color.GREEN);
break;
case 3:
this.setBackground(Color.RED);
}
this.setBorder(BorderFactory.createEtchedBorder());
}
}
}