【问题标题】:ClassNotFound Exception when calling Applet调用 Applet 时出现 ClassNotFound 异常
【发布时间】:2014-10-31 14:16:36
【问题描述】:

我正在尝试使用 IntelliJ 设置一个简单的JApplet。我有一个名为Square 的类和一个应该使它工作的HTML 文件,但我不断收到ClassNotFoundException

import javax.swing.*;
import java.awt.*;

public class Square extends JApplet {

    int size = 40;

    public void init() {
        JButton butSmall = new JButton("Small");
        JButton butMedium = new JButton("Medium");
        JButton butLarge = new JButton("Large");
        JButton butMessage = new JButton("Say Hi!");

        SquarePanel panel = new SquarePanel(this);
        JPanel butPanel = new JPanel();
        butPanel.add(butSmall);
        butPanel.add(butMedium);
        butPanel.add(butLarge);
        butPanel.add(butMessage);
        add(butPanel, BorderLayout.NORTH);
        add(panel, BorderLayout.CENTER);
    }
}

class SquarePanel extends JPanel {

    Square theApplet;

    SquarePanel(Square app) {
        theApplet = app;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.green);
        g.fillRect(20, 20, theApplet.size, theApplet.size);
    }
}

和 HTML 文件

<HTML>
<APPLET CODE="Square.class"> WIDTH=400 HEIGHT=200>
</APPLET>
</HTML>

这是文件夹结构。我尝试了很多不同的组合和名称以及 分隔符,但我无法正确打开它。

【问题讨论】:

    标签: java swing intellij-idea applet


    【解决方案1】:

    问题是小程序容器(通常是浏览器)被告知在哪里可以找到类Square,而不是类SquarePanel。你可以做以下两件事之一:

    • 将您的类包含在一个 JAR 中,并在您的 &lt;APPLET\&gt; 标记中指定 archive 名称,如 here 所示。

    • Square 中嵌套SquarePanel,如下图所示。

    JAR 是首选方法,但也可以考虑使用hybrid 以获得更灵活的测试和部署。为了方便appletviewer测试,标签包含在注释中,如图here

    命令行:

    $ appletviewer Square.java
    

    经过测试的代码:

    // <applet code='Square' width='400' height='200'></applet>
    import javax.swing.*;
    import java.awt.*;
    
    public class Square extends JApplet {
    
        int size = 40;
    
        public void init() {
            JButton butSmall = new JButton("Small");
            JButton butMedium = new JButton("Medium");
            JButton butLarge = new JButton("Large");
            JButton butMessage = new JButton("Say Hi!");
    
            SquarePanel panel = new SquarePanel(this);
            JPanel butPanel = new JPanel();
            butPanel.add(butSmall);
            butPanel.add(butMedium);
            butPanel.add(butLarge);
            butPanel.add(butMessage);
            add(butPanel, BorderLayout.NORTH);
            add(panel, BorderLayout.CENTER);
        }
    
        private static class SquarePanel extends JPanel {
    
            Square theApplet;
    
            SquarePanel(Square app) {
                theApplet = app;
            }
    
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.green);
                g.fillRect(20, 20, theApplet.size, theApplet.size);
            }
        }
    }
    
    猜你喜欢
    • 2012-09-24
    • 2011-07-07
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 2015-08-20
    • 1970-01-01
    相关资源
    最近更新 更多