0

Right now I'm working on a GUI program which uses the JButton class (found in an online Swing tutorial), with this code:

import javax.swing.JButton;
public class HelloWorld extends java.applet.Applet {
    public static void main(String[] args) {
        @ConstructorProperties(value="Click")
        public JButton(String Click); //identifier expected and ; expected error from here 
        drawString("test",50,25);
    }
}

The compiler gives an "identifier expected" error and a "; expected error". Why is that and what should I do?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
randomguy537
  • 155
  • 1
  • 1
  • 7
  • *"found in an online Swing tutorial"* Lose that tutorial. BTW - 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 19 '14 at 01:16

1 Answers1

2
  1. Applets do not have a main method
  2. You "seem" to be creating a method or class or some other "thing" within the main method (the syntax is not correct for either)
  3. I have no idea what drawString is...
  4. Don't mix heavy and light weight components (when you understand that statement, you'll be ready for Swing programming)
  5. Avoid applets, seriously, just avoid applets, they carry to much baggage and have to many other issues you just don't need right now

You declare an object using the syntax of [type] [identifier], ie JButton aButton. You initlaise that variable with the new keyword. JButton aButton = new JButton("Click");

You seem to lack a level of common Java language knowledge as well as a lack of knowledge of the Swing API

Start by going back to basic, starting with The Java™ Tutorials, looking at the topics convered by the section titled Trails Covering the Basics

Once you have that under your belt, take a look at Creating a GUI With JFC/Swing

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • *"`Applet`s do not have a main method"* But an hybrid applet/application might. ;) (Not that the OP should be coding applets yet, or possibly at all.) – Andrew Thompson Dec 19 '14 at 01:17
  • @AndrewThompson Hell no! Okay, technically it's possible, but I would kill anybody who tried. Have a "main" class for handling "windowed" applications, which loads a common component, have an "applet" which loads said same common component. Just make life easier for yourself... – MadProgrammer Dec 19 '14 at 01:19
  • *"Just make life easier for yourself."* Right you are. I guess I have 'MCVE' on the mind (in which it is *slightly* more convenient to jam the applet and `main(String[])` into the same class). – Andrew Thompson Dec 19 '14 at 01:21
  • @AndrewThompson I've seen it cause too many issues with the approach and I, personally, don't like it, but that's just me – MadProgrammer Dec 19 '14 at 01:37
  • @AndrewThompson I have no idea what I mean, I'm 4 hours away from a two week holiday, I think my brain might have left early :P – MadProgrammer Dec 19 '14 at 01:53