如何执行此程序?
// 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);
}
});
}
}
答案 0 :(得分: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);
}
});
}
将此添加到您的框架类。
答案 1 :(得分:0)
FrameWithBorderLayout frameTest = new FrameWithBorderLayout();
frameTest.setVisible(true);