更改Windows的JButton的禁用前景(字体)颜色

时间:2013-05-01 12:58:50

标签: java swing colors jbutton uimanager

我意识到这听起来像很多已经发布的问题,但请继续阅读;这有一个看似相似的问题,但还没有与已经提供的许多解决方案一起使用。

我目前正在使用Eclipse在OS X和Windows上编写Java。在OS X中,我有一个从.setEnabled(true)到.setEnabled(false)的JButton。当发生这种情况时,因为它被禁用,我通过.setBackground(someColor)更改其背景颜色。发生这种情况的整个时间,前景(字体)颜色不会改变并保持黑色。这就是我想要的,它是完美的。

然后是Windows的问题。与上面相同,我有一个从.setEnabled(true)到.setEnabled(false)的JButton。我也通过.setBackground(someColor)更改其背景。但是,当发生这种情况时,前景(字体)颜色不会保持不变 - 它会从黑色变为浅灰色。这非常不方便,并且使用新的背景颜色很难阅读。

所以问题是:如何更改禁用的JButton的前景色?

我已经尝试了以下内容:

button.setForeground(Color.BLACK);

button.setText(<html><font color = black>BUTTON</font></html>);

UIManager.put("Button.disabledText", Color.BLACK);

UIManager.getDefaults().put("Button.disabledText", Color.BLACK);

UIManager.put("Button.foreground", Color.BLACK);

UIManager.getDefaults().put("Button.foreground", Color.BLACK);

没有一个有效。我也尝试了以下内容:

  • 将OS X导出到.jar文件,然后在Windows中使用它。 Windows字体颜色仍然会发生变化。
  • 为OS X编译.app文件,为Windows编译.exe文件。问题仍然存在。

还有其他任何我忽略的解决方案吗?

目前,我已经将字体保留为当前难看的灰色,并将背景(由于某种原因,仍然可以通过.setBackground()更改)更改为可容纳的其他颜色它

那为什么OS X和Windows之间似乎存在这种色差?我更喜欢OS X配色方案,但我真的不想拥有两组基本相同程序的代码。

我该怎么办?

2 个答案:

答案 0 :(得分:4)

enter image description here。 。 。 。 。 。enter image description here

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class HtmlAndJButton {

    final String buttonText = " Whatever words, <br> but nothing wise";
    final String buttonText1 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, ";
    final String buttonText2 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, <br> plus 2nd. line,";
    private JButton btn1 = new JButton("Toggle");
    private JButton button = new JButton(buttonText);
    private JButton button1 = new JButton("Toggle");
    private JButton button2 = new JButton("Toggle");

    public HtmlAndJButton() {
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.setText("<html><font color=" + (button.isEnabled()
                        ? "blue" : "red") + ">" + buttonText + "</font></html>");
                button.setEnabled(!button.isEnabled());
                button1.setText("<html><font color=" + (button1.isEnabled()
                        ? "red" : "green") + ">" + buttonText1 + "</font></html>");
                button1.setEnabled(!button1.isEnabled());
                button2.setText("<html><font color=" + (button2.isEnabled()
                        ? "green" : "yellow") + ">" + buttonText2 + "</font></html>");
                button2.setEnabled(!button2.isEnabled());
            }
        });
        button.setText("<html><font color=red>" + buttonText + "</font></html>");
        button1.setText("<html><font color=green>" + buttonText1 + "</font></html>");
        button2.setText("<html><font color=yellow>" + buttonText2 + "</font></html>");
        JFrame f = new JFrame("ButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        f.add(button);
        f.add(button1);
        f.add(button2);
        f.add(btn1);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                HtmlAndJButton t = new HtmlAndJButton();
            }
        });
    }
}

答案 1 :(得分:0)

你的Java版本是什么?因为,在我尝试您的示例代码之后,它不起作用。我使用Java 1.7并且它不起作用。

试试这个......

public class TestEntryPoint {

    private JButton btn1 = new JButton("Test");
    private JButton button1 = new JButton("Toggle");

    public TestEntryPoint() {

        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                button1.setEnabled(!button1.isEnabled());

            }
        });
        UIManager.put("Button.disabledText", Color.red);
        JFrame f = new JFrame("ButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        f.add(button1);
        f.add(btn1);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestEntryPoint();
            }
        });
    }
}
相关问题