本文介绍了Java - 向JFrame添加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看过几种做有神论的方法,但他们似乎都在工作,但我只是想知道一种方法是否比另一方更好。
I've seen a couple of ways of doing theism they both seem to work but I'm just wondering if one is better practice over the other.
For例如,使用 JFrame
调用 myFrame
,您可以这样做:
For example, with a JFrame
called myFrame
you could do:
myFrame.add(new JButton("OK"));
你也可以这样做:
Container c = myFrame.getContentPane();
c.add(new JButton("OK"));
其中一个'正确'?
推荐答案
来自javadoc类的文字副本 JFrame
A literal copy from the class javadoc of JFrame
frame.add(child);
因此两者都是等效的,两者都是正确的
So both are equivalent, and both are correct
这篇关于Java - 向JFrame添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!