62,623
社区成员
发帖
与我相关
我的任务
分享 protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight)
{
int vHeight = fontHeight;
if (vHeight % 2 > 0)
{
vHeight += 1;
}
return vHeight;
}
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics)
{
return super.calculateTabWidth(tabPlacement, tabIndex, metrics) + metrics.getHeight();
}
class ClippedTitleTabbedPane extends JTabbedPane {
private final Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
private final Insets tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets");
public ClippedTitleTabbedPane() {
super();
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
initTabWidth();
}
});
}
@Override
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
super.insertTab(title, icon, component, tip==null?title:tip, index);
JLabel label = new JLabel(title, JLabel.CENTER);
Dimension dim = label.getPreferredSize();
label.setPreferredSize(new Dimension(0, dim.height+tabInsets.top+tabInsets.bottom));
setTabComponentAt(index, label);
initTabWidth();
}
public void initTabWidth() {
Insets insets = getInsets();
int areaWidth = getWidth() - tabAreaInsets.left - tabAreaInsets.right
- insets.left - insets.right;
int tabCount = getTabCount();
int tabWidth = tabInsets.left + tabInsets.right + 3;
switch(getTabPlacement()) {
case LEFT: case RIGHT:
tabWidth = (int)(areaWidth/4) - tabWidth;
break;
case BOTTOM: case TOP: default:
tabWidth = (int)(areaWidth/tabCount) - tabWidth;
}
for(int i=0;i < tabCount;i++) {
JLabel l = (JLabel)getTabComponentAt(i);
if(l==null) break;
l.setPreferredSize(new Dimension(tabWidth, l.getPreferredSize().height));
}
}
@Override
public synchronized void repaint() {
initTabWidth();
super.repaint();
}
}