Creating a Reusable JFrame by extending JFrame - Java Swing

Java examples for Swing:JFrame

Description

Creating a Reusable JFrame by extending JFrame

Demo Code

import java.awt.Container;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
  JButton b1 = new JButton("Button 1");
  JButton b2 = new JButton("this is a test 2");

  public Main(String title) {
    super(title);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();
    GroupLayout groupLayout = new GroupLayout(contentPane);
    contentPane.setLayout(groupLayout);/*from www .ja v a  2 s .  c om*/

    groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
        .addComponent(b1).addComponent(b2));

    groupLayout.setVerticalGroup(groupLayout
        .createParallelGroup(Alignment.BASELINE).addComponent(b1)
        .addComponent(b2));
  }

  public static void main(String[] args) {
    Main frame = new Main("Custom Frame");
    frame.pack();
    frame.setVisible(true);
  }
}

Related Tutorials