gpt4 book ai didi

html 中的 Java 小程序

转载 作者:行者123 更新时间:2023-12-01 14:17:33 26 4
gpt4 key购买 nike

当我使用 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();
}
}

最佳答案

找到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>
<小时/>

关于框架问题,尝试重写constructor、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);
}

关于html 中的 Java 小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17984982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com