执行 JFrame 程序
如何执行这个程序?
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个 Java 程序都从 main 方法开始:
将其添加到您的框架类中。
Every Java program starts from a main method:
Add this to your frame class.