gpt4 book ai didi

java - Swing 中 "Container c=getContentPane();"的用途是什么?

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:37 25 4
gpt4 key购买 nike

import java.awt.*;
import javax.swing.*;
public class
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import javax.swing.event.*;
/*<applet code="JT.class" width=200 height=300>
</applet>*/

public class JT extends JApplet {
JTree tree;
JTextField box;
Object nodeInfo;
String node1;
public void init() {
Container c=getContentPane();
c.setLayout(new BorderLayout());
DefaultMutableTreeNode topNode=new DefaultMutableTreeNode("qiscet");
DefaultMutableTreeNode cou=new DefaultMutableTreeNode("Courses");
DefaultMutableTreeNode mca=new DefaultMutableTreeNode("MCA");
DefaultMutableTreeNode mba=new DefaultMutableTreeNode("MBA");
DefaultMutableTreeNode tech=new DefaultMutableTreeNode("B.tech");
topNode.add(cou);
cou.add(mca);
cou.add(mba);
cou.add(tech);
DefaultMutableTreeNode manage=new DefaultMutableTreeNode("Management");
DefaultMutableTreeNode ac=new DefaultMutableTreeNode("Accounts");
DefaultMutableTreeNode sp=new DefaultMutableTreeNode("Sports");
DefaultMutableTreeNode lib=new DefaultMutableTreeNode("Library");
topNode.add(manage);
manage.add(ac);
manage.add(sp);
manage.add(lib);
tree=new JTree(topNode);
c.add(tree,BorderLayout.NORTH);
box=new JTextField("",80);
c.add(box,BorderLayout.SOUTH);
}
}

我的问题是不使用“Container c=getContentPane();”我得到正确的输出。这怎么可能?这是什么原因?

最佳答案

首先,Swing 让您对 add() 和 setLayout() 之类的东西使用 getContentPane(),让您意识到存在不同的层。过了一会儿,我猜他们承认这很痛苦,所以他们在内部调用了 getContentPane(),这样您就不必再这样做了。

This was changed in JDK 1.5 :

Lastly, after seven years, we've made jFrame.add equivalent to jFrame.getContentPane().add()

And here is a link to the rationale behind the original reason.

关于java - Swing 中 "Container c=getContentPane();"的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/708302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com