您不需要Canvas或JPanel绘制吗? [英] You don't need a Canvas or JPanel to draw?

查看:74
本文介绍了您不需要Canvas或JPanel绘制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直以为必须使用Canvas或JPanel才能在JFrame上放置图形,但是我之前看过一个视频,该视频中的人在扩展JFrame时正在使用paint(graphics g),但并没有制作面板或画布.如果是这种情况,为什么人们会花时间制作Canvas或JPanel?

I have been under the impression that a Canvas or a JPanel were necessary to put graphics on a JFrame, however I saw a video earlier in which the person was using paint(graphics g)while extending JFrame and hadn't made a panel or a canvas. If this is a case, why do people bother with making a Canvas or a JPanel?

推荐答案

JFrame Frame 扩展,而 Window 扩展来自 Container ,从定义了 paint Component 扩展.

JFrame extends from Frame, which extends from Window, which extends from Container, extends from Component which defines paint.

如果是这种情况,为什么人们会费心制作Canvas或JPanel?

If this is a case, why do people bother with making a Canvas or a JPanel?

要回答该问题,您需要对 JFrame (和基于窗口的类)有更好的了解.

To answer that question, you need to have a better understanding of JFrame (and window based classes).

JFrame 实际上是一个复合组件,也就是说,它由许多其他组件组成,这些组件提供了窗口的核心功能

JFrame is actually a composite component, that is, it's made of a number of other components which provide the core functionality of the window

这是什么意思,如果您覆盖 paint 并在框架上执行自定义绘制,则很有可能会在子组件上绘制或子组件将在其上绘制,并且由于绘制子系统工作的方式,可以在不调用框架的绘制方法的情况下随时执行.

What this means is, if you override paint and perform custom painting on the frame, it's very possible that you will paint over the child components or the child components will paint over it, and because of the way the paint subsystem works, will do so any time without the frame's paint method been called.

框架在其可用区域内包括窗户装饰.这表示可见"区域实际上比框架的定义区域小.

Frame's include the window decorations within their available area. This means that the "viewable" area is actually smaller then the defined area of the frame.

这也是为什么建议使用 pack 而不是 setSize

This is also why it's recommend to use pack instead of setSize

这意味着,如果您覆盖 paint ,则实际上可以在窗户装饰物下绘画(是的,这种情况一直发生,我们厌倦了回答问题)

This means that if you override paint you could actually paint under the window decorations (yes, this happens all the time, and we're tired of answering it)

中的屏幕截图中间?

Jcode 的 contentPane 会处理此问题,因为它位于可见区域内.

The contentPane of JFrame takes care of this, as it's laid out within the viewable area.

顶级容器,例如 JFrame ,不是双重缓冲的,因此,即使您克服了上述所有问题,也会得到闪烁的更新.当然,您可以设计"您拥有双重缓冲算法,但是在Swing中(即 JPanel ),它是免费的,所以为什么要麻烦

Top level containers, like JFrame aren't double buffered, so, even if you overcome all of the above, you will get updates which flicker. Sure you could "devise" you're own double buffering algorithm, but in Swing (ie JPanel), it's free, so why bother

作为一般建议,我们通常不鼓励从 JFrame (或其他顶级容器)扩展,因为它将您锁定在单个用例中,并防止了重用的可能性.

As a general recommendation, we generally discourage extending from JFrame (or other top level containers) as it locks you into a single use case and prevents the possibility of re-use.

另一方面,如果您使用 JPanel ,则可以将其添加到所需的任何容器中,无论何时需要

On the other hand, if you use a JPanel, you can add it to what ever container you want, when ever you want

这篇关于您不需要Canvas或JPanel绘制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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