I just want to add a button in my empty frame and it's very very simple.

The frame is not showing anything and although the program runs, IDE tells me :"

java.lang.ClassCastException: Spots cannot be cast to java.applet.Applet
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:793)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:722)
    at sun.applet.AppletPanel.run(AppletPanel.java:379)
    at java.lang.Thread.run(Thread.java:744)"

So I extend the JApplet for it and it doesn't complain anymore, the frame now is grey and still nothing. Also, the title is not showing.

What is interesting is that even if I fully copy the example code on the Oracle tutorial site(Official) : Tutorial Site, the same happens and it compains the applet thing.

Please help and Thank you very much!!!

public class Spots{

private static void createAndShowGUI() {
    JFrame frame = new JFrame();
    JButton jButton = new JButton("Click Me");

    jButton.setSize(20,20);
    jButton.setVisible(true);

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(jButton);
    frame.setSize(500, 500);
    frame.setTitle("Bar Code Scanner");

    frame.pack();
    frame.setVisible(true);

}

public static void main(String[] args){

    createAndShowGUI();



    }
}

To everybody who has this issue-- please check your IDE running setting to see it runs as an applet or application. Thanks for your kindly help again!

有帮助吗?

解决方案

If you're making an applet your code shouldn't even have a JFrame variable. Instead in the init method, add your JButton to the applet's contentPane() or to a JPanel that is added to the contentPane as just about any tutorial will tell you.

Of course this doesn't hold if you're not trying to create an applet, but if so, why would you be running it as if it were an applet. Please clarify this for us.


Edit
You ask in comment:

Thanks for the help! but what if I want to do it in Jframe way? Because I don't want to create an applet so I didn't extends Japplet or create init(). But the ide complains "you need to extends applet" Is anywhere in my code telling it I'm creating an applet? I dont want to :(

The IDE shouldn't care what type of class you're creating as long as it compiles. It's when you try to run the code that the JVM might complain that it's not an applet, if 1) you try to run it as an applet called in some HTML code, or 2) try to run it with your IDE's applet simulator. If the former, don't do it. Run it as a stand alone program. If the latter, don't do it. Tell the IDE that you're trying to run a Java program, and for both, make sure that you've got a valid main method.

其他提示

filename cannot be cast to java.applet.Applet: check points: 1. extends JFrame -> Extends JApplet 2. public constructor() -> public void init() 3. /public static void main(String[] args) { ... }/. It means no need this method. 4. When you've done all above, then save and compile. Because When you run(appletviewer index.html), you need filename.class. This filename.class should be the one that you've done 1~3 and compile already.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top