JTabbedPane 中宽度相等(恒定)的选项卡 [英] Tabs with equal (constant) width in JTabbedPane

查看:50
本文介绍了JTabbedPane 中宽度相等(恒定)的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得一个 JTabbedPane,其中所有选项卡(实际选项卡,而不是组件)具有相同的宽度(最宽标签所需的最小宽度或恒定宽度).

I'm trying to get a JTabbedPane where all tabs (the actual tabs, not the components) have the same width (either the minimum width needed for the widest label or a constant width).

我试图覆盖 BasicTabbedPaneUI.getTabBounds(int tabIndex, Rectangle dest),但显然 BasicTabbedPaneUI 的绘制方法没有使用这个方法,而是它使用 rects 数组来确定标签大小.

I've tried to override BasicTabbedPaneUI.getTabBounds(int tabIndex, Rectangle dest), but apparently this method isn't used by the painting methods of BasicTabbedPaneUI, instead it uses a rects array to determine the tabs size.

我的下一个方法是覆盖 JTabbedPane.insertTab(String title, Icon icon, Component component, String tip, int index) 并设置标签组件的首选大小,但这不会看起来很优雅,我什至不确定它是否会起作用.

My next approach would be to override JTabbedPane.insertTab(String title, Icon icon, Component component, String tip, int index) and setting the preferred size of the label component, but this doesn't seem very elegant and I'm not even sure it would work at all.

有没有办法做到这一点?

Is there a way to achieve this?

推荐答案

答案很简单.当我们为选项卡命名时,只需使用 html 格式化名称.说

The answer is simple. When we put the name for the tab, just format the name using html. say

tp - JTabbedPane 对象

tp - JTabbedPane object

tp.addTab("<html><body><table width='150'>Name</table></body></html>",Componentobject)

这篇关于JTabbedPane 中宽度相等(恒定)的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆