JPanel said to be opaque what does that mean?
In Swing JPanel said to be opaque, what does that mean?
Also how JPanel VS JComponent compared in relation to opaque?
Please explain if possible in simple terms, hence i am not very experienced in GUI programming.
开发者_如何学JAVAThanks in advance for your help
Opaque has a very specific meaning in Swing - it means that the component fully paints the full area within its bounds (see the setOpaque javadoc)
This is used primarily to determine whether it is necessary to repaint components behind the current component.
- If isOpaque is true, it is unnecessary to repaint anything behind the component (since it would just be overwritten), hence such background drawing may be omitted as an optimisation.
- If isOpaque is false, then it indicates that the component is implementing some transparency effects - e.g. drawing a component that has a semi-transparent window in the middle, or drawing a non-rectangular component
If you are creating your own JComponent and setOpaque to true but do not honour the contract (i.e. you do not draw the full area within the bounds despite claiming to be opaque) then you may get unexpected results due to the background not being redrawn.
Opaque and transparent are opposite words.
A window, water, or air are transparent - you can see through them.
A wall, mud or concrete are opaque - you cannot see through them.
In terms of GUI programming, both JPanel and JComponent are opaque by default. If you set them transparent, you are likely not to see much difference, since the underlying JFrame (or whatever) will usually be the same color as the JPanel/JComponent.
Another useful reference is the section on Opacity in the article Painting in AWT and Swing. It's also helpful to know that JPanel
is opaque by default on many L&Fs, while JLabel
is not.
精彩评论