我正在尝试使用 Java 中的 Swing UI 在按下鼠标按钮时更改按钮背景颜色和文本(前景)颜色。我的主要课程非常简单明了:
public class Main {
public static void main(String[] args) {
SynthLookAndFeel laf = new SynthLookAndFeel();
try {
laf.load(Main.class.getResourceAsStream("/styles.xml"), Main.class);
} catch (ParseException e) {
e.printStackTrace();
}
try {
UIManager.setLookAndFeel(laf);
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Not hello world");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocation(50, 50);
frame.setLayout(new FlowLayout());
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.setVisible(true);
}
}
加载的 XML Synth 样式文件如下:
<synth>
<style id="basicStyle">
<font name="Verdana" size="16"/>
<state>
<color value="#eeeeee" type="BACKGROUND"/>
<color value="#000000" type="FOREGROUND"/>
</state>
</style>
<bind style="basicStyle" type="region" key=".*"/>
<style id ="button_style">
<state value="PRESSED">
<color value="#666666" type="FOREGROUND" />
<color value="#aaaaaa" type="BACKGROUND" />
</state>
</style>
<bind style="button_style" type="region" key="button"/>
但我得到的只是屏幕上带有黑色文本和灰色背景的按钮。按下按钮时,没有任何反应:
我有什么办法可以实现这种行为?