Java 调用小程序时ClassNotFound异常
我正在尝试使用IntelliJ设置一个简单的Java 调用小程序时ClassNotFound异常,java,swing,intellij-idea,applet,Java,Swing,Intellij Idea,Applet,我正在尝试使用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
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>
宽度=400高度=200>
这是文件夹结构。我尝试了很多不同的组合、名称和分隔符,但无法正确打开
问题在于,小程序容器(通常是浏览器)被告知在哪里可以找到类
Square
,而不是类SquarePanel
。您可以执行以下两种操作之一:
- 将类封装在JAR中,并在
中指定
名称存档
- 将
嵌套在SquarePanel
中,如下所示,以便于说明Square
jar是首选的方法,但也考虑了更灵活的测试和部署。为了方便
appletviewer
测试,标签包含在注释中,如图所示
命令行:
$ 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);
}
}
}
//
导入javax.swing.*;
导入java.awt.*;
公共类广场延伸至JApplet{
int size=40;
公共void init(){
JButton butSmall=新JButton(“小”);
JButton butMedium=新JButton(“Medium”);
JButton butLarge=新JButton(“大”);
JButton butMessage=新的JButton(“说嗨!”);
方形面板=新的方形面板(本);
JPanel butPanel=新的JPanel();
butPanel.add(butSmall);
butPanel.add(butMedium);
butPanel.add(butLarge);
butPanel.add(butMessage);
添加(butPanel,BorderLayout.NORTH);
添加(面板、边框布局、中心);
}
私有静态类SquarePanel扩展了JPanel{
正方形的应用程序;
方形面板(方形应用程序){
theApplet=app;
}
公共组件(图形g){
超级组件(g);
g、 setColor(Color.green);
g、 fillRect(20,20,theApplet.size,theApplet.size);
}
}
}
另请参见和。