Java BasicTabbedPaneUI绘制html文本
我会扩展Java BasicTabbedPaneUI绘制html文本,java,swing,Java,Swing,我会扩展BasicTabbedPaneUi,这样我就可以设计自己的选项卡窗格了。我对html文本有一个问题,就是一旦选择了选项卡,就设置文本的颜色。我使用以下代码覆盖paintText方法,代码与原始方法几乎相同: @Override protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRec
BasicTabbedPaneUi
,这样我就可以设计自己的选项卡窗格了。我对html文本有一个问题,就是一旦选择了选项卡,就设置文本的颜色。我使用以下代码覆盖paintText方法,代码与原始方法几乎相同:
@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) {
g.setFont(font);
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
Color fg = tabPane.getForegroundAt(tabIndex);
if (isSelected && (fg instanceof UIResource)) {
Color selectedFG = UIManager.getColor(
"TabbedPane.selectedForeground");
if (selectedFG != null) {
fg = selectedFG;
}
}
v.paint(g, textRect);
} else {
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
Color fg = tabPane.getForegroundAt(tabIndex);
if (isSelected && (fg instanceof UIResource)) {
Color selectedFG = UIManager.getColor(
"TabbedPane.selectedForeground");
if (selectedFG != null) {
fg = selectedFG;
}
}
g.setColor(fg);
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent());
} else { // tab disabled
g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent());
g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
title, mnemIndex,
textRect.x - 1, textRect.y + metrics.getAscent() - 1);
}
}
}
如果选项卡有html文本,则它不为null。如果为绘制方法中使用的图形对象设置颜色,则不会更改文本颜色。
我使用html是因为我想把我的标签文本放在两行上。
感谢您帮助更改颜色。我不愿意开发定制,除非它是完整外观和感觉实现的一部分
替代,考虑自定义选项卡组件,如图中所示和讨论的。这将为您提供对组件外观的绝对控制,而不会牺牲与用户选择的外观的兼容性。
设置HTML属性时会发生什么情况?当我直接在HTML标记中设置它时,它会起作用,但当选择选项卡时,如何更改它?