gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:48:42 25 4
gpt4 key购买 nike

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

我目前在 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 套代码。

我该怎么办?

最佳答案

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

关于java - 为 Windows 更改 JButton 的禁用前景(字体)颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16318537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com