我有这个代码:
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/