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

时间:2011-10-03 04:40:04

标签: java swing jbutton

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

IssuBtn.setEnabled(false);

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

1 个答案:

答案 0 :(得分:12)

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

Look Of Disabled Buttons

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 tweak

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指出的那样..

  

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

我的答案在哪里。事实上,我怀疑她通过评论找到了真正的原因:

  

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

我猜错了。