Java getContentPane()具体做什么?
何时使用Java getContentPane()具体做什么?,java,swing,user-interface,Java,Swing,User Interface,何时使用 Container c = getContentpane(); &何时使用 frame.getcontentpane(); 嗯,我: 返回此框架的contentPane对象 。Java的协议确实,无可否认,是一些让您的GUI启动的样板文件: public class FlowLayoutExample extends JApplet { public void init () { getContentPane().setLayout(new FlowLayout ()
Container c = getContentpane();
&何时使用
frame.getcontentpane();
嗯,我:
返回此框架的contentPane对象
。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"));
}
}
但本质上,我们正在获取内容窗格层,以便您以后可以向其中添加对象 如果代码是
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
}
}
您可能正在扩展,这意味着该类将从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();
}
}
如果我们扩展
JFrame
,那么我们可以在BoxLayout
对象中直接使用getContentPane()
方法。
但是如果我们应用关联规则(意味着我们在代码中创建一个JFrame
的对象,即JFrame f=newjframe()
),那么我们需要创建一个容器对象,并将该对象作为参数传递到BoxLayout()
中
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();
}
}
<>这段代码很难理解,而当你继续学习java时,导师会为你充分理解它奠定基础。 首先要考虑的是用<强>方法< /强>修改<强>对象< /强>的规则。在时段的左侧是一个对象,修改对象的方法位于时段的右侧。这条规则在这里适用。 容器中有多个层。您可以将层视为覆盖在容器上的透明薄膜。在JavaSwing中,用于保存对象的层称为内容窗格。对象将添加到容器的内容窗格层。
getContentPane()
方法检索内容窗格层,以便向其中添加对象。内容窗格是由Java运行时环境创建的对象。您不必知道内容窗格的名称即可使用它。当您使用getContentPane()
时,内容窗格对象将被替换,以便您可以对其应用方法。在这行代码中,我们没有向内容窗格添加对象。相反,我们将内容窗格的颜色设置为黄色。这行代码将默认颜色白色更改为黄色,您可能还记得在浏览器中运行的程序示例中看到的黄色矩形。这行代码使矩形区域变成黄色
考虑这一点的一种方法是,将内容窗格对象替换为getContentPane()方法,如下所示:
contentpaneobject.setBackground(Color.YELLOW);
尽管您从未真正看到上面的语句,但您确实拥有语句的功能。使用getContentPane()
方法检索内容窗格时,可以修改内容窗格对象,该对象在上面的示例中被任意命名为contentpaneobject。在此语句中,修改是更改内容窗格的颜色。下一步将在导师中介绍该步骤
请注意getContentPane()
作为方法的形式。该方法以小写字母开头,并带有括号。括号是空的
嗯?你能举个例子吗?API告诉了你什么?我的教科书程序只使用frame.getcontentpane。。但在实际实施时,它并不起作用。我必须使用con=getcontentpane()。。你能告诉我getcontentpane的机制吗。。这可能会消除我所有的疑虑。你能发布一些你的代码吗?第一个示例中调用getContentPane()的类是否扩展了JFrame?我所说的这个框架不是user类的var。。JFrame的itsclass@ChinmayKale-是的,我应该意识到这一点
getContentPane()
是一种Swing方法。不过,我想你已经有了基本的想法。那么我们可以只使用容器类或Jframe类来制作相同的gui?没关系吗?@ChinmayKale-很重要。JFrame
是一种特定类型的容器
,用于显示应用程序的顶级容器。通用的容器
几乎是Java程序的任何一种可视组件,可以容纳其他可视组件。JFrame
本身包含一个特殊的容器
,称为内容窗格,其中放置了它所包含的大部分组件。(一个JFrame
也有一个菜单栏;其他Swing容器类型也可能有自己的专用组件。)当您将组件添加到JFrame
时,它们实际上会添加到其内容窗格中。感谢您的帮助
contentpaneobject.setBackground(Color.YELLOW);