java 重写paint_java – JLabel:重写paintComponent()之后.如何...

我写了一个JLabel的子类,并且我重写了paintComponent(Graphics)方法来制作渐变背景颜色.

这是JLabel的子类:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.BorderFactory;

import javax.swing.JLabel;

public class DLabel extends JLabel

{

Dimension size = new Dimension(70, 80);

public DLabel()

{

this.setPreferredSize(size);

this.setBorder(BorderFactory.createBevelBorder(TOP, Color.white, Color.black));

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

Color color1 = new Color(226, 218, 145);

Color color2 = color1.brighter();

int w = getWidth();

int h = getHeight();

GradientPaint gp = new GradientPaint(

0, 0, color1, 0, h, color2);

g2d.setPaint(gp);

g2d.fillRect(0, 0, w, h);

}

}

当我创建此标签的实例时,它会正确显示,但是当我使用setText(String)时,文本不会呈现任何内容.

DLabel label = new DLabel();

label.setText("I am"); //No text displayed.

在设置文本后,我尝试了这些方法的不同编译:

label.setOpaque(true);

label.revalidate();

label.repaint();

但没有任何事情发生

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值