4

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

我目前在 OS X 和 Windows 上使用 Eclipse 编写 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 组代码。

我应该怎么办?

4

2 回答 2

4

在此处输入图像描述 . . . . . .在此处输入图像描述

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();
            }
        });
    }
}
于 2013-05-01T13:39:11.093 回答
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();
            }
        });
    }
}
于 2015-07-01T06:05:31.677 回答