如何禁用JButton而不隐藏其标签?

我正在使用netbeans IDE开发Java项目,我需要禁用特定的JButton。 我使用以下代码。

IssuBtn.setEnabled(false); 

但是在禁用它之后它不会在JButton上显示文本。 如何在JButton上保留该文本?

这个实验表明一个答案是’使用非金属的PLAF’。

看残疾按钮

 import java.awt.*; import javax.swing.*; class LookOfDisabledButton { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JPanel gui = new JPanel(new BorderLayout(3,3)); JPanel pEnabled = new JPanel(new GridLayout(1,0,2,2)); pEnabled.setBackground(Color.green); gui.add(pEnabled, BorderLayout.NORTH); JPanel pDisabled = new JPanel(new GridLayout(1,0,2,2)); pDisabled.setBackground(Color.red); gui.add(pDisabled, BorderLayout.SOUTH); UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels(); for (UIManager.LookAndFeelInfo plafInfo : plafs) { try { UIManager.setLookAndFeel(plafInfo.getClassName()); JButton bEnabled = new JButton(plafInfo.getName()); pEnabled.add(bEnabled); JButton bDisabled = new JButton(plafInfo.getName()); bDisabled.setEnabled(false); pDisabled.add(bDisabled); } catch(Exception e) { e.printStackTrace(); } } JOptionPane.showMessageDialog(null, gui); } }); } } 

或者,调整UIManager的值。

UIManager调整

 import java.awt.*; import javax.swing.*; class LookOfDisabledButton { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JPanel gui = new JPanel(new BorderLayout(3,3)); JPanel pEnabled = new JPanel(new GridLayout(1,0,2,2)); pEnabled.setBackground(Color.green); gui.add(pEnabled, BorderLayout.NORTH); JPanel pDisabled = new JPanel(new GridLayout(1,0,2,2)); pDisabled.setBackground(Color.red); gui.add(pDisabled, BorderLayout.SOUTH); // tweak the Color of the Metal disabled button UIManager.put("Button.disabledText", new Color(40,40,255)); UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels(); for (UIManager.LookAndFeelInfo plafInfo : plafs) { try { UIManager.setLookAndFeel(plafInfo.getClassName()); JButton bEnabled = new JButton(plafInfo.getName()); pEnabled.add(bEnabled); JButton bDisabled = new JButton(plafInfo.getName()); bDisabled.setEnabled(false); pDisabled.add(bDisabled); } catch(Exception e) { e.printStackTrace(); } } JOptionPane.showMessageDialog(null, gui); } }); } } 

正如kleopatra指出..

它不是解决方案,但可能是指向搜索解决方案的指针

哪个’它’是我的答案。 事实上,我怀疑她在评论中找到了真正的原因:

仅猜测:这是因为违反了仅限于一条平台的规则。

我猜第二个猜测。