您好,有没有一种方法可以像我使用此 LabelField 那样重写绘制,以仅更改 ObjectChoiceField 的标题颜色。
LabelField f = new LabelField("Title "){
protected void paint(Graphics g) {
g.setColor(Color.RED);
super.paint(g);
}
};
我想更改“标题”的字体颜色:
final String[] options={"1","2","3"};
final ObjectChoiceField ocf=new ObjectChoiceField("Title",options);
最佳答案
您的操作几乎与 LabelField
的操作完全一样。您可以重写paint()
并调用Graphics#setColor()
:
public class CustomColorChoiceField extends ObjectChoiceField {
public CustomColorChoiceField(String label, Object[] choices, int initialIndex) {
super("title", choices, initialIndex);
}
protected void paint(Graphics graphics) {
int oldColor = graphics.getColor();
graphics.setColor(Color.GREEN);
super.paint(graphics);
graphics.setColor(oldColor);
}
}
然后以正常方式将其添加到屏幕:
add(new CustomColorChoiceField("title", choices1, 0));
注意:实际上我通常不以这种方式使用ObjectChoiceField
。我几乎总是以 ""
(空字符串)作为标题。如果我确实想要标题/标签之类的内容,我通常会创建一个LabelField
,然后将其准确放置在我想要的位置。因此,如果您这样做,则根本不需要创建自己的 ObjectChoiceField
子类。只需将一个空字符串作为标题/标签传递到选择字段中,然后创建一个彩色 LabelField
,如您在问题中所示。
如果您有兴趣更改颜色或选项本身的其他属性(不是标题/标签),则 see this recent Stack Overflow question ,或check out a blog post I wrote on this a long time ago .
关于java - 如何更改对象选择字段标题的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20130999/