Java GUI:关于getContentPane()方法和内容 [英] Java GUI: about getContentPane( ) method and content
问题描述
在这段代码中:
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
我可以看到它生成一个新标签并将其添加到 JFrame
object frame
。
但我想了解 getContentPane()
做了什么,为什么需要它呢?
I can see it makes a new label and adds it to the JFrame
object frame
.
But I want to understand what does getContentPane()
do, and why do I need it?
我读了这个API 但我仍然不明白。
I read this API but I still didn't understand.
推荐答案
每个 JPanel
是一个容器,所以要么将它添加到面板然后将其添加到容器中,要么直接使用 add(component)
或使用 getContentPane()。add
方法。两者都将组件添加到Java 7中的容器中(我不知道版本6是否存在此问题)。
Every JPanel
is a container, so either add it to a panel then add it to the container or directly use add(component)
or use the getContentPane().add
method. Both add the component to the container in Java 7 (I don't know if version 6 has a problem with this or not).
这篇关于Java GUI:关于getContentPane()方法和内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!