Java 如何设置 JLabel 的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2380314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set a JLabel's background color?
提问by Catalina Island
In my JPanel
, I set the background of a JLabel
to a different color. I can see the word "Test" and it's blue, but the background doesn't change at all. How can I get it to show?
在 my 中JPanel
,我将 a 的背景设置JLabel
为不同的颜色。我可以看到“测试”这个词,它是蓝色的,但背景根本没有改变。我怎样才能让它显示?
this.setBackground(Color.white);
JLabel label = new JLabel("Test");
label.setForeground(Color.blue);
label.setBackground(Color.lightGray);
this.add(label);
采纳答案by Peter Lang
Use
用
label.setOpaque(true);
Otherwise the background is not painted, since the default of opaque
is false
for JLabel
.
否则不绘制背景,因为默认值opaque
是false
for JLabel
。
From the JavaDocs:
从JavaDocs:
If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.
如果为 true,则组件在其边界内绘制每个像素。否则,组件可能不会绘制其部分或全部像素,从而允许底层像素显示出来。
For more information, read the Java Tutorial How to Use Labels.
有关更多信息,请阅读 Java 教程如何使用标签。
回答by Yannick Loriot
The JLabel background is transparent by default. Set the opacity at true like that:
JLabel 背景默认是透明的。像这样将不透明度设置为 true:
label.setOpaque(true);
回答by Orji Friday Oko
For the Background, make sure you have imported java.awt.Color
into your package.
对于背景,请确保您已导入java.awt.Color
到您的包中。
In your main
method, i.e. public static void main(String[] args)
, call the already imported method:
在您的main
方法中,即public static void main(String[] args)
调用已经导入的方法:
JLabel name_of_your_label=new JLabel("the title of your label");
name_of_your_label.setBackground(Color.the_color_you_wish);
name_of_your_label.setOpaque(true);
NB: Setting opaque will affect its visibility. Remember the case sensitivity in Java.
注意:设置不透明会影响其可见性。请记住 Java 中的区分大小写。
回答by Orji Friday Oko
You must set the setOpaque(true) to true other wise the background will not be painted to the form. I think from reading that if it is not set to true that it will paint some or not any of its pixels to the form. The background is transparent by default which seems odd to me at least but in the way of programming you have to set it to true as shown below.
您必须将 setOpaque(true) 设置为 true 否则背景将不会被绘制到表单上。我认为从阅读中可以看出,如果它没有设置为 true,它将在表单上绘制一些或不绘制任何像素。默认情况下背景是透明的,这至少对我来说似乎很奇怪,但在编程方式中,您必须将其设置为 true,如下所示。
JLabel lb = new JLabel("Test");
lb.setBackground(Color.red);
lb.setOpaque(true); <--This line of code must be set to true or otherwise the
From the JavaDocs
来自 JavaDocs
setOpaque
设置不透明
public void setOpaque(boolean isOpaque)
If true the component paints every pixel within its bounds. Otherwise,
the component may not paint some or all of its pixels, allowing the underlying
pixels to show through.
The default value of this property is false for JComponent. However,
the default value for this property on most standard JComponent subclasses
(such as JButton and JTree) is look-and-feel dependent.
Parameters:
isOpaque - true if this component should be opaque
See Also:
isOpaque()