为什么.paintComponent()是在JComponent上定义的? [英] Why .paintComponent() is defined on JComponent?

查看:123
本文介绍了为什么.paintComponent()是在JComponent上定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我的问题不符合Stackoverflow的要求,因为它是理论上的,但我不知道还能在哪里提出。



过去的几个我一直试图更好地了解Swing API如何工作以及它的组件以创建我自己的自定义组件。我已经阅读了大量的教程,在这里搜索过,我在Java的摇摆源代码中非常深入,坦率地说......我的思绪一团糟。



目前为止据我所知,swing组件由3部分组成:




  • 模型:组件状态和数据的存储位置

  • UI委托:绘制组件和

  • JComponent:它将所有内容联系在一起。



在本教程中, https:// docs .oracle.com / javase / tutorial / uiswing / painting / step2.html 有一段说:


paintComponent方法是进行所有自定义绘制的地方。 >此方法由javax.swing.JComponent定义,然后由>子类重写以提供其自定义行为


为什么有JComponent上的paintComponent方法?它不应该是UI委托的独占方法吗?

解决方案

Swing基于AWT。因此,最初的限制是基于AWT如何绘制组件。因为Swing重量很轻,所以当绘制组件根组件时,需要通知子组件Swing组件他们还需要更新。这是通过调用受更新影响的所有子组件的 paint 方法来完成的(后者又调用 paintComponent 方法)。



关于Swing API的绘画链的许多决定都是基于定制的概念。它降低了所涉及的复杂性(通过阻止覆盖 paint 并将功能集中在 paintComponent 方法)。 / p>

外观和感觉API基于委托模型。这意味着用于执行所述动作的功能被委托给某个其他对象。这意味着UI委托UI实际上并不知道组件需要何时被绘制,而是由组件告知它需要被绘制。这使得它更加灵活,在许多情况下,更容易定制。



基于您的上一个问题自定义 ButtonUI 可能是一个更好的选择,这样你获得更多控制按钮的绘制方式。



试图让按钮跟随当前的外观和感觉颜色方案会非常困难,但你可以尝试一下与JDK一起安装的 src.jar ,其中包括许多外观实现(如果您在Windows上也应该获得Windows的外观和感觉,如果你在Mac上,然后你没有得到Mac或Windows

Sorry if my question doesn't adjust to the Stackoverflow requirements due to it is theorical but i don't know where else to ask.

For the past few weeks i've been trying to understand better how the Swing API works and it's components in order to create my own custom components. I've read tons of tutorials, searched in here, i am neck-deep in Java's swing source code and frankly... my mind is a mess.

As far as i understand, the swing components are composed of 3 parts:

  • the model: where the component state and data are stored
  • the UI delegate: which paints the component and
  • the JComponent: it ties everything together.

In this tutorial https://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html there is a paragraph that says:

The paintComponent method is where all of your custom painting takes place. >This method is defined by javax.swing.JComponent and then overridden by your >subclasses to provide their custom behavior

Why is there a paintComponent method on a JComponent? Shouldn't it be an exclusive method of an UI delegate?

解决方案

Swing is based on AWT. So the initial restriction is based on how AWT paints it components. Because Swing is light weight, when a components root component is painted, the child, Swing, components need to be notified that they need to update as well. This is done by calling the paint methods of all the child components affected by the update (which in turn call the paintComponent methods).

Much of the decisions about the Swing API's paint chain are based around the concept of customisation. It reduces the complexity involved (by discouraging the overriding of paint and focus the functionality down t the paintComponent method).

The Look and Feel API is based on a "delegate" model. This means that the functionality used to perform a said action is "delegated" to some other object. This means that the UI delegate UI doesn't actually know when a component "needs" to be painted, but instead is told, by the component, that it needs to be painted. This makes it much more flexible and in many cases, easier to customise.

Based on your previous question a custom ButtonUI is probably one the better choices, this way you gain much more control over how the button is painted.

Trying to get the buttons to follow the current look and feels color schemes would be very difficult, but you could try having a look at the src.jar which is installed with the JDK, which includes many implementations of look and feels (and if your on Windows you should get the Windows look and feel as well, if your on Mac, then you don't get either Mac or Windows

这篇关于为什么.paintComponent()是在JComponent上定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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