2013-05-01 103 views
4

我意识到这听起来很多很多已发布的问题,但请仔细阅读;这有一个看似相似的问题,但没有与许多已经提供的解决方案合作。更改JButton的被禁用的前景(字体)颜色为Windows

我目前使用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

设置按钮的背景色似乎是一个非常糟糕的主意(1),除非设计PLAF。话虽如此,为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 1)*“我更喜欢OS X配色方案”*如果您的用户偏爱OS X配色方案,他们可能会*使用* OS X! – 2013-05-01 13:01:27

+0

你可以给出一些你想要的颜色。并且请为SSCCE – Hydroid 2013-05-01 13:04:58

+1

为什么不使用JToggleButton呢? – Azad 2013-05-01 13:26:15

回答

4

enter image description here AFAIK为按钮JComponents和JTabbedPane中。 。 。 。 。 。 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(); 
      } 
     }); 
    } 
}