本文介绍了为什么setBackground到JButton不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下简单代码:
btn = new JButton();
btn.setBackground(backgroundColor)
我使用时工作:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
但在我评论上述行后,它停止了工作。有没有人知道为什么会发生这种情况以及如何在不使用明确外观的情况下为按钮设置背景颜色?
But it stopped to work after I have commented the above line. Does anybody know why it can happen and how I can set a background color to a button without the usage of an explicit Look and Feel?
ADDED
在我看来,我需要使用 getBackground
。但我不知道如何。
It seems to me that I need to use getBackground
. But I do not know how.
推荐答案
有必要将元素的不透明度设置为true以填充颜色
it is necessary to set Opaque of the element to true for color to be filled
btn = new JButton();
btn.setOpaque(true);
btn.setBackground(backgroundColor);
这篇关于为什么setBackground到JButton不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!