执行JFrame程序

hkg*_*ile 1 java swing execute

如何执行这个程序?

// 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

---------------------------------当前来源--------------- ----------------------

// 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);
              }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*hev 12

每个Java程序都从一个main方法开始:

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

将其添加到您的帧类.