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

查看:33
本文介绍了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 的实例),然后将面板添加到窗口(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 中包含按钮,然后在容器中包含 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() 放在中间有什么用?我只试过 frame.add(panel); 并且效果很好.

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

添加了 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 是一个 Container(只需查看 JPanel javadocs).JFrame.getContentPane() 只返回一个 Container 来放置你想要在 JFrame 中显示的 Component.在内部,它使用 JPanel(默认情况下 - 你可以通过调用 setContentPane() 来改变它)至于为什么它返回一个 Container 而不是一个 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().add().情况并非总是如此 - 在 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天全站免登陆