简体   繁体   English

Canvas和JPanel之间的区别

[英]Difference between Canvas and JPanel

  1. What is the difference between the Canvas class and the JPanel class, as far as graphics goes? 就图形而言, Canvas类和JPanel类之间有什么区别?
  2. Are there any advantages or disadvantages to either of these classes? 这些课程中是否有任何优点或缺点?
  3. I always use the Canvas class. 我总是使用Canvas类。 Should I be using one class over the other? 我应该使用一个班级吗? If so, why? 如果是这样,为什么?

There are a few differences. 有一些差异。 JPanel: 的JPanel:

  1. is a Container, so you can add other Components to it 是一个Container,因此您可以添加其他组件
  2. setBackground will automatically fill the background color setBackground将自动填充背景颜色
  3. Set / Add tooltip 设置/添加工具提示
  4. Add a Border 添加Border
  5. printComponent for printing printComponent用于打印

Fairly minor stuff, but sometimes important 相当小的东西,但有时很重要

Canvas: AWT JPanel: Swing 画布:AWT JPanel:Swing

Swing is based on AWT, so Canvas can be more lightweight and in lower layer. Swing基于AWT,因此Canvas可以更轻巧,更低层。

If canvas meet all your requirements, just use Canvas. 如果canvas满足您的所有要求,请使用Canvas。

PS: I don't think there would be too much expensive to use JPanel, just choose the one you like. PS:我认为使用JPanel不会太昂贵,只需选择你喜欢的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM