JLabel默认isOpaque()为false,看不到背景色,设置了背景色也看不到,JLabel要setOpaque(true)才能看到background

前言

JLabel继承自JComponent
JComponent重写了Component的 isOpaque 方法 完全成了另一回事
Label 默认isOpaque()为false , 看得到背景色;
JLabel 默认isOpaque()也为false , 但看不到背景色

将JLabel设置setOpaque(true), 就能看到setBackground(Color)的颜色

示例

示例1

package jcomponent;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

public class JLabel要setOpaque为true才能看到background2205231559 {
	
	public static void main(String...arguments) throws Exception {
		
		Frame frame = (Frame)Class.forName("java.awt.Frame").getDeclaredConstructor(String.class).newInstance("JLabel要setOpaque为true才能看到background2205231559");
		frame.addWindowListener(new WindowAdapter() {
			@Override public void windowClosing(WindowEvent ev) {System.exit(0);}
		});
		frame.setBounds(100, 100, 600, 480);
		frame.setPreferredSize(new Dimension(1600,900));  //无效
		
		Box vBox = Box.createVerticalBox(); frame.add(vBox);
		
		vBox.setBackground(Color.DARK_GRAY);
		
		JLabel jlb1 = new JLabel("我的背景是红色,但看不到,因为我的isOpaque()默认是false"); jlb1.setBackground(Color.red); vBox.add(jlb1);
		
		JLabel jlb2 = new JLabel("我的背景是橙色,能看到是因为我setOpaque(true);"); jlb2.setOpaque(true); jlb2.setBackground(Color.ORANGE); vBox.add(jlb2);
		
		JLabel jlb3 = new JLabel("我的背景是黄色,看不到是因为我jlb3.setOpaque(false);和默认一样"); jlb3.setOpaque(false); jlb3.setBackground(Color.YELLOW); vBox.add(jlb3);
		
		frame.setVisible(true);
	}
}

在这里插入图片描述
↑同时也能看出,JLabel没有被Box拉伸, 而使用Label时,是被拉伸的

Component与JComponent的isOpaque()源码

Component

    /**
     * Returns true if this component is completely opaque, returns
     * false by default.
     * <p>
     * An opaque component paints every pixel within its
     * rectangular region. A non-opaque component paints only some of
     * its pixels, allowing the pixels underneath it to "show through".
     * A component that does not fully paint its pixels therefore
     * provides a degree of transparency.
     * <p>
     * Subclasses that guarantee to always completely paint their
     * contents should override this method and return true.
     *
     * @return true if this component is completely opaque
     * @see #isLightweight
     * @since 1.2

	* 如果此组件完全不透明,则返回 true,返回
      * 默认为false。
      * 一个不透明的组件绘制其内的每个像素
      * 矩形区域。 一个不透明的组件只绘制一些
      *它的像素,允许它下面的像素“显示出来”。
      * 因此,组件没有完全绘制其像素
      * 提供一定程度的透明度。
      * 保证始终完全绘制它们的子类
      * 内容应覆盖此方法并返回 true。
      * 如果此组件完全不透明,则返回 true
     */
    public boolean isOpaque() {
        if (peer == null) {
            return false;
        }
        else {
            return !isLightweight();
        }
    }

JComponent

/**
     * Returns true if this component is completely opaque.
     * <p>
     * An opaque component paints every pixel within its
     * rectangular bounds. A non-opaque component paints only a subset of
     * its pixels or none at all, allowing the pixels underneath it to
     * "show through".  Therefore, a component that does not fully paint
     * its pixels provides a degree of transparency.
     * <p>
     * Subclasses that guarantee to always completely paint their contents
     * should override this method and return true.
     *
     * @return true if this component is completely opaque
     * @see #setOpaque
	如果此组件完全不透明,则返回 true。
       不透明组件绘制其内部的每个像素
       矩形边界。 一个不透明的组件只绘制一个子集
       它的像素或根本没有,允许它下面的像素
       “显示通过”。 因此,未完全绘制的组件
       它的像素提供了一定程度的透明度。
       保证始终完全绘制其内容的子类
       应该重写此方法并返回 true。
     */
    public boolean isOpaque() {
        return getFlag(IS_OPAQUE);
    }

JComponent与Component的isOpaque不是一个概念

查看一些组件默认的isOpaque()

测试代码👇

package component;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

public class 默认的isOpaque2205231730 {
	
	public static void main(String...arugments) throws Exception {
		
		Frame frame = (Frame)Class.forName("java.awt.Frame").getDeclaredConstructor(String.class).newInstance("默认的isOpaque2205231730");
		frame.addWindowListener(new WindowAdapter() {
			@Override public void windowClosing(WindowEvent ev) {System.exit(0);}
		});
		frame.setBounds(100, 100, 600, 480);
		LayoutManager frameLayout = new GridLayout(0, 6, 10, 10);
		frame.setLayout(frameLayout);
		
		frame.add(new Label("Label"));
		frame.add(new JLabel("JLabel"));
		frame.add(new Button("Button"));
		frame.add(new JButton("Button"));
		frame.add(new TextField("TextField"));
		frame.add(new JTextField("JTextField"));
		frame.add(new TextArea("TextArea"));
		frame.add(new JTextArea("JTextArea"));
		
		
		
		for(int i=0; i<frame.getComponentCount(); i++) {
			Component c = frame.getComponent(i);
			c.setBackground(Color.RED);
			System.out.println(c.getClass()+" 的isOpaque()="+c.isOpaque());
		}
		
		frame.setVisible(true);
		
	}

}

UI 👇
JLabel默认看不到背景色
控制台输出👇

class java.awt.Label 的isOpaque()=false
class javax.swing.JLabel 的isOpaque()=false
class java.awt.Button 的isOpaque()=false
class javax.swing.JButton 的isOpaque()=true
class java.awt.TextField 的isOpaque()=false
class javax.swing.JTextField 的isOpaque()=true
class java.awt.TextArea 的isOpaque()=false
class javax.swing.JTextArea 的isOpaque()=true

可以看到, awt的组件的isOpaque()都是false,但都看得到background , swing组件只有JLabel的isOpaque()为false, 为false看不到

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值