我正在尝试使用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>
这是文件夹结构。我尝试了很多不同的组合和名称,并且&lt;&gt;分隔符,但我无法正常打开它。
答案 0 :(得分:2)
问题是applet容器(通常是浏览器)已被告知在哪里可以找到类Square
而不是类SquarePanel
。你可以做以下两件事之一:
将您的课程封闭在JAR中,并在archive
代码中指定<APPLET\>
名称,如here所示。
将SquarePanel
嵌套在Square
中,如下图所示。
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);
}
}
}