何时使用
Container c = getContentpane();
&安培;何时使用
frame.getcontentpane();
答案 0 :(得分:10)
如果代码是JFrame
子类的一部分,则应使用getContentPane()
。如果代码不是框架的一部分(可能您在应用程序的static main()
方法中),那么您需要使用JFrame
对象来调用getContentPane()
;这就是frame.getContentPane()
的作用。
示例:
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
}
}
答案 1 :(得分:9)
getContentPane().setBackground(Color.YELLOW);
这行代码很难理解,导师将为您在继续学习Java时充分理解它奠定基础。 首先要考虑的是使用方法修改对象的规则。 期间的左侧是对象,修改对象的方法位于右侧期间的一面。该规则适用于此。
容器中有多个图层。您可以将图层视为覆盖容器的透明胶片。在Java Swing中,用于保存对象的图层称为内容窗格。对象将添加到容器的内容窗格层。 getContentPane()
方法检索内容窗格层,以便您可以向其添加对象。内容窗格是Java运行时环境创建的对象。您不必知道要使用它的内容窗格的名称。当您使用getContentPane()
时,内容窗格对象将替换在那里,以便您可以将方法应用于它。在这行代码中,我们没有向内容窗格添加对象。相反,我们将内容窗格的颜色设置为黄色。这行代码将默认颜色(白色)更改为黄色,您可以回想一下在浏览器中运行的程序中看到黄色矩形。这行代码是使矩形区域变黄的原因。
考虑这一点的一种方法是认为内容窗格对象替换了 getContentPane()方法,如下所示:
contentpaneobject.setBackground(Color.YELLOW);
虽然您从未真正看到上述声明,但您确实拥有声明的功能。当您使用getContentPane()
方法检索内容窗格时,您可以修改内容窗格对象,该对象任意命名为contentpaneobject在上面的例子中。在此语句中,修改是更改内容窗格的颜色。接下来将在导师中介绍该步骤。
请注意getContentPane()
作为方法的形式。该方法以小写字母开头,并带有括号。括号是空的。
答案 2 :(得分:1)
返回此框架的contentPane对象。
It's all part of the gui initialization process。实际上,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"));
}
}
但实质上,我们正在获取内容窗格层,以便您以后可以向其添加对象。 See this for more details.
答案 3 :(得分:1)
可能您正在扩展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();
}
}
在上面的示例中,不需要使用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();
}
}
答案 4 :(得分:0)
如果我们延长JFrame
,那么我们可以在getContentPane()
对象中直接使用BoxLayout
方法。
但是如果我们应用关联规则(意味着我们在代码中创建JFrame
的对象,即JFrame f=new JFrame()
),那么我们需要创建一个Container对象并将此对象作为参数传递给BoxLayout()
。
当我们扩展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();
}
}