Search code examples
javaswingexecute

Execute a JFrame program


how to execute this program?

// Resolve class BorderLayout
import java.awt.*;
// Resolve class JFrame and JButton
import javax.swing.*;
// Definition of class FrameWithBorderLayout

public class FrameWithBorderLayout extends JFrame {// Attribute

    private JButton buttonEast; // The east button
    private JButton buttonSouth; // The south button
    private JButton buttonWest; // The west button
    private JButton buttonNorth; // The north button
    private JButton buttonCenter; // The center button
    // Constructor

    public FrameWithBorderLayout() {
        // Call super class constructor with a title
        super("Frame With Multiple Buttons");
        // Create JButton objects
        buttonEast = new JButton("East");
        buttonSouth = new JButton("South");
        buttonWest = new JButton("West");
        buttonNorth = new JButton("North");
        buttonCenter = new JButton("Center");
        // Add the JButton objects
        add(buttonEast, BorderLayout.EAST);
        add(buttonSouth, BorderLayout.SOUTH);
        add(buttonWest, BorderLayout.WEST);
        add(buttonNorth, BorderLayout.NORTH);
        add(buttonCenter, BorderLayout.CENTER);
        // Set when the close button is clicked, the application exits
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // Reorganize the embedded components
        pack();
    }
}

---------------------------------current source-------------------------------------

// Resolve class BorderLayout
import java.awt.*;
// Resolve class JFrame and JButton
import javax.swing.*;
// Definition of class FrameWithBorderLayout
public class test extends JFrame {

// Attribute
    private JButton buttonEast; // The east button
    private JButton buttonSouth; // The south button
    private JButton buttonWest; // The west button
    private JButton buttonNorth; // The north button
    private JButton buttonCenter; // The center button
// Constructor
    public test() {
    // Call super class constructor with a title
        super("Frame With Multiple Buttons");
        // Create JButton objects
        buttonEast = new JButton("East");
        buttonSouth = new JButton("South");
        buttonWest = new JButton("West");
        buttonNorth = new JButton("North");
        buttonCenter = new JButton("Center");
        // Add the JButton objects
        add(buttonEast, BorderLayout.EAST);
        add(buttonSouth, BorderLayout.SOUTH);
        add(buttonWest, BorderLayout.WEST);
        add(buttonNorth, BorderLayout.NORTH);
        add(buttonCenter, BorderLayout.CENTER);
        // Set when the close button is clicked, the application exits
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // Reorganize the embedded components
        pack();
    }
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                   FrameWithBorderLayout frame = new FrameWithBorderLayout();
                   frame.setVisible(true);
              }
        });
    }
}

Solution

  • Every Java program starts from a main method:

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                   FrameWithBorderLayout frame = new FrameWithBorderLayout();
                   frame.setVisible(true);
              }
        });
    }
    

    Add this to your frame class.