【发布时间】:2011-12-13 03:04:46
【问题描述】:
我想自定义 JTabbedPane 中选项卡的外观。
我想从最简单、最朴素的行为开始:没有边框,纯色。
问题是仍然存在不规则性:标签页边距略有重叠。
您会看到,由于选择了第二个选项卡,它就“脱颖而出”了。 这是通过轻微的边缘重叠来实现的。 是否有(非棘手)方法来禁用此行为?
简单、可测试(仅修复导入)代码:
public class TabbedPane_LookStudy extends JFrame{
public static void main(String [] args) throws UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
new TabbedPane_LookStudy().setVisible(true);
}
public TabbedPane_LookStudy() {
JTabbedPane tp = new JTabbedPane();
tp.setUI(new MyTabbedPaneUI());
add(tp);
tp.addTab("first",new JPanel());
tp.addTab("second", new JPanel());
tp.addTab("third", new JPanel());
setPreferredSize(new Dimension(180,100));
pack();
}
public static class MyTabbedPaneUI extends javax.swing.plaf.basic.BasicTabbedPaneUI {
@Override
protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects,
int tabIndex, Rectangle iconRect, Rectangle textRect) {
Color savedColor = g.getColor();
g.setColor(Color.PINK);
g.fillRect(rects[tabIndex].x, rects[tabIndex].y,
rects[tabIndex].width, rects[tabIndex].height);
g.setColor(Color.BLUE);
g.drawRect(rects[tabIndex].x, rects[tabIndex].y,
rects[tabIndex].width, rects[tabIndex].height);
g.setColor(savedColor);
}
}
}
【问题讨论】:
标签: java swing jtabbedpane