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

前端 未结 4 1883
陌清茗
陌清茗 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:17

    Your problem lies with these 3 lines:

    public class Panel_Test extends JFrame{
    
    public static void main(String[] args){
        public Board(){
    

    The main method should not have the constructor in it either, this needs to be separate and outside of the method. I would also suggest having a Board class with the Board constructor and a Panel_Test class with the main method in there. Try this instead:

    public class Panel_Test {
    
        public static void main(String[] args){
            new Board().doSwing();
        }
    }
    
    public class Board extends JFrame {
    
        public Board() {
        }    
    
        public void doSwing() {
        //Your Swing code here...
        }
    }
    
    0 讨论(0)
  • 2021-01-17 01:25

    Your syntax doesn't make any sense. You have a method called Board() seemingly inside your main() method, which isn't possible.

    It further doesn't make sense, because Board() is a constructor (notice it starts with an upper-case letter and has no return type), but I don't see a Board class here.

    You need to figure out exactly what you want to do: are you supposed to create a Board class? If so, that belongs in another file. Do you already have a Board class that you're trying to construct from the main() method? If so, then you call the constructor using the new keyword.

    Recommended reading: The Java Tutorials: Providing Constructors for Your Classes

    0 讨论(0)
  • 2021-01-17 01:37

    Your Board() is actually a constructor, which cannot be created inside a method. Every class has a constructor. If one isn't coded, a default no-arg constructor is provided to you. The main problem you are facing is you are creating a constructor called Board inside a class called Panel_Test.

    To give you a basic example of how it works:

    public class Board extends JFrame {
    
        //your fields
    
        public Board() {//your constructor
    
        }
    
        //your methods
    
    }
    
    public class Test_Panel {
    
        public static void main(String[] args) {
    
            Board board = new Board();//calling your Test_Panel class' constructor 
    
        }
    
    }
    

    If you're new to programming and would like to become more solid with the basics, here's a nice website I've used on occasion when learning Java in my time as a programmer.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题