问题描述:

The part with asterisks is what it shows error. Can anyone explain to me why this is happening?

I already have a public class in the beginning.

If I take off the bracket "()", the code following that all shows errors.

Thanks!

 public class ButtonTester {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new adasdad();

}

//Setting up variables

private static final int FRAME_WIDTH = 500;

private static final int FRAME_HEIGHT = 600;

private JButton button = new JButton ("A");

private JButton button2 = new JButton ("B");

**class ExP12_4 () {**

//Creating a frame

//Creating a panel

...

...

//Display frame

...

}

网友答案:

You have to remove the parenthesis and create a constructor for the class:

class ExP12_4 {
    private JFrame frame;
    private JPanel panel;

    private JButton button;
    private JButton button2;

    public ExP12_4 (){
        button = new JButton ("A");
        button2 = new JButton ("B");

        frame = new JFrame ();
        panel = new JPanel ();

        panel.add(button);
        panel.add(button2);
        frame.add(panel); 

        ...
    }
}

Take a look at this tutorial for more info about Java Classes

网友答案:

There's no parenthesis allowed in a class definition. These belong in a constructor. Also you need to put your statements in a code block such as the constructor or init method:

public class ExP12_4 {

    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 300;
    private JButton button;
    private JButton button2;

    public ExP12_4() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        panel.add(button);
        panel.add(button2);
        frame.add(panel);

        // etc.
    }
}
相关阅读:
Top