简体   繁体   English

Jframe 未显示任何内容且“无法转换为 java.applet.Applet”

[英]Jframe not showing anything and "cannot be cast to java.applet.Applet"

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 :"该框架没有显示任何内容,尽管程序运行,但 IDE 告诉我:”

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.所以我为它扩展了 JApplet,它不再抱怨了,框架现在是灰色的,仍然没有。 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.有趣的是,即使我完全复制了 Oracle 教程站点(官方): 教程站点上的示例代码,也会发生同样的情况并且它包含小程序。

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.对于遇到此问题的每个人 - 请检查您的 IDE 运行设置以查看它作为小程序或应用程序运行。 Thanks for your kindly help again!再次感谢您的热心帮助!

If you're making an applet your code shouldn't even have a JFrame variable.如果您正在制作小程序,您的代码甚至不应该有 JFrame 变量。 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.相反,在 init 方法中,将您的 JButton 添加到小程序的contentPane()或添加到添加到 contentPane 的 JPanel,就像任何教程都会告诉您的那样。

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?但是如果我想用 Jframe 的方式来做呢? Because I don't want to create an applet so I didn't extends Japplet or create init().因为我不想创建小程序所以我没有扩展 Japplet 或创建 init()。 But the ide complains "you need to extends applet" Is anywhere in my code telling it I'm creating an applet?但是 ide 抱怨“你需要扩展小程序”我的代码中是否有任何地方告诉它我正在创建一个小程序? I dont want to :(我不想:(

The IDE shouldn't care what type of class you're creating as long as it compiles. IDE 不应该关心您正在创建什么类型的类,只要它编译即可。 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.当您尝试运行代码时, JVM可能会抱怨它不是小程序,如果 1) 您尝试将它作为在某些 HTML 代码中调用的小程序运行,或者 2) 尝试使用 IDE 的小程序模拟器运行它。 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.告诉 IDE 您正在尝试运行 Java 程序,并确保您有一个有效的 main 方法。

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) { ... } /.文件名不能转换为 java.applet.Applet:检查点: 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. 4. 完成以上所有操作后,保存并编译。 Because When you run(appletviewer index.html), you need filename.class.因为当你运行(appletviewer index.html)时,你需要filename.class。 This filename.class should be the one that you've done 1~3 and compile already .这个文件名.class 应该是你已经完成 1~3 并且已经编译的那个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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