如何设置JLabel的背景颜色? [英] How do I set a JLabel's background color?

查看:140
本文介绍了如何设置JLabel的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 JPanel 中,我将 JLabel 的背景设置为其他颜色。我可以看到测试这个词,它是蓝色的,但背景根本不会改变。如何才能显示?

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?

this.setBackground(Color.white);
JLabel label = new JLabel("Test");
label.setForeground(Color.blue);
label.setBackground(Color.lightGray);
this.add(label);


推荐答案

使用

label.setOpaque(true);

否则后台未绘制,因为默认值 opaque false JLabel

Otherwise the background is not painted, since the default of opaque is false for JLabel.

来自 JavaDocs


如果为true,则组件绘制其边界内的每个像素。否则,组件可能不会绘制其部分或全部像素,从而允许底层像素显示。

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.

有关更多信息,请阅读Java教程如何使用标签

For more information, read the Java Tutorial How to Use Labels.

这篇关于如何设置JLabel的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆