2013-07-03 20 views
1

我想從javascript調用JApplet函數,但它不起作用。從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。我的班主要在包主。

+0

@AndrewThompson謝謝。它解決了這個問題。你是否想發表一個答案,以便我可以放棄它並將問題標記爲已完成。 – Niklas

回答

0

小程序元素無效。它從來不是一個自閉的元素。爲了魯棒性,它應該聲明scriptable標誌。