【问题标题】:How to change button text color and background color while button is pressed按下按钮时如何更改按钮文本颜色和背景颜色
【发布时间】:2020-11-07 06:37:27
【问题描述】:

我正在尝试使用 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"/>

但我得到的只是屏幕上带有黑色文本和灰色背景的按钮。按下按钮时,没有任何反应:

有什么方法可以实现这样的行为?

【问题讨论】:

    标签: java swing synth


    【解决方案1】:

    您的Synth 文件中有一些问题:

    1. 它没有被&lt;/synth&gt;关闭

    2. 如果您没有在其中设置opaque="true",默认情况下您的JButton 将是透明的(即您的背景将不可见)。

    3. 我不完全确定为什么,但是要让前景字体颜色在状态更改时更改,您需要使用 TEXT_FOREGROUND 而不是 FOREGROUND 键。

    我更改了您文件中的一些颜色,因为它们不太明显,但您的文件应该类似于:

    <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">
            <opaque value="true" />
            <insets top="4" left="4" right="4" bottom="4" />
            <state>
                <color value="blue" type="TEXT_FOREGROUND" />
                <color value="#ffffff" type="BACKGROUND" />
            </state>
            <state value="PRESSED">
                <color value="white" type="TEXT_FOREGROUND" />
                <color value="#aaaaaa" type="BACKGROUND" />
            </state>
        </style>
        <bind style="button_style" type="region" key="button" />
    </synth>
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2021-02-23
      • 1970-01-01
      • 2013-10-24
      • 2015-06-04
      相关资源
      最近更新 更多