Java Swing - JPanel 与 JComponent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19122416/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:18:23  来源:igfitidea点击:

Java Swing - JPanel vs JComponent

javaswingjpanelawtjcomponent

提问by yapkm01

I'm playing around with Java Swing and i'm really confused when comes to JPanel vs JComponent. According to CoreJava Vol 1 (cay horstmann):

我正在玩 Java Swing,当谈到 JPanel 与 JComponent 时,我真的很困惑。根据 CoreJava 第 1 卷(cay horstmann):

Instead of extending JComponent, some programmers prefer to extend the JPanel class. A JPanel is intended to be a container that can contain other components, but it is also possible to paint on it. There is just one difference. A panel is opaque, which means that it is responsible for painting all pixels within its bounds. The easiest way to achieve that is to paint the panel with the background color, by calling super.paintComponent in the paintComponent method of each panel subclass:

一些程序员更喜欢扩展 JPanel 类而不是扩展 JComponent。JPanel 旨在成为可以包含其他组件的容器,但也可以在其上绘画。只有一处不同。面板是不透明的,这意味着它负责绘制其边界内的所有像素。最简单的方法是用背景颜色绘制面板,通过在每个面板子类的paintComponent方法中调用super.paintComponent:

class NotHelloWorldPanel extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    . . . // code for drawing will go here
  }
}

I know what opaque is. What does he meant by 'A panel is opaque .. responsible for painting all pixels within its bound'? If I read it correctly it says a panel will paint its own areas within its boundaries .. Doesn't JComponent does that too?

我知道什么是不透明。他所说的“面板是不透明的……负责绘制其边界内的所有像素”是什么意思?如果我没看错,它说面板将在其边界内绘制自己的区域.. JComponent 不也这样做吗?

Bottom line is I couldn't see the difference between JPanel and JComponent. Is there a simple examples where I can REALLY see it?

底线是我看不到 JPanel 和 JComponent 之间的区别。有没有我可以真正看到的简单例子?

Any help is appreciated

任何帮助表示赞赏

回答by Cruncher

A JComponent is not required to paint all pixels in it's bound. It may leave some transparent so you can see components behind it.

JComponent 不需要绘制其绑定中的所有像素。它可能会留下一些透明,以便您可以看到它后面的组件。

A JPanel is required to paint all pixels.

需要 JPanel 来绘制所有像素。

Of course if you don't call the super.paintComponent(..)method in either case, they're going to be more or less equivilent, as you then throw out the guarentee that the JPanel will paint the entire panel.

当然,如果您super.paintComponent(..)在任何一种情况下都不调用该方法,它们将或多或少是等效的,因为您随后会放弃 JPanel 将绘制整个面板的保证。

In short, the difference lies in the methods that already exist(namely paint component).

简而言之,区别在于已经存在的方法(即paint组件)。

EDIT:

编辑:

Example of a ball class implemented with a JComponent would look something like this:

使用 JComponent 实现的球类示例如下所示:

(This would not work with a JPanel, as the JPanel is opaque, and the ball would have a square background. You could not call super.paintComponent, but as a general rule you always should, or you break what the component really is. If I pass a JPanel into something, they expect it to be a JPanel, not a JPanel hacked to behave like a JComponent)

(这不适用于 JPanel,因为 JPanel 是不透明的,并且球将具有方形背景。您不能调用super.paintComponent,但作为一般规则,您总是应该这样做,否则您会破坏组件的真正含义。如果我通过一个 JPanel 到某物,他们希望它是一个 JPanel,而不是一个 JPanel 被黑客入侵以表现得像一个 JComponent)

public class Ball extends JComponent
{
    public Ball(int x, int y, int diameter)
    {
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(0, 0, width, height);
    }
}