在JPanel与JComponent上进行绘画有什么好处? [英] What are the benefits to painting on a JPanel vs. JComponent?

查看:119
本文介绍了在JPanel与JComponent上进行绘画有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在最近的回答中,有人对此评论(关于绘画):

So in a recent answer, someone commented this (in regards to painting):

这可能是90%的Swing程序员的病:当他们创建自己的组件时,总是扩展JPanel而不是JComponent.为什么?"

我对编程还是很陌生,所以我称自己为Swing程序员为时尚早,因为我还没有找到合适的人.但是覆盖JPanel只是我的学习方法.因此,我着手找到评论者为什么?" 问题的答案.这些是我找到的一些答案.

I'm still fairly new to programming, so I think it's too early to call myself a Swing programmer, as I have yet to find my niche. But overriding JPanel is just the way I was taught. So I set out to find the answer to the "Why?" question of the commenter. These are some of the answers I found.

背景绘画是主要区别. JComponent类不绘制其背景,因此您必须使用重写的paintComponent方法绘制背景.相比之下,JPanel具有不透明的背景,可以通过调用其paintComponennt方法对其进行绘画.

Background painting is main difference. The JComponent class doesn't paint its background, so you have to paint the background in the overridden paintComponent method. In contrast, JPanel has an opaque background which can be painted by calling its paintComponennt method.


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

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:


如果opaque属性设置为true ...,那么Swing绘画系统不必浪费时间尝试在组件后面进行绘画,因此可以提高性能.

If the opaque property is set to true ... then the Swing painting system does not have to waste time trying to paint behind the component, hence improves performance.


我认为最后一句话确实能最好地说明这一点.但是,除了不透明之外,还有其他有益的理由吗?"90%的Swing程序员有这种病" 扩展了JPanel而不是JComponent吗?


I think the last quote really explains it best. But besides the opacity, are there other beneficial reasons "90% of Swing programmers have this illness" of extending JPanel rather than JComponent?

推荐答案

不透明度处理的差异不是唯一因素.

Difference in opacity handling is not the only factor.

查看JPanel源代码会有所帮助,因为它只有大约100行.

Looking at JPanel source code helps because it is only ~100 lines.

所有构造函数最终都会调用此构造函数. 不透明度和双缓冲默认为true. 默认的LayoutManager是您可能想要或不想要的FlowLayout.

All constructors eventually call this constructor. Opacity and double buffering default to true. The default LayoutManager is FlowLayout which you may or may not want.

public JPanel(LayoutManager layout, boolean isDoubleBuffered) {
        setLayout(layout);
        setDoubleBuffered(isDoubleBuffered);
        setUIProperty("opaque", Boolean.TRUE);
        updateUI();
}

O'Reilly的Java Swing第2版中的Loy等人建议将JComponent扩展为真正的自定义组件(第1333页),但也提到需要考虑UI委托. JPanel处理它自己的具体AccessibleContext,而扩展JComponent的类返回null.

Loy et al in O'Reilly's Java Swing 2nd edition recommend extending JComponent for truly custom components (p.1333) but also mention the need to consider a UI delegate. JPanel handles it's own concrete AccessibleContext whereas a class extending JComponent returns null.

对于只读视觉组件,我通常会扩展JComponent,但由于对可访问性的额外考虑,我可能会对交互式组件三思而后行.

For a read-only visual component I usually extend JComponent but I'd probably think twice for an interactive component because of the extra considerations for accessibility.

干杯

这篇关于在JPanel与JComponent上进行绘画有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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