简体   繁体   中英

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

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 an error. Most of the time it tells me that ";" is expected. I know that putting ";" infront of that code is wrong. Can someone tell me where exactly to write that? Or if possible fix the code for me? Here is the code:

import javax.swing.*;
import java.awt.*;

public class Panel_Test extends JFrame{

public static void main(String[] args){
    public Board(){

        super("Java Panel");

        setSize(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        JPanel a = new JPanel();
        JPanel a2 = new JPanel();
        JButton b = new JButton("Button 1");
        JButton c = new JButton("Button 2");
        JCheckBox cb = new JCheckBox("CB1");
        JCheckBox cb2 = new JCheckBox("CB2");
        a.add(b);
        a.add(c);
        a2.add(cb);
        a2.add(cb2);
        add(a, BorderLayout.SOUTH);
        add(a2, BorderLayout.NORTH);
        }
    }
}

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...
    }
}

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

Have your constructor be separate from the main method, and have its name be the same as your class name (ie 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM