Java 更改JButton';s已禁用Windows的前景(字体)颜色
我意识到这听起来很像已经发布的许多问题,但请继续阅读;这似乎有一个类似的问题,但在已经提供的许多解决方案中都不起作用 我目前正在用Java编写OSX和Windows上的Eclipse。对于OSX,我有一个从.setEnabled(true)到.setEnabled(false)的JButton。当这种情况发生时,当它被禁用时,我通过.setBackground(someColor)更改它的背景色。在发生这种情况的整个过程中,前景(字体)颜色不会改变并保持黑色。这就是我想要的,这是完美的方式 然后是Windows的问题。和上面一样,我有一个JButton,它从.setEnabled(true)到.setEnabled(false)。我还通过.setBackground(someColor)更改了它的背景。但是,当这种情况发生时,前景(字体)颜色不会保持不变–它会从黑色变为浅灰色。这非常不方便,并且使用新的背景色很难阅读 所以问题是:如何更改禁用的JButton的前景色 我已经尝试了以下方法:Java 更改JButton';s已禁用Windows的前景(字体)颜色,java,swing,colors,jbutton,uimanager,Java,Swing,Colors,Jbutton,Uimanager,我意识到这听起来很像已经发布的许多问题,但请继续阅读;这似乎有一个类似的问题,但在已经提供的许多解决方案中都不起作用 我目前正在用Java编写OSX和Windows上的Eclipse。对于OSX,我有一个从.setEnabled(true)到.setEnabled(false)的JButton。当这种情况发生时,当它被禁用时,我通过.setBackground(someColor)更改它的背景色。在发生这种情况的整个过程中,前景(字体)颜色不会改变并保持黑色。这就是我想要的,这是完美的方式 然后
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);
按钮。设置前景(颜色。黑色);
按钮.setText(按钮);
UIManager.put(“按钮.禁用文本”,颜色.黑色);
UIManager.getDefaults().put(“Button.disabledText”,Color.BLACK);
UIManager.put(“按钮.前景”,颜色.黑色);
UIManager.getDefaults().put(“Button.foreground”,Color.BLACK);
这些都不管用。我还尝试了以下方法:
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);
- 将OSX导出到.jar文件,然后在Windows中使用它。Windows字体颜色仍在更改
- 编译适用于OS X的.app文件和适用于Windows的.exe文件。问题仍然存在
- AFAIK用于按钮JComponents和JTabbedPane,仅使用Html
- 还是要推翻
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.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
公共类HtmlAndJButton{
最后一个字符串buttonText=“不管用什么词,
但不明智”;
最后一个字符串buttonext1=“不管用什么词,
但都不明智,”
+“
加上第一行,”;
最后一个字符串buttonext2=“不管用什么词,
但都不明智,”
+“
加上第一行,
加上第二行,”;
私有JButton btn1=新JButton(“切换”);
私有JButton按钮=新JButton(buttonText);
私有JButton button1=新JButton(“切换”);
私有JButton button2=新JButton(“切换”);
公共HtmlAndJButton(){
btn1.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
button.setText(“+buttonext+”);
button.setEnabled(!button.isEnabled());
button1.setText(“+buttonext1+”);
button1.setEnabled(!button1.isEnabled());
button2.setText(“+buttonext2+”);
button2.setEnabled(!button2.isEnabled());
}
});
button.setText(“+buttonext+”);
button1.setText(“+buttonext1+”);
button2.setText(“+buttonext2+”);
JFrame f=新JFrame(“按钮测试”);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 setLayout(新的GridLayout(2,2));
f、 添加(按钮);
f、 添加(按钮1);
f、 添加(按钮2);
f、 添加(btn1);
f、 包装();
f、 setLocationRelativeTo(空);
f、 setVisible(真);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
HtmlAndJButton t=新的HtmlAndJButton();
}
});
}
}
您的Java版本是什么??因为,在我尝试了您的示例代码之后,它就不起作用了。我使用了Java1.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();
}
});
}
}
设置按钮的背景颜色似乎是一个非常糟糕的主意(1),除非设计一个PLAF。话虽如此,为了更快地获得更好的帮助,请发布一篇文章。1) “我更喜欢OSX配色方案”如果您的用户更喜欢OSX配色方案,他们可能会使用OSX!也许你应该给我你想要的颜色。还有,为什么你不使用
JToggleButton
来代替呢?当然,他的代码——显然——只是一个显示一些尝试的片段,必须嵌入到完整的程序中。