且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我将把“Public static void main(String [] args)”放在哪里?

更新时间:2023-11-18 15:14:46

让您的构造函数与 main 方法分开,并使其名称与您的类名相同(即 public Panel_Test 因为您的班级名称是 Panel_Test )。

Have your constructor be separate from the main method, and have its name be the same as your class name (i.e. public Panel_Test since the name of your class is Panel_Test).

public class Panel_Test extends JFrame {
    public Panel_Test() {
        // code here
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Panel_Test();
            }
        });
    }
}

不要忘记对你进行Swing操作Event Dispatch Thread,使用 javax.swing.SwingUtilities

Don't forget to do your Swing operations on the Event Dispatch Thread, using javax.swing.SwingUtilities.