Where will i put “Public static void main(String[] args)”?

前端 未结 4 1888
陌清茗
陌清茗 2021-01-17 00:54

I started coding java a few days ago. İ made a few succesfull programs but im stuck on this one. Where ever i write the \"Public static void main(String[] args)\" code i get

4条回答
  •  不知归路
    2021-01-17 01:38

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

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

提交回复
热议问题