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