applet带外部参数问题

lcrain428 2010-11-23 04:11:55
程序代码如下:

//HelloWorld.java
import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet{
private String title;
private int size;
private int color;
public void init(){
title="Hello World!";
size=Integer.parseInt(getParameter("size"));
color=Integer.parseInt(getParameter("color"));
}
public void paint(Graphics g){
Color c=new Color(color);
Font f=new Font("",1,size);
g.getColor(c);
g.getFont(f);
g.drawString("Hello World!", 30, 30);
}
}

//HelloWorld.html
<html>
<applet code="HelloWorld.class" width=400 height=150 codebase="./bin">
<param name=size value=40>
<param name=color value=008800>
</applet>
</html>

用eclipse运行就失败。
原因我觉得是这样的:HelloWorld.java先调用外部参数,生成HelloWorld.class,但是此时没有外部参数,
导致init()失败,问怎么修改,还是我java配置不当?其他的java程序都能运行起来
...全文
110 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcrain428 2010-11-23
  • 打赏
  • 举报
回复
lcrain428 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dyllove98 的回复:]

引用 4 楼 lcrain428 的回复:

引用 3 楼 houjin_cn 的回复:

Java code
这样能部分解决问题,但还是报错
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Int……
[/Quote]

代码都在上面摆着了,没有传值正确是必然的,从html得到要传的值,但是编译java时又不调用html,没有传值当然不行了。命令行我都不知道为什么会通过??
Jlins 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lcrain428 的回复:]

引用 3 楼 houjin_cn 的回复:

Java code
这样能部分解决问题,但还是报错
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at HelloWorld.init(HelloWorld.java:14)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
[/Quote]


你肯定没有传值正确
lcrain428 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 houjin_cn 的回复:]

Java code

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet{
private String title;
private int size;
private int color;

public void init(){
t……
[/Quote]

这样能部分解决问题,但还是报错
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at HelloWorld.init(HelloWorld.java:14)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
程序能运行了。
我觉得这是eclipse设置的问题,应该有别的方法解决。

我的代码修改为“setFont”后能够通过命令行正常运行。
houjin_cn 2010-11-23
  • 打赏
  • 举报
回复

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet{
private String title;
private int size;
private int color;

public void init(){
title="Hello World!";
size=12;
color=0xFF0000;
try{
size=Integer.parseInt(getParameter("size"));
color=Integer.parseInt(getParameter("color"));
}
catch(Exception ex){
ex.printStackTrace();
}
}

public void paint(Graphics g){
Color c=new Color(color);
Font f=new Font("宋体",Font.PLAIN,size);
g.setColor(c);
g.setFont(f);
g.drawString("Hello World!",30,30);
}
}
lcrain428 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 houjin_cn 的回复:]

那就 try catch 起下面这两行,并给size和color设个默认值吧
size=Integer.parseInt(getParameter("size"));
color=Integer.parseInt(getParameter("color"));
[/Quote]
这个....不懂
houjin_cn 2010-11-23
  • 打赏
  • 举报
回复
那就 try catch 起下面这两行,并给size和color设个默认值吧
size=Integer.parseInt(getParameter("size"));
color=Integer.parseInt(getParameter("color"));

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧