【问题标题】:Java applets in htmlhtml 中的 Java 小程序
【发布时间】:2013-08-01 03:51:58
【问题描述】:

当我使用 HTML 5 的新对象标签时,我似乎无法让这个 jar 在 html 中运行。 我需要添加任何东西才能使其正常工作吗?这是 w3schools 网站上的外观,除了他们将其链接到 .swf 文件。

<object height = "800" width="600" data="ECPrototype.jar"></object>

使用代码更新:

import java.applet.Applet; 
import java.awt.Dimension;         
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.Timer;


public class EC extends Applet implements ActionListener{
    private static final long serialVersionUID = 1L;
    Animation test= new Animation();
    Timer timer= new Timer(5,this);
    Thread thread = new Thread(test);
    Thread t = null; 
    public void init() {
    }
     public void stop() {
    }
    public void actionPerformed(ActionEvent e) {
        test.move();
        test.update();
        test.repaint();
    }

    private class TAdapter extends KeyAdapter implements ActionListener {

        public void keyReleased(KeyEvent e) {
            test.keyReleased(e);
            test.stopAnimation();
        }

        public void keyPressed(KeyEvent e) {
            test.keyPressed(e);
            test.startAnimation();
            t= new Thread(test.animate);
            t.start();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
        }
    }
    public EC()
    {
        thread.start();
        timer.start();
        JFrame window=new JFrame("EC");
        window.setPreferredSize(new Dimension(800,600));
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(test);
        window.addKeyListener(new TAdapter());
        window.setFocusable(true);
        window.pack();
        window.setVisible(true);
    }
    public static void main(String args[])
    {
        new EC();
    }
}

【问题讨论】:

    标签: java html applet object-tag


    【解决方案1】:

    找到here并在我的电脑上测试:

    <object type="application/x-java-applet" width="400" height="400">
        <param name="code" value="name.of.your.Applet">
        <param name="archive" value="YourJarFile.jar">
    </object>
    

    关于框架问题,尝试重写构造函数、init和main:

        public void init() {
            addKeyListener(new TAdapter()); // only executed in applet
        }
        public EC() {
            // executed in both applet and application
            thread.start();
            timer.start();
        }
        public static void main(String args[]) {   
            // only executed in application
            EC ec = new EC();
            JFrame window=new JFrame("EC");
            window.setPreferredSize(new Dimension(800,600));
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.add(ec.test);
            window.addKeyListener(ec.new TAdapter());
            window.setFocusable(true);
            window.pack();
            window.setVisible(true);
        }
    

    【讨论】:

    • 仍然不起作用,我尝试使用您链接到的页面中的示例,但这也不起作用。 小程序无法运行。未找到 Java 插件。 对象>。这就是我想要的。
    • @user1058860 尝试执行命令appletviewer path/to/your/document.html。有错误提示吗?
    • 好的,所以我只是在 java.policy 中授予权限,当我在终端中运行命令时它运行了,但是它在 safari 中仍然不起作用。
    • 也在 Firefox 中我得到 java.lang.ExceptionInInitializerError
    • 我正在使用 jframe,我应该使用其他东西吗?当我在 Firefox 中运行它时,它为小程序打开了一个框架。
    【解决方案2】:

    试试

        <applet code=TicTacToe.class 
        archive="ECPrototype.jar"
        width=120 height=120>
        </applet>
    

    (我假设这个类有你的 main(),jar 就是全部)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多