Java JFrame content pane with FlowLayout manager

Description

Java JFrame content pane with FlowLayout manager


import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;

public class Main extends JFrame {
    JButton closeButton = new JButton("Close");

    public Main() {
        super("Simplest Event Handling JFrame");
        this.initFrame();
    }/*from   w ww.jav a2  s.c  o m*/

    private void initFrame() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Set a FlowLayout for the content pane 
        this.setLayout(new FlowLayout());

        // Add the Close JButton to the content pane 
        this.getContentPane().add(closeButton);

        // Add an ActionListener to closeButton 
        closeButton.addActionListener(e -> System.exit(0));
    }

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



PreviousNext

Related