执行 JFrame 程序

发布于 2024-12-02 23:40:39 字数 3098 浏览 1 评论 0原文

如何执行这个程序?

// 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();
    }
}

----------------------------------电流源---------------------------- ----------------------

// 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);
              }
        });
    }
}

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);
              }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多彩岁月 2024-12-09 23:40:40

每个 Java 程序都从 ma​​in 方法开始:

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

将其添加到您的框架类中。

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.

FrameWithBorderLayout frameTest = new FrameWithBorderLayout();
frameTest.setVisible(true);
FrameWithBorderLayout frameTest = new FrameWithBorderLayout();
frameTest.setVisible(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文