且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-29 15:49:16

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

This experiment suggests one answer is 'Use a PLAF that is not Metal'.

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 中的值。

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指出..


As pointed out by kleopatra..


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

it's not a solution but might be a pointer to the direction to search for a solution

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

Where 'it' is my answer. In fact, I suspect she hit upon the real cause with the comment:


仅猜测:这是因为违反了一个 - 只有规则。

guessing only: here it's due to violating the one-plaf-only rule.

我猜的第二个。