gpt4 book ai didi

java - 为什么单元格渲染器经常扩展 JLabel?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:32:01 29 4
gpt4 key购买 nike

我注意到这很常见。例如 DefaultListCellRenderer、DefaultTableCellRenderer 和 DefaultTreeCellRenderer 都使用它。我在网上看到的很多自定义单元格渲染器也使用它。我想在我的代码中使用自定义 TableCellRenderer,但我对是否真的需要子类化 JLabel 感到困惑。子类化 JLabel 有什么好处?

最佳答案

API for the DefaultTableCellRenderer状态:

The table class defines a single cell renderer and uses it as a as a rubber-stamp for rendering all cells in the table; it renders the first cell, changes the contents of that cell renderer, shifts the origin to the new location, re-draws it, and so on. The standard JLabel component was not designed to be used this way and we want to avoid triggering a revalidate each time the cell is drawn. This would greatly decrease performance because the revalidate message would be passed up the hierarchy of the container to determine whether any other components would be affected. As the renderer is only parented for the lifetime of a painting operation we similarly want to avoid the overhead associated with walking the hierarchy for painting operations. So this class overrides the validate, invalidate, revalidate, repaint, and firePropertyChange methods to be no-ops and override the isOpaque method solely to improve performance. If you write your own renderer, please keep this performance consideration in mind.

关于java - 为什么单元格渲染器经常扩展 JLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669910/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com