Contoh program berikut ini mendemonstrasikan bagaimana membuat
panel-panel dalam bentuk Tab di java. Class yang digunakan adalah
JTabbedPane.
Berikut ini tampilannya:
Berikut ini contoh programnya:
02 | import java.awt.event.*; |
05 | public class TabbedPaneDemo extends JFrame { |
06 | private JTabbedPane tabPane; |
07 | private JLabel label1, label2, label3; |
08 | private JPanel panel1, panel2, panel3; |
10 | public TabbedPaneDemo () { |
11 | super ( "Tabbed Pane Demo" ); |
13 | tabPane = new JTabbedPane (); |
14 | label1 = new JLabel ( "Panel pertama" , SwingConstants.CENTER); |
15 | panel1 = new JPanel (); |
17 | tabPane.addTab ( "Tab Pertama" , null , panel1, "Panel Pertama" ); |
19 | label2 = new JLabel ( "Panel kedua" , SwingConstants.CENTER); |
20 | panel2 = new JPanel (); |
21 | panel2.setBackground(Color.ORANGE); |
23 | Icon icon = new ImageIcon ( "images/new.gif" ); |
24 | tabPane.addTab ( "Tab Kedua" , icon, panel2, "Panel Kedua" ); |
26 | label3 = new JLabel ( "Panel ketiga" , SwingConstants.CENTER); |
27 | panel3 = new JPanel (); |
28 | panel3.setBackground(Color.YELLOW); |
29 | panel3.setLayout( new BorderLayout()); |
30 | panel3.add( new JButton ( "North" ), BorderLayout.NORTH); |
31 | panel3.add( new JButton ( "West" ), BorderLayout.WEST); |
32 | panel3.add( new JButton ( "East" ), BorderLayout.EAST); |
33 | panel3.add( new JButton ( "South" ), BorderLayout.SOUTH); |
35 | tabPane.addTab ( "Tab Ketiga" , null , panel3, "Panel Ketiga" ); |
37 | getContentPane().add(tabPane); |
40 | setLocationRelativeTo ( null ); |
44 | public static void main (String args[]) { |
45 | TabbedPaneDemo test = new TabbedPaneDemo (); |
46 | test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
Semoga bermanfaat