简体   繁体   中英

Java applet Jbutton error

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?

  1. Applet s 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

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