我正在阅读 Head First Java。在介绍 GUI 时(阅读本书的读者请参见第 354 页),作者写道:
frame.getContentPane().add(button);
“您不会直接将东西添加到框架中。将框架视为窗户周围的装饰,然后您将东西添加到窗格中。”
在查看JFrame 的 Java API 条目后,我看到 JFrame 是 Component 的子类,并且从 Component 继承了 add(Component),因此可以简单地编写:
frame.add(button);
为什么书中推荐使用frame.getContentPane.add(button)?
提前致谢!