java label颜色_如何设置JLabel的背景颜色?

问题

在myJPanel中,我将aJLabel的背景设置为不同的颜色。我可以看到"测试"这个词,它是蓝色的,但背景根本不会改变。我该怎么把它展示出来?

this.setBackground(Color.white);

JLabel label = new JLabel("Test");

label.setForeground(Color.blue);

label.setBackground(Color.lightGray);

this.add(label);

#1 热门回答(279 赞)

使用

label.setOpaque(true);

否则后台未绘制,因为默认值为opaqueisfalseforJLabel。

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

有关更多信息,请阅读Java TutorialHow to Use Labels。

#2 热门回答(38 赞)

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

label.setOpaque(true);

#3 热门回答(11 赞)

你必须将setOpaque(true)设置为true,否则背景将不会绘制到表单中。我认为从阅读中可以看出,如果它没有设置为true,它会将一些或不是任何像素绘制到表单中。默认情况下背景是透明的,至少对我来说很奇怪,但是在编程方式中你必须将它设置为true,如下所示。

JLabel lb = new JLabel("Test");

lb.setBackground(Color.red);

lb.setOpaque(true);

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

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在Java中创建带圆角的JLabel的最简单代码: ``` import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class RoundedLabel extends JLabel { private int radius; private Color backgroundColor; public RoundedLabel(String text, int radius, Color backgroundColor) { super(text); this.radius = radius; this.backgroundColor = backgroundColor; setOpaque(false); setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10)); } @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(backgroundColor); g2.fillRoundRect(0, 0, getWidth(), getHeight(), radius, radius); super.paintComponent(g); } public static void main(String[] args) { JFrame frame = new JFrame("Rounded Label"); JPanel panel = new JPanel(); panel.setBackground(Color.WHITE); RoundedLabel label = new RoundedLabel("Hello world!", 15, Color.YELLOW); panel.add(label); frame.add(panel); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ``` 在这个例子中,我们创建了一个自定义的JLabel,名为RoundedLabel。我们可以通过调用它的构造函数来创建一个带圆角的JLabel。在构造函数中,我们设置了标签的文本、圆角半径和背景颜色,并调用了setOpaque(false)和setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10))来去除默认的标签边框和背景。在paintComponent方法中,我们使用Graphics2D类来绘制一个带圆角的矩形作为标签的背景,并调用了super.paintComponent(g)来绘制标签的文本。 最后,我们在main方法中创建了一个JFrame和一个JPanel,并将RoundedLabel添加到JPanel中。我们也可以通过调用label.setBackground(Color.RED)来更改标签的背景颜色

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值