如何禁用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指出..
我的答案在哪里’它’.事实上,我怀疑她通过评论找到了真正的原因:
我猜第二个猜测. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Java ScriptEngine支持的语言
- [译]Introduction to Concurrency in Spring Boot
- java – 清理finalize()或finally()中的代码?
- java – 无法加载’类路径资源[org / springframework / ws
- java – 从InputStream读取并写入OutputStream
- self attention pytorch代码
- java – (Generics)不能对非静态类型T进行静态引用
- 全面解析Java设计模式之单例模式
- java – 如何使用JSF和Facelets实现一些if-then逻辑?
- java – 运行Google Compute Engine实例的GAE开发服务器