Why .paintComponent() is defined on JComponent?

后端 未结 1 826
耶瑟儿~
耶瑟儿~ 2021-01-27 11:31

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 tryi

相关标签:
1条回答
  • 2021-01-27 12:04

    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

    0 讨论(0)
提交回复
热议问题