2

我有这个代码:

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>

当我运行它时,它给出了一个java.lang.reflect.InvocationTargetException!我查找了异常,发现没有任何帮助。

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

4

2 回答 2

1

小程序找不到splash.png文件,你把它包含在小程序Jar中了吗?

于 2012-05-02T02:14:04.277 回答
1

该图像位于包含 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));
// ...
于 2012-05-02T11:27:10.957 回答