JFrame class 在 Main 中不工作
JFrame class not working in Main
我正在尝试编写一个 Java 程序来声明一个 JFrame class 并在主程序中创建一个 class 的 object。在我的生活中,当我在 main 中创建 "MyButtons" object 时,我无法让 JFrame 标题、JPanel 和 JButtons 出现。我得到的只是一个空白的 JPanel。
import java.awt.*;
import javax.swing.*;
import java.util.*;
class MyButtons extends JFrame
{
public MyButtons()
{
JFrame frame = new JFrame("MyButtons");
JPanel panel = new JPanel();
frame.add(panel);
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
panel.add(b1);
panel.add(b2);
}
}
class TestMyButtons
{
public static void main(String [] args)
{
MyButtons go = new MyButtons();
go.setSize(200,75);
go.setLocation(200,300);
go.setVisible(true);
go.setResizable(true);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
您正在创建 JFrame
、MyButtons
和 JFrame frame = new JFrame("MyButtons");
的两个实例,您正在向其中添加所有组件...
很少需要直接从像 JFrame
这样的顶级容器扩展,而是从像 JPanel
这样的东西扩展,然后将其添加到 [=12] 的实例=] 而不是...
MyButtons
...
import javax.swing.JButton;
import javax.swing.JPanel;
class MyButtons extends JPanel {
public MyButtons() {
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
add(b1);
add(b2);
}
}
TestMyButtons
...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
class TestMyButtons {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyButtons());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
当您扩展 JFrame 时,您不需要在构造函数中创建另一个 JFrame 实例。只需将 panel
添加到已经继承 JFrame
.
的 MyButtons
public MyButtons(){
//JFrame frame = new JFrame("MyButtons"); Commentted this
JPanel panel = new JPanel();
//frame.add(panel); And this
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
panel.add(b1);
panel.add(b2);
add(panel); // Add panel to MyButtons frame
}
我正在尝试编写一个 Java 程序来声明一个 JFrame class 并在主程序中创建一个 class 的 object。在我的生活中,当我在 main 中创建 "MyButtons" object 时,我无法让 JFrame 标题、JPanel 和 JButtons 出现。我得到的只是一个空白的 JPanel。
import java.awt.*;
import javax.swing.*;
import java.util.*;
class MyButtons extends JFrame
{
public MyButtons()
{
JFrame frame = new JFrame("MyButtons");
JPanel panel = new JPanel();
frame.add(panel);
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
panel.add(b1);
panel.add(b2);
}
}
class TestMyButtons
{
public static void main(String [] args)
{
MyButtons go = new MyButtons();
go.setSize(200,75);
go.setLocation(200,300);
go.setVisible(true);
go.setResizable(true);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
您正在创建 JFrame
、MyButtons
和 JFrame frame = new JFrame("MyButtons");
的两个实例,您正在向其中添加所有组件...
很少需要直接从像 JFrame
这样的顶级容器扩展,而是从像 JPanel
这样的东西扩展,然后将其添加到 [=12] 的实例=] 而不是...
MyButtons
...
import javax.swing.JButton;
import javax.swing.JPanel;
class MyButtons extends JPanel {
public MyButtons() {
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
add(b1);
add(b2);
}
}
TestMyButtons
...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
class TestMyButtons {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyButtons());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
当您扩展 JFrame 时,您不需要在构造函数中创建另一个 JFrame 实例。只需将 panel
添加到已经继承 JFrame
.
MyButtons
public MyButtons(){
//JFrame frame = new JFrame("MyButtons"); Commentted this
JPanel panel = new JPanel();
//frame.add(panel); And this
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
panel.add(b1);
panel.add(b2);
add(panel); // Add panel to MyButtons frame
}