Java小程序在eclipse中工作,而不是在其他地方工作
我对 java 很陌生,在过去 48 小时内一直试图让它工作,但我即将放弃。
我想把java小程序放在一个网站上。仅在日食中工作正常。我尝试了该网站上已经建议的许多解决方案,但没有一个对我有用,只是搞乱了代码,所以我将其恢复到原始状态。有人能指出问题所在吗?谢谢!
(编辑代码以反映建议的答案)
package nameapp;
import java.util.*;
import java.io.*;
import java.applet.Applet
public class NameApp extends Applet{
public static void main(String[] args) throws IOException {
String name;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is your name? ");
name = reader.readLine();
if (name.equals("Mike")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I am totally awesome!!!");
}
else if (name.equals("Lisa")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm the prettiest gal in the world!");
}
else {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm just ok i guess...");
}
}
}
html 是...
<applet code=nameapp/NameApp.class width=300 height=300>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.
I'm brand new to java and have tried to get this to work for the last 48 hours and I'm about to give up.
I want to put the java applet on a website. Works fine only in eclipse. I tried many solutions already suggested on this site and none of them worked for me and just mucked up the code so I've reverted it to my original state. Could anyone pinpoint the problem? Thanks!
(code edited to reflect suggested answers)
package nameapp;
import java.util.*;
import java.io.*;
import java.applet.Applet
public class NameApp extends Applet{
public static void main(String[] args) throws IOException {
String name;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is your name? ");
name = reader.readLine();
if (name.equals("Mike")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I am totally awesome!!!");
}
else if (name.equals("Lisa")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm the prettiest gal in the world!");
}
else {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm just ok i guess...");
}
}
}
And html is...
<applet code=nameapp/NameApp.class width=300 height=300>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
阅读本教程,其中包含有关 JApplet 的基本内容
请 使用JApplet而不是Applet
仔细阅读签名的java applet限制
Applet 可以做什么和不能做什么
https://stackoverflow.com/tags/java-web-start/info
注意 Java 1.6.025 对 JApplet 带来了另一个限制,并且安德鲁·汤普森在这个论坛上详细描述了这些问题和可能的解决方法,但我丢失了链接......
please read this tutorial that's contains basic stuff about JApplet,
please use JApplet not Applet
carrefully read signed java applet restrictions
What Applets Can and Cannot Do
https://stackoverflow.com/tags/java-web-start/info
note with Java 1.6.025 comings another restrictions for JApplet, and these problems and possible workaround are detailed described on this forum by Andrew Thompson, but I lost link ...
看起来您正在编写一个应用程序而不是一个小程序。当您在 eclipse 中运行它时,您是否选择 Run As...,然后选择 Java Application?尝试将其作为 Java Applet 运行。您应该会看到弹出的 appletviewer 中没有任何内容。
小程序的入口点是 init() 方法,而不是 main(),并且与图形相关的方法 Paint() 通常也被重载;我还没有见过可以访问标准输入和输出的小程序。
您可能会发现这里的 stackoverflow 问题很有用: Main 与 init。
It looks like you are writing an application rather than an applet. When you run it in eclipse, do you select Run As... and then select Java Application? Try running it as a Java Applet instead. You should see appletviewer pop up with no content in it.
The entry point for an applet is the init() method, not main(), and the graphics related method paint() is also usually overloaded; I haven't seen an applet that has access to standard in and out.
You might find the stackoverflow question here useful: Main vs init.
Put它还需要扩展
java.applet.Applet
并且通常是一个applet。Put it would also need to extend
java.applet.Applet
and generally be an applet.阅读 Applet 教程 - 而不是
public static void main(String[] args)
方法,Applet 需要public void init()
。另外,对于您现在拥有的代码,您只会看到一个空白的 Applet - 您必须启动 Java 调试控制台才能看到由
System.out.println()
打印的任何内容,并且Applet 无法访问 System.in 来读取输入 - 相反,您需要向 Applet 添加一些 TextField 组件,并使用这些组件读取和写入文本。Read the Applet tutorial - Instead of a
public static void main(String[] args)
method, an Applet needspublic void init()
.Also, with the code you have now, you'll just see a blank Applet - you'll have to have the Java Debug Console up to see anything printed by
System.out.println()
, and Applets can't accessSystem.in
to read input - instead you'll want to add some TextField components to your Applet and read and write the text with those.