java 使用 JFrame 时,add() 方法从何而来?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14860479/
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
When using JFrame, where does the add() method come from?
提问by Andrew Martin
I've been roaming the Internet looking for an answer to this question, but have come up rather empty handed.
我一直在互联网上漫游寻找这个问题的答案,但结果却是两手空空。
I'm following a JFrame practical from university, and using my lecture notes and the advice on the practical guidance sheet I have this basic code:
我正在关注大学的 JFrame 实践,并使用我的讲义和实践指导表上的建议,我有这个基本代码:
public HelloFrameMenu()
{
message = "Hello, World!";
label = new JLabel(message);
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
add(label, BorderLayout.CENTER);
createSouthPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
This creates a text message on my JPanel and executes the createSouthPanel() method which works fine and adds some menu lists to the JFrame.
这会在我的 JPanel 上创建一条文本消息并执行 createSouthPanel() 方法,该方法工作正常并向 JFrame 添加一些菜单列表。
My question is regarding the add function. I don't understand what this is part of. I thought if it was a static method, its class name would precede it. As it doesn't, I assumed it was part of the JFrame package or one of the classes automatically loaded by Java, but I can't find the method in either.
我的问题是关于添加功能。我不明白这是什么的一部分。我想如果它是一个静态方法,它的类名会在它之前。因为它不是,所以我假设它是 JFrame 包的一部分或由 Java 自动加载的类之一,但我在两者中都找不到该方法。
Where does this method come from?
Why is it not preceded by anything?
What other methods can be used like this?
这种方法从何而来?
为什么它前面没有任何东西?
像这样可以使用哪些其他方法?
回答by assylias
回答by Reimeus
JFrame#addis an instance method that is inherited from java.awt.Container
. It is not preceded my any instance here as you have subclassed a JFrame
directly. Normally you are not adding any new functionality to the frame so the preferred approach is to create an instance directly and use:
JFrame#add是一个从java.awt.Container
. 它没有在我这里的任何实例之前,因为您已经JFrame
直接子类化了 a 。通常您不会向框架添加任何新功能,因此首选方法是直接创建一个实例并使用:
JFrame myFrame = new JFrame();
...
myFrame.add(...);
Check out the javadocfor all available methods.
查看所有可用方法的javadoc。
回答by amphibient
Class HelloFrameMenu
would have to extend JFrame
and the add
method comes from Containerand is used for adding visual components to the frame. It would be more intelligible if add
and setSize
were prefixed with super(e.g. super.add(...)
), which is what I always to to denote that the method comes from a superclass.
类HelloFrameMenu
必须扩展,JFrame
并且该add
方法来自Container并用于向框架添加可视组件。如果add
和setSize
以super(例如super.add(...)
)为前缀会更容易理解,这就是我一直要表示的方法来自超类。
回答by David Lavender
It's to do with Inheritance. Java is an object oriented language which means that (almost) everything is an object.
这与继承有关。Java 是一种面向对象的语言,这意味着(几乎)一切都是对象。
Objects can then be arranged in a hierarchy.
eg a Cat object might extend an Animal object. It inherits all the behaviour (methods) of Animal, and can add its own.
然后可以按层次结构排列对象。
例如,一个 Cat 对象可能会扩展一个 Animal 对象。它继承了Animal的所有行为(方法),并且可以添加自己的。
In your case, JFrame extends Container and Container has an add method.
在您的情况下,JFrame 扩展了 Container 并且 Container 有一个 add 方法。
回答by Srikant Krishna
You can think of it as "this.add" as it is an inherited member from Container.
您可以将其视为“this.add”,因为它是从 Container 继承的成员。