Java getContentPane() 究竟做了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25851894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does getContentPane() do exactly?
提问by Chinmay Kale
When to use
何时使用
Container c = getContentpane();
& when to use.
& 何时使用。
frame.getcontentpane();
采纳答案by Ted Hopp
If the code is part of a JFrame
subclass, you should use getContentPane()
. If the code is not part of the frame (perhaps you're in the static main()
method for the application), then you need to use a JFrame
object to call getContentPane()
; that's what frame.getContentPane()
does.
如果代码是JFrame
子类的一部分,则应使用getContentPane()
. 如果代码不是框架的一部分(也许您在static main()
应用程序的方法中),那么您需要使用一个JFrame
对象来调用getContentPane()
; 就是frame.getContentPane()
这样。
Examples:
例子:
public class TestApp extends JFrame {
public static void main(String[] args) {
TestApp frame = new TestApp();
Container c = frame.getContentPane();
// do something with c
frame.pack();
frame.show();
}
/* constructor */
public TestApp() {
Container c = getContentPane(); // same as this.getContentPane()
// initialize contents of frame
}
}
回答by Coffee
Well, I could direct to the api:
好吧,我可以直接访问 api:
Returns the contentPane object for this frame.
返回此框架的 contentPane 对象。
It's all part of the gui initialization process. Java's protocol really, admittedly some boilerplate to get your GUI up:
这都是 gui 初始化过程的一部分。诚然,Java 的协议确实是一些样板,可以让您的 GUI 启动:
public class FlowLayoutExample extends JApplet {
public void init () {
getContentPane().setLayout(new FlowLayout ());
getContentPane().add(new JButton("One"));
getContentPane().add(new JButton("Two"));
getContentPane().add(new JButton("Three"));
getContentPane().add(new JButton("Four"));
getContentPane().add(new JButton("Five"));
getContentPane().add(new JButton("Six"));
}
}
But essentially, we're obtaining the content pane layer so that you can later add an object to it. See this for more details.
但本质上,我们正在获取内容窗格层,以便您稍后可以向其中添加对象。有关更多详细信息,请参阅此内容。
回答by David Yee
Likely you are extending JFrame
which means that the class will inherit the methods from JFrame
. As such, your code may look somewhat like the following:
您可能正在扩展JFrame
,这意味着该类将从JFrame
. 因此,您的代码可能类似于以下内容:
public class MyClass extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new MyClass();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyClass() {
...
Container c = getContentPane();
}
}
In the above example, there is no need to use frame.getContentPane()
because you are inheriting the methods of JFrame
. In other words, you only need to write getContentPane()
. Alternatively, in most cases you should actually be instantiatinga new JFrame
as an instance variable unless you are actually adding new functionality to the JFrame
class:
在上面的例子中,没有必要使用,frame.getContentPane()
因为你继承了JFrame
. 换句话说,您只需要编写getContentPane()
. 另外,在大多数情况下,你实际上应该实例化一个新JFrame
的实例变量,除非你实际上是在增加新功能的JFrame
类:
public class MyClass {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new MyClass();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyClass() {
...
Container c = frame.getContentPane();
}
}
回答by Sachindra N. Pandey
If you we extend JFrame
then we can use getContentPane()
method direct in our BoxLayout
object .
But if we apply association rule (means we create an Object of JFrame
in our code i.e. JFrame f=new JFrame()
), then we need to create a Container object and pass this object in BoxLayout()
as an argument.
如果我们扩展,JFrame
那么我们可以getContentPane()
直接在我们的BoxLayout
对象中使用方法。但是如果我们应用关联规则(意味着我们JFrame
在我们的代码 ie 中创建了一个对象JFrame f=new JFrame()
),那么我们需要创建一个 Container 对象并将这个对象BoxLayout()
作为参数传入。
When we extends JFrame:
public class BoxLayOutTest extends JFrame { public BoxLayOutTest() { // TODO Auto-generated constructor stub setSize(300, 400); setVisible(true); getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); JButton b1 = new JButton("One"); JButton b2 = new JButton("Two"); JButton b3 = new JButton("Three"); JButton b4 = new JButton("Four"); JButton b5 = new JButton("Five"); add(b1); add(b2); add(b3); add(b4); add(b5); } public static void main(String[] args) { new BoxLayOutTest(); } }
If we create
JFrame
Object inside code:public class TwoPanelinFrame { JFrame f; public TwoPanelinFrame() { f = new JFrame("Panel Test"); f.setSize(600, 600); f.setVisible(true); Container c = f.getContentPane(); f.getContentPane().setLayout(new BoxLayout(c, BoxLayout.X_AXIS)); JButton b2 = new JButton("One"); JButton b3 = new JButton("One"); JButton b4 = new JButton("One"); JButton b5 = new JButton("One"); f.add(b2); f.add(b3); f.add(b4); f.add(b4); } public static void main(String[] args) { new TwoPanelinFrame(); } }
当我们扩展 JFrame 时:
public class BoxLayOutTest extends JFrame { public BoxLayOutTest() { // TODO Auto-generated constructor stub setSize(300, 400); setVisible(true); getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); JButton b1 = new JButton("One"); JButton b2 = new JButton("Two"); JButton b3 = new JButton("Three"); JButton b4 = new JButton("Four"); JButton b5 = new JButton("Five"); add(b1); add(b2); add(b3); add(b4); add(b5); } public static void main(String[] args) { new BoxLayOutTest(); } }
如果我们
JFrame
在代码中创建对象:public class TwoPanelinFrame { JFrame f; public TwoPanelinFrame() { f = new JFrame("Panel Test"); f.setSize(600, 600); f.setVisible(true); Container c = f.getContentPane(); f.getContentPane().setLayout(new BoxLayout(c, BoxLayout.X_AXIS)); JButton b2 = new JButton("One"); JButton b3 = new JButton("One"); JButton b4 = new JButton("One"); JButton b5 = new JButton("One"); f.add(b2); f.add(b3); f.add(b4); f.add(b4); } public static void main(String[] args) { new TwoPanelinFrame(); } }
回答by Jaimin Patel
getContentPane().setBackground(Color.YELLOW);
This line of code is difficult to understand, and the tutor will lay the foundation for you to understand it fully as you continue to study Java. First to consider is the rule about modifying an objectwith a method. On the left side of a periodis an object, and the methodthat modifies the objectis on the right side of the period. That rule applies here.
这行代码难懂,导师会在你继续学习Java的过程中,为你完全理解它打下基础。首先要考虑的是使用方法修改对象的规则。句号左边是一个对象,句号右边是修改对象的方法。该规则适用于此。
A containerhas several layers in it. You can think of a layer as a transparent film that overlays the container. In Java Swing, the layer that is used to hold objects is called the content pane. Objects are added to the content pane layer of the container. The getContentPane()
method retrieves the content pane layer so that you can add an object to it. The content pane is an object created by the Java run time environment. You do not have to know the name of the content pane to use it. When you use getContentPane()
, the content pane object then is substituted there so that you can apply a method to it. In this line of code, we are not adding an object to the content pane. Rather, we are making the color of the content pane to be yellow. This line of code is what changes the default color, white, to yellow, and you may recall seeing the yellow rectangle in the example the program running in a browser. This line of code is what made that rectangular area yellow.
一个容器中有几层。您可以将层视为覆盖容器的透明薄膜。在 Java Swing 中,用于保存对象的层称为内容窗格。对象被添加到容器的内容窗格层。该getContentPane()
方法检索内容窗格层,以便您可以向其添加对象。内容窗格是由 Java 运行时环境创建的对象。您不必知道内容窗格的名称即可使用它。当你使用getContentPane()
,然后内容窗格对象会在那里被替换,以便您可以对其应用方法。在这行代码中,我们没有向内容窗格添加对象。相反,我们将内容窗格的颜色设为黄色。这行代码将默认颜色白色更改为黄色,您可能还记得在浏览器中运行的程序示例中看到的黄色矩形。这行代码使矩形区域变黄。
One way to think about this is to think that the content pane object is substituted for the getContentPane() method, like this:
考虑这一点的一种方法是认为内容窗格对象替换了getContentPane() 方法,如下所示:
contentpaneobject.setBackground(Color.YELLOW);
Although you never really see the above statement, you do have the functionality of the statement. When you retrieve the content panewith the getContentPane()
method, you can then modify the content pane object, which is arbitrarily named contentpaneobject in the example above. In this statement, the modification is to change the color of the content pane. That step is presented next in the tutor.
尽管您从未真正看到上述语句,但您确实拥有该语句的功能。当您使用方法检索内容窗格时,您可以修改内容窗格对象,它在上面的示例中被任意命名为 contentpaneobject。在此语句中,修改是更改内容窗格的颜色。接下来将在导师中介绍该步骤。getContentPane()
Notice the form of getContentPane()
as a method. The method begins with a lower case letter, and it has parentheses. The parentheses are empty.
注意getContentPane()
作为方法的形式。该方法以小写字母开头,并带有括号。括号是空的。