简体   繁体   English

Java 中的容器 (AWT) 对象

[英]Container (AWT) Object in Java

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:我正在研究 AWT 作为FrameJFrame并且我看到对象Contianer作为它们的父级,但我想知道我可以将容器对象用作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);
    }
}

Why don't we use icon = new Container() instead JFrame ?为什么我们不使用icon = new Container()而不是JFrame

That is not how inheritance works.这不是继承的工作方式。

A Container is a base class that does not much more but keeping track of its children. Container 是一个基类,除了跟踪它的子类之外,它并没有做更多的事情。 And that is it.就是这样。 Its not a window itself.它本身不是窗户。

From the Javadoc:来自 Javadoc:

A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components.通用抽象窗口工具包 (AWT) 容器对象是一个可以包含其他 AWT 组件的组件。
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).另一方面,JFFrame一个窗口(它从字面上通过 java.awt.Frame 从 java.awt.Window 继承)。

From the Javadoc of java.awt.Frame:来自 java.awt.Frame 的 Javadoc:

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.然后还有一些关于 Frame 如何布局内容等的细节。pp,以及窗口装饰的外观和行为,尤其是 wrt。 WindowEvents like WINDOW_CLOSING. WindowEvents 像 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 eg the content pane to make it work better with the JFC/Swing component architecture.如果你深入到 JFrame,你会得到一些专门的处理,例如内容窗格,以使其更好地与 JFC/Swing 组件架构一起工作。

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 .为了让容器真正显示在屏幕上,它仍然需要放入一个window

Container window = new JFrame();

You can do that, because JFrame is a subclass of container, and thus is assignment-compatible to Container.您可以这样做,因为 JFrame 是容器的子类,因此与容器的赋值兼容 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.但是,您无法调用容器上由 java.awt.Window、java.awt.Frame 或 javax.swing.JFrame 引入的任何方法——因为容器上不存在这些方法。

You should use a JFrame or Frame, because a Container of its own is not a "Window" that is displayed to the User.您应该使用 JFrame 或 Frame,因为它自己的容器不是显示给用户的“窗口”。

I do not recommend it, but you could store your JFrame or Frame as a Container like this:我不推荐它,但您可以像这样将 JFrame 或 Frame 存储为容器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM