Java中的容器(AWT)对象

huangapple go评论72阅读模式
英文:

Container (AWT) Object in Java

问题

我正在研究 AWT 中的 FrameJFrame,我看到了对象 Container 作为它们的父对象,但我想知道我是否可以将容器对象用作 FrameJFrame,以下是我的代码,但它不起作用:

public class icontainer {

    public static void main(String[] args) {
        Container icon = new Container(); // new JFRAME(); 
        icon.setSize(300, 300);
        icon.setLocation(300, 300);	
        icon.setVisible(true);
    }
}

为什么我们不使用 icon = new Container() 而不是 JFrame

英文:

I am researching about AWT as Frame, JFrame and I saw object Contianer as parent of them but I wonder that can I use container object as Frame or JFrame, below is my code but it's not working:

public class icontainer {

	public static void main(String[] args) {
		Container icon= new Container(); // new JFRAME(); 
		icon.setSize(300,300);
		icon.setLocation(300, 300);	
		icon.setVisible(true);
	}
}

Why don't we use icon = new Container() instead JFrame?

答案1

得分: 1

这不是继承的工作方式。

一个容器是一个基类,除了跟踪其子元素之外没有做更多的事情。就是这样。它本身不是一个窗口。

根据 Javadoc 的描述:

通用的 Abstract Window Toolkit(AWT) 容器对象是一个可以包含其他 AWT 组件的组件。
添加到容器的组件将被跟踪在列表中。列表的顺序将定义组件在容器内的前后叠放顺序。如果在将组件添加到容器时没有指定索引,它将被添加到列表的末尾(因此位于叠放顺序的底部)。

但就是这样。没有任何地方提到窗口。它本身不显示任何内容。它是在其他地方显示的。

另一方面,JFFrame 一个窗口(它直接继承自 java.awt.Window 通过 java.awt.Frame)。

根据 java.awt.Frame 的 Javadoc:

Frame 是一个具有标题和边框的顶级窗口。

然后有一些关于 Frame 如何布局内容等方面的细节,以及关于 窗口装饰 应该如何看起来和行为的详细信息,特别是关于 WindowEvents(如 WINDOW_CLOSING)的方面。这些都是只存在于 Frames 中的东西,而不是 Containers。

如果你深入研究 JFrame,你会发现一些关于内容窗格的专门处理,以使其与 JFC/Swing 组件体系结构更好地配合工作。

Container window = new Container();

这是你可以编写的代码,但几乎没有用。要使容器实际显示在屏幕上,仍然需要将它放入 窗口 中。

Container window = new JFrame();

你可以这样做,因为 JFrame 是 Container 的子类,因此 可以分配 给 Container。但是,你将失去调用 java.awt.Window、java.awt.Frame 或 javax.swing.JFrame 引入的任何方法的能力,因为这些方法在 Container 上不存在。

英文:

That is not how inheritance works.

A Container is a base class that does not much more but keeping track of its children. And that is it. Its not a window itself.

From the Javadoc:

> A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components.
> Components added to a container are tracked in a list. The order of the list will define the components' front-to-back stacking order within the container. If no index is specified when adding a component to a container, it will be added to the end of the list (and hence to the bottom of the stacking order).

But that is it. There is no mention of a window anywhere. It doesn't display anything on its own. It is displayed in something else.

A JFFrame on the other hand is a window (it literally inherits from java.awt.Window via java.awt.Frame).

From the Javadoc of java.awt.Frame:

> A Frame is a top-level window with a title and a border.

And then there are some details about how the Frame layouts the contents etc.pp, and how the window decorations should look like and behave, especially wrt. WindowEvents like WINDOW_CLOSING. Those are things that are all only present in Frames, not Containers.

And if you go down to JFrame, you get some specialized handling of e.g. the content pane to make it work better with the JFC/Swing component architecture.

Container window = new Container();

This is code you can write, but nearly useless. In order for the container to actually be displayed on screen, it still needs to be put into a window.

Container window = new JFrame();

You can do that, because JFrame is a subclass of container, and thus is assignment-compatible to Container.
However, you lose the ability to call any methods introduced by java.awt.Window, java.awt.Frame or javax.swing.JFrame on the container -- because those don't exist on Container.

答案2

得分: 0

你应该使用JFrame或Frame,因为它们自身的Container不是向用户显示的“窗口”。

我不建议这样做,但你可以像这样将你的JFrame或Frame存储为一个Container:

Container icon = new JFrame();
icon.setSize(300,300);
icon.setLocation(300, 300); 
icon.setVisible(true);
英文:

You should use a JFrame or Frame, because a Container of its own is not a "Window" that is displayed to the User.

I do not recommend it, but you could store your JFrame or Frame as a Container like this:

Container icon = new JFrame();
icon.setSize(300,300);
icon.setLocation(300, 300); 
icon.setVisible(true);

huangapple
  • 本文由 发表于 2020年9月14日 15:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63879515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定