Java JPanel extend

Description

Java JPanel extend


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class MyPanel extends JPanel {

  public MyPanel() {
    // first name
    add(new JLabel("First Name"));
    add(new JTextField("Java"));

    // last name/*ww w  .java  2  s.c  o m*/
    add(new JLabel("Last Name"));
    add(new JTextField("CSS"));

    // save button
    add(new JButton("Save"));
  }

}

public class Main {
  public static void main(String[] args) {
    // create frame for Main
    JFrame frame = new JFrame("java2s.com");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new MyPanel());
    frame.setSize(300, 210);
    frame.setVisible(true);
  }
}



PreviousNext

Related