JTabbedPane 中具有相等(恒定)宽度的选项卡

发布于 2024-07-12 14:08:12 字数 403 浏览 13 评论 0原文

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

我尝试覆盖 BasicTabbedPaneUI.getTabBounds(int tabIndex, Rectangle dest),但显然 BasicTabbedPaneUI 的绘画方法不使用此方法,而是使用rects 数组来确定选项卡大小。

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

有办法实现这一点吗?

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).

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晨敛清荷 2024-07-19 14:08:12

答案很简单。
当我们为选项卡输入名称时,只需使用 html 格式化名称即可。

tp - JTabbedPane 对象

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

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

tp - JTabbedPane object

tp.addTab("<html><body><table width='150'>Name</table></body></html>",Componentobject)
殊姿 2024-07-19 14:08:12

我认为这并不像你做的那么复杂。 只需将 setTabComponentAt() 与已设置首选大小的 JLabel 结合使用即可。

I think it's not as complicated as you've done. Just use setTabComponentAt() with a JLabel on which you've set preferred size.

↙厌世 2024-07-19 14:08:12

我尝试过以下方法:

tabPane.setUI(new javax.swing.plaf.metal.MetalTabbedPaneUI() {
    @Override
    protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
        return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight) + 12;
    }
});

这似乎对我来说效果很好。 但如果您使用不同的 L&F,无论如何您最终都会使用“金属”渲染它。

我想您可以获取默认 UI 并对其执行“instanceof”以确定正在使用哪个 UI 并相应地实例化它。

例如:

TabbedPaneUI ui = tabPane.getUI();

if (ui instanceof WindowsTabbedPaneUI) {
    // Create the windows rendition
} else if (ui instanceof MetalTabbedPaneUI) {
    // Create the metal rendition
} else if (ui instanceof MotifTabbedPaneUI) {
    // Create the motif rendition
} else if (ui instanceof SynthTabbedPaneUI) {
    // Etc...
}

I've tried the following:

tabPane.setUI(new javax.swing.plaf.metal.MetalTabbedPaneUI() {
    @Override
    protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
        return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight) + 12;
    }
});

This seems to work fine for me. But if your using a different L&F, you'll end up rendering it with the 'metal' regardless.

I guess you could get the default UI and do an 'instanceof' on it to determine which is being used and instantiate it accordingly.

For example:

TabbedPaneUI ui = tabPane.getUI();

if (ui instanceof WindowsTabbedPaneUI) {
    // Create the windows rendition
} else if (ui instanceof MetalTabbedPaneUI) {
    // Create the metal rendition
} else if (ui instanceof MotifTabbedPaneUI) {
    // Create the motif rendition
} else if (ui instanceof SynthTabbedPaneUI) {
    // Etc...
}
何时共饮酒 2024-07-19 14:08:12

这对我有用。

 JLabel lab = new JLabel();
        lab.setPreferredSize(new Dimension(200, 30));
        jTabbedPane1.setTabComponentAt(0, lab);  // tab index, jLabel

或尝试对相同大小的所有选项卡组件进行此更改(在主方法中调用)

UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab.contentMargins", new Insets(10, 100, 0, 0));< /代码>

this is worked for me.

 JLabel lab = new JLabel();
        lab.setPreferredSize(new Dimension(200, 30));
        jTabbedPane1.setTabComponentAt(0, lab);  // tab index, jLabel

or try this change to all tab component in same sizes (called in main method)

UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab.contentMargins", new Insets(10, 100, 0, 0));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文