Does 开发者_高级运维getContentPane().add() mean the same as add() ?
public class TestFrame extends JFrame{
public TestFrame() {
JLabel label = new JLabel("jo");
getContentPane().add(label);
add(label);
}
}
Does getContentPane().add() mean the same as add() ?
Yes, since 1.5+.
Mostly. To make things "easier", addImpl
was changed to forward to the content pane, but in uncommon corner cases it doesn't (for instance, the content pane needs to be added somehow).
This method is overridden to conditionally forward calls to the contentPane.
Yes, because if you notice they both are from
Class Container
java.lang.Object
java.awt.Component
java.awt.Container
add()
JLabel extends container and getContentPane() returns container.
精彩评论