java.awt.Component与java.swing.JComponent的 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, 1600, 800);
		LayoutManager frameLayout = new GridLayout(0, 8, 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"));
		frame.add(new JCheckBox("JCheckBox"));
		frame.add(new JMenu("JMenu是Component,Menu不是,frame不能add(Menu)"));
		frame.add(new JPanel());
		frame.add(Box.createVerticalBox());
		frame.add(Box.createHorizontalBox());
		frame.add(new JRootPane());
		
		
		
		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👇
在这里插入图片描述
控制台输出👇

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
class javax.swing.JCheckBox 	的isOpaque()=true
class javax.swing.JMenu 	的isOpaque()=false
class javax.swing.JPanel 	的isOpaque()=true
class javax.swing.Box 	的isOpaque()=false
class javax.swing.Box 	的isOpaque()=false
class javax.swing.JRootPane 	的isOpaque()=false

可以看到Awt组件的isOpaque()都是false, 但都能看到background,
Swing组件中isOpaque()为flalse的, 都看不到background

Component与JComponent的isOpaque()源码

Component 的 isOpaque()

    /**
     * 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 的 isOpaque()

/**
     * 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);
    }

结论: awt的isOpaque()与swing的isOpaque()不一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kfepiza

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

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

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

打赏作者

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

抵扣说明:

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

余额充值