Java Swing How to - import javax.swing.JButton;








Question

We would like to know how to import javax.swing.JButton;.

Answer

import javax.swing.JFrame;
import javax.swing.JPanel;
//from w w  w .  ja v  a 2  s . c o m
public class Main extends JFrame {
  public Main() {
    super("Test");
    setSize(200, 200);

    JPanel panel = new JPanel();
    setContentPane(panel);

    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");
    JButton button3 = new JButton("Button 3");

    button1.setBounds(10, 10, 100, 40);
    button2.setBounds(5, 5, 100, 30);
    button3.setBounds(15, 15, 150, 40);

    panel.setLayout(null);

    panel.add(button1);
    panel.add(button2);
    panel.add(button3);

    panel.setComponentZOrder(button1, 1);
    panel.setComponentZOrder(button2, 0);
    panel.setComponentZOrder(button3, 2);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }

  public static void main(String[] args) {
    new Main();
  }
}