Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用 2 种颜色为表格的同一个单元格着色。我还必须在单元格中显示一些文本,我想通过保持当前结构来做到这一点。
我已经尝试过使用标签、面板,但我无法让它正常工作。此外,颜色必须根据单元格上的状态更新(更新前半部分的颜色,同时保持第二部分的颜色)。
您可以编写自己的Component/扩展,JPanel它知道如何显示文本并知道如何用 2 种颜色绘制背景。这意味着您必须覆盖paint或paintComponent在您的自定义组件中。
Component
JPanel
paint
paintComponent
然后,getTableCellRendererComponent将返回此自定义组件的实例。
getTableCellRendererComponent