栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

JLabel要setOpaque为true才能看到background
  • 前言
  • 将JLabel设置setOpaque(true), 就能看到setBackground(Color)的颜色
    • 示例
      • 示例1
  • Component与JComponent的isOpaque()源码
    • Component
    • JComponent
  • JComponent与Component的isOpaque不是一个概念
    • 查看一些组件默认的isOpaque()

前言

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
    
    public boolean isOpaque() {
        if (peer == null) {
            return false;
        }
        else {
            return !isLightweight();
        }
    }
JComponent
    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
			Component c = frame.getComponent(i);
			c.setBackground(Color.RED);
			System.out.println(c.getClass()+" 的isOpaque()="+c.isOpaque());
		}
		
		frame.setVisible(true);
		
	}

}

UI 

控制台输出

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看不到

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926668.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号