在JTabbedPane的相等(不变)宽度突片
-
20-08-2019 - |
题
我试图得到一个JTabbedPane
其中所有标签(实际的标签,而不是组件)具有相同的宽度(或者所需的最宽的标签或恒定宽度的最小宽度)。
我试图覆盖BasicTabbedPaneUI.getTabBounds(int tabIndex, Rectangle dest)
,但显然不使用BasicTabbedPaneUI
的绘画方法该方法中,代替它使用一个rects阵列,以确定翼片的大小。
我的下一个方法是重写JTabbedPane.insertTab(String title, Icon icon, Component component, String tip, int index)
和设置标签组件的首选大小,但是这似乎并不很优雅,我甚至不知道它会在所有的工作。
有没有办法实现这一目标?
解决方案
答案很简单。 当我们把这个名字的标签,只是用HTML格式的名称。 说
TP - JTabbedPane的对象
tp.addTab("<html><body><table width='150'>Name</table></body></html>",Componentobject)
其他提示
我认为这不是复杂,因为你所做的一切。只是在其上已设置优选尺寸的JLabel使用setTabComponentAt()。
我尝试了以下内容:
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”它来确定正在使用,并相应地对其进行实例化。
例如:
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...
}
这是为我工作。
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));
不隶属于 StackOverflow