如何设置 JLabel 的背景颜色?

发布于 2024-08-24 00:49:06 字数 290 浏览 4 评论 0原文

在我的 JPanel 中,我将 JLabel 的背景设置为不同的颜色。我可以看到“测试”一词,它是蓝色的,但背景根本没有改变。我怎样才能让它显示出来?

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

勿忘初心 2024-08-31 00:49:06

使用

label.setOpaque(true);

否则,不会绘制背景,因为 JLabelopaque 默认为 false

来自 JavaDocs :

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

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

Use

label.setOpaque(true);

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

From the 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.

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

烏雲後面有陽光 2024-08-31 00:49:06

JLabel 背景默认是透明的。
将不透明度设置为 true,如下所示:

label.setOpaque(true);

The JLabel background is transparent by default.
Set the opacity at true like that:

label.setOpaque(true);
陌伤ぢ 2024-08-31 00:49:06

您必须将 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 

来自 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()

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.

      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

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()
独行侠 2024-08-31 00:49:06

对于背景,请确保您已将 java.awt.Color 导入到您的包中。

在您的 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);

注意:设置不透明将影响其可见性。请记住 Java 中的区分大小写。

For the Background, make sure you have imported java.awt.Color into your package.

In your main method, i.e. public static void main(String[] args), call the already imported method:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文