创建和运行 Java 小程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5034233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating and running a Java applet
提问by headscratch
I thought I would try my hand at applets - I made an applet using Eclipse. It runs fine using the Run As -> Java Applet.
我想我会尝试使用小程序 - 我使用 Eclipse 制作了一个小程序。它使用 Run As -> Java Applet 运行良好。
I read a bit about running it outside Eclipse, so I did the following:
我阅读了一些关于在 Eclipse 之外运行它的信息,因此我执行了以下操作:
- Made a folder.
- Created New -> Java Project [applet_test].
- Inside the project, I created New -> Other -> Visual Swing Class -> Applet [Number1] - that created
Number1.class
. - Added code and ran it as a Java applet - it ran fine.
- Exported the project as a JARfile (not a runnable JAR file).
- Wrote HTML using TextEdit(Mac's version of Windows' Notepad). The HTML follows, below...
- I put the JAR file, HTML and .class file in the folder.
- In Terminal (Mac's version of Windows command prompt window), I ran Appletviewer applet_testX2.html (that's the name of my HTML).
- I could see a brief flash of the application name at the top of the screen (as would any other running application).
- 做了一个文件夹。
- 新建 -> Java 项目 [applet_test]。
- 在项目中,我创建了 New -> Other -> Visual Swing Class -> Applet [Number1] - 创建了
Number1.class
. - 添加了代码并将其作为 Java 小程序运行 - 它运行良好。
- 将项目导出为JAR文件(不是可运行的 JAR 文件)。
- 使用TextEdit(Mac 版本的 Windows记事本)编写 HTML 。HTML如下,如下...
- 我将 JAR 文件、HTML 和 .class 文件放在文件夹中。
- 在终端(Mac 版本的 Windows 命令提示窗口)中,我运行 Appletviewer applet_testX2.html(这是我的 HTML 的名称)。
- 我可以在屏幕顶部看到应用程序名称的简短闪烁(与任何其他正在运行的应用程序一样)。
However, the application (which should display a Jpanel with a label and a button) did NOT appear. I also tried running it from Firefox and Safari. Only the HTML code appeared.
但是,应用程序(应该显示带有标签和按钮的 Jpanel)没有出现。我还尝试从 Firefox 和 Safari 运行它。只出现了 HTML 代码。
So, what am I doing wrong? And, more importantly, how do I do it correctly?
那么,我做错了什么?而且,更重要的是,我该如何正确地做到这一点?
Code follows without imports statements:
代码如下,没有导入语句:
<html>
<body>
<applet code="Number1.class" archive="applet_test.jar"
width=300
height=300>
</applet>
</body>
</html>
The Java code:
Java代码:
public class Number1 extends JApplet {
public Number1() {
}
private static final long serialVersionUID = 1L;
@Override
public void init() {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
initComponents();
}
});
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
setSize(320, 240);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
JLabel lblAppletTest = new JLabel("Applet test 1");
panel.add(lblAppletTest);
JButton btnPushIt = new JButton("Push it");
panel.add(btnPushIt);
}
}
Firefox source view:
火狐源视图:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Author" content="BT">
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1038.35">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica; min-height: 19.0px}
</style>
</head>
<body>
<p class="p1"><html></p>
<p class="p1"><span class="Apple-converted-space"> </span><body></p>
<p class="p1"><span class="Apple-converted-space"> </span><applet code="Number1.class" archive="applet_test.jar"</p>
<p class="p2"><br></p>
<p class="p1"><span class="Apple-converted-space"> </span>width=300</p>
<p class="p1"><span class="Apple-converted-space"> </span>height=300></p>
<p class="p1"></applet></p>
<p class="p1"></body></p>
<p class="p1"></html></p>
</body>
</html>
回答by Hovercraft Full Of Eels
My guess is that here:
我的猜测是这里:
<applet code="Number1.class" archive="applet_test.jar"
you're not taking packages into consideration. For instance, if the package is myPackage.vol3 then the line should read
你没有考虑包裹。例如,如果包是 myPackage.vol3,则该行应为
<applet code="myPackage.vol3.Number1.class" archive="applet_test.jar"
But if this doesn't help, you'll want to extract any error messages that the browser gives you and edit your original post to show us what they are.
但是,如果这没有帮助,您将需要提取浏览器提供给您的任何错误消息并编辑您的原始帖子以向我们展示它们是什么。
回答by Ameer Chand
Using Appletviewer
------------------
Write code of Applet.
If you installed tomcat in D:
code
编写Applet 的代码。
如果您在 D 中安装了 tomcat:
代码
-
——
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyApplet extends Applet
{
public void init()
{
System.out.println("init intilize");
GridLayout g=new GridLayout(4,6,0,0);
setLayout(g);
MyListener m=new MyListener();
for(int i=1;i<=12;i++)
{
Button b=new Button("ok"+i);
add(b);
b.addActionListener(m);
}
}//end of init
public void start()
{
System.out.println("applet started");
}//end of start
public void stop()
{
System.out.println("applet stop");
}//end of
public void paint(Graphics g)
{
g.drawString("Naveed",200,25);
g.drawOval(20,30,30,20);
System.out.println("applet paint");
}//end of start
public void destroy()
{
System.out.println("applet destroy");
}//end of start
}
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("button clicked");
}//end of actionPerformed
}
Now save this code in D:, not in sub folder.
现在将此代码保存在 D: 中,而不是子文件夹中。
First Compile it.
首先编译它。
open the cmd
打开cmd
cd D:
cd D:
type
类型
`javac MyApplet.java -d classpath D:\Tomcat\common\lib\servlet.jar`
This will make a MyApplet.classfile
这将创建一个MyApplet.class文件
Now make a html file.
现在制作一个html文件。
<html>
<body>
<applet code="Number1.class" width=30 height=300 > </applet>
</body>
</html>
Save with the name of you want let's say app.html
用你想要的名字保存让我们说app.html
run the html file now.
现在运行 html 文件。
In the cmd window
在 cmd 窗口中
appletviewer app.html
appletviewer app.html
Output will be in front of you.
输出将在您面前。