我想为特定状态更改JComboBox的ArrowButton的前景色。
我尝试使用绘画工具覆盖默认键值。但它不起作用。
这是我的代码
UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\[Enabled].foregroundPainter", new ArrowPainter(new Color(255,255,255)));
UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\"[MouseOver].foregroundPainter", new ArrowPainter(new Color(255,255,255)));
有人可以帮我改变颜色吗?
这是我的ArrowPainter课
public class ArrowPainter implements Painter {
private float leftWidth;
private float topHeight;
private float centerWidth;
private float centerHeight;
private float rightWidth;
private float bottomHeight;
private float leftScale;
private float topScale;
private float centerHScale;
private float centerVScale;
private float rightScale;
private float bottomScale;
private Color new_color;
private Path2D path = new Path2D.Float();
private Object[] componentColors;
public ArrowPainter(Color new_color)
{
this.new_color = new_color;
}
@Override
public void paint(Graphics2D g, Object c, int width, int height) {
// TODO Auto-generated method stub
paintForegroundEnabled(g);
}
public void paintForegroundEnabled(Graphics2D g) {
path = decodePath5();
g.setPaint(decodeGradient9(path));
g.fill(path);
}
private Paint decodeGradient9(Shape s) {
// TODO Auto-generated method stub
Rectangle2D bounds = s.getBounds2D();
float x = (float)bounds.getX();
float y = (float)bounds.getY();
float w = (float)bounds.getWidth();
float h = (float)bounds.getHeight();
return decodeGradient((1.0f * w) + x, (0.5f * h) + y, (0.0f * w) + x, (0.5f * h) + y,
new float[] { 0.0f,0.5f,1.0f },
new Color[] { new_color,
decodeColor(new_color,new_color,0.5f),
new_color});
}
private Color decodeColor(Color color1, Color color2,
float midPoint) {
// TODO Auto-generated method stub
return new Color(deriveARGB(color1, color2, midPoint));
}
private int deriveARGB(Color color1, Color color2, float midPoint) {
// TODO Auto-generated method stub
int r = color1.getRed() +
Math.round((color2.getRed() - color1.getRed()) * midPoint);
int g = color1.getGreen() +
Math.round((color2.getGreen() - color1.getGreen()) * midPoint);
int b = color1.getBlue() +
Math.round((color2.getBlue() - color1.getBlue()) * midPoint);
int a = color1.getAlpha() +
Math.round((color2.getAlpha() - color1.getAlpha()) * midPoint);
return ((a & 0xFF) << 24) |
((r & 0xFF) << 16) |
((g & 0xFF) << 8) |
(b & 0xFF);
}
private Paint decodeGradient(float x1, float y1, float x2, float y2, float[] midpoints, Color[] colors) {
// TODO Auto-generated method stub
if (x1 == x2 && y1 == y2) {
y2 += .00001f;
}
return new LinearGradientPaint(x1, y1, x2, y2, midpoints, colors);
}
private double decodeY(float y) {
// TODO Auto-generated method stub
if (y >= 0 && y <= 1) {
return y * topHeight;
} else if (y > 1 && y < 2) {
return ((y-1) * centerHeight) + topHeight;
} else if (y >= 2 && y <= 3) {
return ((y-2) * bottomHeight) + topHeight + centerHeight;
} else {
throw new IllegalArgumentException("Invalid y");
}
}
private double decodeX(float x) {
// TODO Auto-generated method stub
if (x >= 0 && x <= 1) {
return x * leftWidth;
} else if (x > 1 && x < 2) {
return ((x-1) * centerWidth) + leftWidth;
} else if (x >= 2 && x <= 3) {
return ((x-2) * rightWidth) + leftWidth + centerWidth;
} else {
throw new IllegalArgumentException("Invalid x");
}
}
private Path2D decodePath5() {
// TODO Auto-generated method stub
path.reset();
path.moveTo(decodeX(0.9995915f), decodeY(1.3616071f));
path.lineTo(decodeX(2.0f), decodeY(0.8333333f));
path.lineTo(decodeX(2.0f), decodeY(1.8571429f));
path.lineTo(decodeX(0.9995915f), decodeY(1.3616071f));
path.closePath();
return path;
}
}
最佳答案
您尝试使用的UIManager方法仅适用于Nimbus外观。此处更多信息:http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html
问题是您没有在任何地方设置Nimbus的外观。
该代码对我有用,还请注意,这是一个MCVE而不是带有无关方法的整个项目:
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.Painter;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class Test {
public static void main (String [] args) throws InvocationTargetException, InterruptedException{
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\"[Enabled].foregroundPainter", new MyPainter());
UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\"[MouseOver].foregroundPainter", new MyPainter());
JComboBox comboBox = new JComboBox();
comboBox.addItem("one");
comboBox.addItem("two");
comboBox.addItem("three");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(comboBox);
frame.setSize(300, 300);
frame.setVisible(true);
}
});
}
public static class MyPainter implements Painter<Component>{
@Override
public void paint(Graphics2D g, Component object, int width, int height){
g.setColor(Color.RED);
g.fillRect(0, 0, width, height);
}
}
}