使用Windows LAF时按钮的背景色? [英] Background color of button when using Windows LAF?
问题描述
我有一个使用本机LAF的Java应用程序,如下所示:
I have a Java application which uses the native LAF like so:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
这很好用,但是,我试图使按钮的背景为红色,但最终却像这样:
This is working great, however, I am trying to make a button have a red background, but ends up like this:
如您所见,我在按钮上设置了背景和前景,但是结果并不令人满意. 有没有一种方法可以使按钮在不继承JButton的情况下绘制红色背景?
As you can see, I'm setting background and foreground on the button, but what results is not visually pleasing. Is there a way to have the button draw a red background without subclassing JButton?
推荐答案
对于遇到我所遇到问题的任何人,这是我所采用的解决方案:
For anybody who comes along the problem I did, here's the solution I went with:
我转向使用ImageIcon作为按钮的子项添加的图像:
I switched to using an image added as a child of the button using ImageIcon:
BufferedImage stopPicture = null;
try {
stopPicture = ImageIO.read(new File("stop.png"));
} catch (IOException ex) { }
JLabel picLabel = new JLabel(new ImageIcon( stopPicture ));
JButton btnStop = new JButton("");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SerialTest.getInstance().stopMoving();
}
});
btnStop.add(picLabel);
这篇关于使用Windows LAF时按钮的背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!