如何修复小程序参数
仅当我移动“setBackground(color);”时,我的程序才能正常工作到“init()”。如何修复它? 我的任务是传入与 RGB 值相对应的三个整数,并使用适当的 Color 构造函数来设置背景颜色。
import java.applet.*;
import java.awt.*;
public class AnAppletSubclass2b extends Applet {
int r;
int g;
int b;
Color color;
public void init()
{
String parmStringRED = getParameter("red");
r = Integer.parseInt(parmStringRED);
String parmStringGREEN = getParameter("green");
g = Integer.parseInt(parmStringGREEN);
String parmStringBLUE = getParameter("blue");
b = Integer.parseInt(parmStringBLUE);
Color color = new Color (r,g,b);
System.out.println("The parameter are: red: " + parmStringRED + ", green: " + parmStringGREEN + ", blue: " + parmStringBLUE);
}
public void paint(Graphics g)
{
setBackground(color);
System.out.println("In paint: n = " + n);
n++;
}
int n;
}
<HTML>
<HEAD>
<TITLE> AnApplet With Parms </TITLE>
</HEAD>
<BODY>
<p>Here is the output of my Program:</p>
<APPLET CODE="AnAppletSubclass2b.class" WIDTH=150 HEIGHT=100>
<PARAM name="red" value="25" >
<PARAM name="green" value="250">
<PARAM name="blue" value="120">
</APPLET>
</BODY>
</HTML>
my program works fine only if i move "setBackground(color);" to "init()". how to fix it ?
my task is to Pass in three integers corresponding to an RGB value and use the appropriate Color constructor to set the background color.
import java.applet.*;
import java.awt.*;
public class AnAppletSubclass2b extends Applet {
int r;
int g;
int b;
Color color;
public void init()
{
String parmStringRED = getParameter("red");
r = Integer.parseInt(parmStringRED);
String parmStringGREEN = getParameter("green");
g = Integer.parseInt(parmStringGREEN);
String parmStringBLUE = getParameter("blue");
b = Integer.parseInt(parmStringBLUE);
Color color = new Color (r,g,b);
System.out.println("The parameter are: red: " + parmStringRED + ", green: " + parmStringGREEN + ", blue: " + parmStringBLUE);
}
public void paint(Graphics g)
{
setBackground(color);
System.out.println("In paint: n = " + n);
n++;
}
int n;
}
<HTML>
<HEAD>
<TITLE> AnApplet With Parms </TITLE>
</HEAD>
<BODY>
<p>Here is the output of my Program:</p>
<APPLET CODE="AnAppletSubclass2b.class" WIDTH=150 HEIGHT=100>
<PARAM name="red" value="25" >
<PARAM name="green" value="250">
<PARAM name="blue" value="120">
</APPLET>
</BODY>
</HTML>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个颜色变量定义为字段,另一个定义为局部变量。更改
为
you have a color variable defined as a field and another one defined as a local. Change
to