Java JTabbedPane create

Description

Java JTabbedPane create

// Demonstrate JTabbedPane. 

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

class Demo extends JPanel {

   public Demo() {
      JTabbedPane jtp = new JTabbedPane();
      jtp.addTab("Cities", new CitiesPanel());
      jtp.addTab("Colors", new ColorsPanel());
      jtp.addTab("Flavors", new FlavorsPanel());
      add(jtp);/*from w  w w.  j a va 2s  .  c  o m*/
   }
}

//Make the panels that will be added to the tabbed pane. 
class CitiesPanel extends JPanel {

   public CitiesPanel() {
      JButton b1 = new JButton("New York");
      add(b1);
      JButton b2 = new JButton("London");
      add(b2);
      JButton b3 = new JButton("Hong Kong");
      add(b3);
      JButton b4 = new JButton("Tokyo");
      add(b4);
   }
}

class ColorsPanel extends JPanel {

   public ColorsPanel() {
      JCheckBox cb1 = new JCheckBox("Red");
      add(cb1);
      JCheckBox cb2 = new JCheckBox("Green");
      add(cb2);
      JCheckBox cb3 = new JCheckBox("Blue");
      add(cb3);
   }
}

class FlavorsPanel extends JPanel {

   public FlavorsPanel() {
      JComboBox<String> jcb = new JComboBox<String>();
      jcb.addItem("Vanilla");
      jcb.addItem("Chocolate");
      jcb.addItem("Strawberry");
      add(jcb);
   }
}

public class Main {
   public static void main(String[] args) {
      Demo panel = new Demo();

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(panel);
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related