java - 无法在 HTML 中使用小程序

标签 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 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 的 applet 查看器中看到它时效果非常好。然而,在 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>

当我运行它时,它给出了一个java.lang.reflect.InitationTargetException!我查了一下异常,发现没有什么帮助。

在我使用 .png 作为按钮之前。一切都很好。我还添加了 repaint(),但这不会产生任何影响。

最佳答案

The image is in the folder that contains the HTML file.

// 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));
// ...

关于java - 无法在 HTML 中使用小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406837/

相关文章:

java - HTML 不再在 JLabel(和其他组件)中工作

java - Java 图像即将出现在 NPE 中

asp.net - 无法调试通过自定义 VirtualPathProvider 加载的 EmbeddedResource View

java - 如何设置 FormAuthenticator 属性

java - 使用 UTF-8 编码的 Tomcat 7

java - 使用 Webfilter 时 Keycloak 刷新过期 token

macOs Sierra 中的 Java 小程序崩溃

java - 如何在maven多模块项目中构建依赖模块

Applet 上的 Java NullPointerException

.net - 在没有程序集前缀的 .NET 程序集中嵌入资源?