Java 如何将值传递给构造函数并从同一类中的方法使用它?
如何将颜色变量传递给下面的类?我想以RGB(-155)格式传递给代码变量: 我收到错误“iconColor无法解析为变量”Java 如何将值传递给构造函数并从同一类中的方法使用它?,java,class,graphics,icons,Java,Class,Graphics,Icons,如何将颜色变量传递给下面的类?我想以RGB(-155)格式传递给代码变量: 我收到错误“iconColor无法解析为变量” 谢谢。您需要将构造函数中的iconColor存储到,或者: 实例变量允许您存储封装的“状态”,您可以在外部代码独立调用的方法中读取和写入该状态。我建议你通过阅读来了解更多关于课程结构的知识 然而,在你们班上,我不确定我是否会这样组织它。最好在构造函数内尝试Color.decode调用,并将字段存储为Color——这样,如果存在NumberFormatException,则会
谢谢。您需要将构造函数中的
iconColor
存储到,或者:
实例变量允许您存储封装的“状态”,您可以在外部代码独立调用的方法中读取和写入该状态。我建议你通过阅读来了解更多关于课程结构的知识
然而,在你们班上,我不确定我是否会这样组织它。最好在构造函数内尝试Color.decode
调用,并将字段存储为Color
——这样,如果存在NumberFormatException
,则会立即通知调用者,而不是在调用paintIcon
时的某个稍后(任意)点:
public class ColorIcon implements Icon {
private final Color iconColor; // add an instance variable/field - see footnote †
ColorIcon(String nm) {
this.iconColor = Color.decode(nm); // set the field
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(this.iconColor); // added "this" for clarity
g.drawRect(7, 5, 11, 11);
g.fillRect(7, 5, 11, 11);
}
/*the rest of your class*/
}
†似乎您永远不会更改实例变量
iconColor
我也将其设置为final
,以防止在其他点发生意外突变-如果需要将iconColor存储在实例变量中,您可以删除此项
package com.epox.cmc.core.util;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
public class ColorIcon implements Icon {
String iconColor ;
ColorIcon(String iconColor) {
this.iconColor = iconColor;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.decode(iconColor)); //<-- This is the problem
g.drawRect(7, 5, 11, 11);
g.fillRect(7, 5, 11, 11);
}
@Override
public int getIconWidth() {
return 16;
}
@Override
public int getIconHeight() {
return 16;
}
}
package com.epox.cmc.core.util;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Graphics;
导入javax.swing.Icon;
公共类ColorIcon实现Icon{
字符串颜色;
颜色图标(字符串图标颜色){
this.iconColor=iconColor;
}
@凌驾
公共虚空绘制图标(组件c、图形g、整数x、整数y){
g、 setColor(Color.decode(iconColor))//
public class ColorIcon implements Icon {
private final String iconColor; // add an instance variable/field - see footnote †
ColorIcon(String iconColor) {
this.iconColor = iconColor; // set the field
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.decode(this.iconColor)); // added "this" for clarity
g.drawRect(7, 5, 11, 11);
g.fillRect(7, 5, 11, 11);
}
/*the rest of your class*/
}
public class ColorIcon implements Icon {
private final Color iconColor; // add an instance variable/field - see footnote †
ColorIcon(String nm) {
this.iconColor = Color.decode(nm); // set the field
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(this.iconColor); // added "this" for clarity
g.drawRect(7, 5, 11, 11);
g.fillRect(7, 5, 11, 11);
}
/*the rest of your class*/
}
package com.epox.cmc.core.util;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
public class ColorIcon implements Icon {
String iconColor ;
ColorIcon(String iconColor) {
this.iconColor = iconColor;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.decode(iconColor)); //<-- This is the problem
g.drawRect(7, 5, 11, 11);
g.fillRect(7, 5, 11, 11);
}
@Override
public int getIconWidth() {
return 16;
}
@Override
public int getIconHeight() {
return 16;
}
}