加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:55:51 所属栏目:Java 来源:网络整理
导读:我正在使用netbeans IDE开发 Java项目,我需要禁用特定的JButton.我使用以下代码. IssuBtn.setEnabled(false); 但是在禁用它之后它不会在JButton上显示文本.如何在JButton上保留该文本? 解决方法 这个实验表明一个答案是’使用非金属的PLAF’. import java.a
我正在使用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,2,2));
                pEnabled.setBackground(Color.green);
                gui.add(pEnabled,BorderLayout.NORTH);

                JPanel pDisabled = new JPanel(new GridLayout(1,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,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指出..

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

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

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

我猜第二个猜测.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读