Java 无法在HTML中使用小程序

Java 无法在HTML中使用小程序,java,applet,embedded-resource,japplet,Java,Applet,Embedded Resource,Japplet,我有以下代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MainApp extends JApplet implements ActionListener { private static final long serialVersionUID = -7076767216192554828L; JButton begin = new JButton(new

我有以下代码:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class MainApp extends JApplet implements ActionListener {
    private static final long serialVersionUID = -7076767216192554828L;
    JButton begin = new JButton(new ImageIcon("splash.png"));
    @Override
    public void init() {
        setSize(300, 300);
        setLayout(new BorderLayout());
    begin.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            begin();
        }

    });
    add(begin);
    setVisible(true);
}
private void begin() {
    remove(begin);
    repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
          //to be used later
}
}
当在Eclipse中的小程序查看器中看到它时,它可以完美地工作。但是,在HTML中,它会失败:

<html>
<head>
<title> Test </title>
<body>
<APPLET code="MainApp.class" width="300" height="300"> Applet unavailable </APPLET> <br>
<a href="essay.docx"> Essay </a> (right click, Save Target As, in the menu under the name change it to "All Files," save as "essay.docx")
</body>
</html>

试验
(右键单击,将目标另存为,在名称下的菜单中将其更改为“所有文件”,另存为“随笔.docx”)
当我运行它时,它会给出一个
java.lang.reflect.InvocationTargetException
!我查了一下异常,没有发现任何有用的东西


在我使用
.png
按钮之前。一切都很好。我还添加了
repaint()
,但这不会有什么不同。

小程序找不到
splash.png
文件,您是否将其包含在小程序Jar中

图像位于包含HTML文件的文件夹中


小程序没有jar。请尝试硬编码
JButton begin=new JButton(new imageIcon(“splash.png”)类似于
jbuttonbegin=newjbutton(newimageicon(“http://localhost/splash.png"));同时检查小程序控制台,它可能输出了一些错误。不,控制台没有错误。事实上,小程序显示了,然后它说“错误。点击这里了解详细信息。”我明天会发布一个网站。实际上,我将在mediafire上上传一个.zip文件。但在eclipse的小程序查看器中,它工作得非常好!相对于HTML或小程序JAR,
splash.png
位于何处?必须通过
URL
而不是
String
访问该图像,因为
String
被解释为
文件
路径,并且小程序无法从服务器加载
文件
,只能加载
URL
。因此,如何获取图像的URL?图像位于包含HTML文件的文件夹中。哦,等等,没关系,安瑟姆的评论中有它。
setSize(300300)应在HTML中设置小程序的大小。不要试图从代码中调整它的大小。不客气。很高兴你把它整理好了。:)
// called from somewhere in the methods (e.g. init()) of the applet class 
URL urlToImage = new URL(getDocumentBase(), "splash.png");
begin = new JButton(new ImageIcon(urlToImage));
// ...