ContentPane和JPanel之间的关系是什么? [英] What is the relation between ContentPane and JPanel?

查看:500
本文介绍了ContentPane和JPanel之间的关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个示例,其中按钮被添加到面板( JPanel 的实例),然后面板被添加到容器(由生成的实例) getContentPane())然后按照构造将容器包含在 JFrame (窗口)中。

I found one example in which buttons are added to panels (instances of JPanel) then panels are added to the the containers (instances generated by getContentPane()) and then containers are, by the construction, included into the JFrame (the windows).

我尝试了两件事:


  1. 我摆脱了容器。更详细地说,我向面板添加了按钮( JPanel 的实例),然后我将面板添加到了Windows( JFrame的实例)。它工作正常。

  1. I got rid of the containers. In more details, I added buttons to a panel (instance of JPanel) and then I added the panel to the windows (instance of JFrame). It worked fine.

我摆脱了面板。更详细地说,我直接向容器添加了按钮,然后我将容器添加到窗口( JFrame 的实例)。

I got rid of the panels. In more details, I added buttons directly to the container and then I added the container to the window (instance of JFrame).

所以,我不明白两件事。

So, I do not understand two things.


  1. 为什么我们有两种竞争机制来做同样的事情?

  1. Why do we have two competing mechanism to do the same things?

将容器与面板结合使用的原因是什么(的JPanel )? (例如,我们在JPanels中包含按钮,然后在Containers中包含JPanels)。我们可以在 JPanel 中包含 JPanel 吗?我们可以在容器中包含容器吗?

What is the reason to use containers in combination with the panels (JPanel)? (For example, what for we include buttons in JPanels and then we include JPanels in the Containers). Can we include JPanel in JPanel? Can we include a container in container?

已添加:

也许我的问题的本质可以放在一行代码中:

Maybe essence of my question can be put into one line of code:

frame.getContentPane().add(panel);

我们放什么 getContentPane() in之间?我尝试了 frame.add(panel); ,它运行正常。

What for we put getContentPane() in between? I tried just frame.add(panel); and it works fine.

ADDED 2:

我想添加一些代码来更清楚我的意思。在这个例子中,我只使用JPane:

I would like to add some code to be more clear about what I mean. In this example I use only JPane:

import java.awt.*;
import javax.swing.*;
public class HelloWorldSwing {
    public static void main(String[] args) {
        JFrame frame = new JFrame("HelloWorldSwing");
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());        
        panel.add(new JButton("W"), BorderLayout.NORTH);
        panel.add(new JButton("E"), BorderLayout.SOUTH);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

在此示例中,我仅使用内容窗格:

And in this example I use only Content Pane:

import java.awt.*;
import javax.swing.*;
public class HelloWorldSwing {
    public static void main(String[] args) {
    JFrame frame = new JFrame("HelloWorldSwing");
    Container pane = frame.getContentPane();
    pane.setLayout(new BorderLayout()); 
    pane.add(new JButton("W"), BorderLayout.NORTH);
    pane.add(new JButton("E"), BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    }
}

两者都运行正常!我只是想知道在这两种方法之间做一件事是否更好(更安全)。

Both work fine! I just want to know if between these two ways to do things one is better (safer).

推荐答案

这不是两种竞争机制 - JPanel 容器(只需查看 JPanel javadocs <<顶部的类层次结构)。 JFrame.getContentPane()只返回容器以放置组件您要在 JFrame 中显示的内容。在内部,它使用 JPanel (默认情况下 - 你可以通过调用 setContentPane()来改变它)至于为什么它是返回容器而不是 JPanel - 这是因为你应该程序到界面,而不是实现 - 在那个级别,你需要关心的就是那个你可以将 Component 添加到某个东西 - 即使 Container 是一个类而不是一个接口 - 它提供了接口需要做到这一点。

It's not two competing mechanisms - a JPanel is a Container (just look at the class hierarchy at the top of the JPanel javadocs). JFrame.getContentPane() just returns a Container to place the Components that you want to display in the JFrame. Internally, it's using a JPanel (by default - you can change this by calling setContentPane()) As for why it's returning a Container instead of a JPanel - it's because you should program to an interface, not an implementation - at that level, all that you need to care about is that you can add Components to something - and even though Container is a class rather than an interface - it provides the interface needed to do exactly that.

至于为什么 JFrame.add() JFrame.getContentPane()。add()都做同样的事情 - JFrame.add()被覆盖以调用 JFrame.getContentPane()。添加()。情况并非总是这样 - 在JDK 1.5之前,您必须明确指定 JFrame.getContentPane()。add()并且 JFrame.add ()如果你调用了它,就会抛出一个 RuntimeException ,但是由于很多抱怨,这在JDK 1.5中被改变了,以达到你所期望的效果。

As for why both JFrame.add() and JFrame.getContentPane().add() both do the same thing - JFrame.add() is overridden to call JFrame.getContentPane().add(). This wasn't always the case - pre-JDK 1.5 you always had to specify JFrame.getContentPane().add() explicitly and JFrame.add() threw a RuntimeException if you called it, but due to many complaints, this was changed in JDK 1.5 to do what you'd expect.

这篇关于ContentPane和JPanel之间的关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆