为Windows更改JButton禁用状态下的前景(字体)颜色

4

我知道这听起来很像已经发布的很多问题,但请继续阅读;这个问题看似相似,但已有许多提供的解决方案都无法解决。

我目前在使用Java和Eclipse,在OS X和Windows上进行编写。在OS X上,我有一个JButton,它从.setEnabled(true)变为.setEnabled(false)。当它被禁用时,我通过.setBackground(someColor)改变其背景颜色。在此期间,前景(字体)颜色不会改变,保持黑色。这正是我想要的,它现在非常完美。

然后就出现了Windows的问题。与上述情况相同,我有一个JButton,它从.setEnabled(true)变为.setEnabled(false)。我还通过.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个代码集。

我该怎么办?


2
设置按钮的背景颜色似乎是个很糟糕的想法(1),除非设计一个PLAF。尽管如此,为了更快地获得帮助,请发布一个SSCCE。1)“我更喜欢OS X的颜色方案”如果你的用户喜欢OS X的颜色方案,他们可能会选择使用OS X! - Andrew Thompson
也许你应该明确指定你想要的颜色。并且请提供一个最小化、完整可运行的示例(SSCCE)。 - Hydroid
1
为什么不使用 JToggleButton 呢? - Azad
2个回答

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();
            }
        });
    }
}

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();
            }
        });
    }
}

当然,他的代码 - 显然 - 只是一些尝试的片段,必须嵌入到完整的程序中。 - Luatic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接