【问题标题】:Call JApplet function from javascript is not a function从 javascript 调用 JApplet 函数不是函数
【发布时间】:2013-07-03 01:18:52
【问题描述】:

我正在尝试从 javascript 调用 JApplet 函数,但它不起作用。

这是我的小程序的代码:

public class Main extends JApplet implements ActionListener {
    private static final long serialVersionUID = 1L;
    private JTextArea m_textArea;
    private JButton m_button;
    public String mypublicvariable = "foo";

    public Main() {
        this.setLayout(null);

        m_textArea = new JTextArea();
        m_textArea.setBounds(25, 25, 200, 100);
        m_textArea.setText("some value");
        getContentPane().add(m_textArea);

        m_button = new JButton("crypt");
        m_button.setBounds(25, 150, 100, 20);
        m_button.addActionListener(this);
        getContentPane().add(m_button);
    }

    public static void main(String[] args) {
        new Main();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.getSource().equals(m_button)) {
            m_textArea.setText("worked");
        }
    }

    public void doSomething() {
        m_textArea.setText("sdlkjfdöjdlkjfsfsyfhudjfdsfj");
    }
}

这是我的 HTML 代码:

<!DOCTYPE HTML>
<html>
    <head>
    </head>
    <body>
        <applet name="TestApplet" id="TestApplet" code="main.Main" archive="test.jar" width="500" height="500" />
        <script>
            console.warn(document.TestApplet.mypublicvariable);

            document.TestApplet.doSomething();
        </script>
    </body>
</html>

我既不能获得公共变量的值(未定义),也不能调用公共函数(document.TestApplet.doSomething 不是函数)。为了清楚起见,我将 Main 类的整个项目导出为 jar 文件,并将其命名为 test.jar。我的 Main 类在 main 包中。

【问题讨论】:

  • @AndrewThompson 谢谢。它解决了这个问题。你想发布一个答案,以便我可以投票并将问题标记为完成。

标签: java javascript html applet japplet


【解决方案1】:

小程序元素无效。它从来都不是一个自动关闭的元素。为了稳健起见,它应该声明scriptable 标志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多