我想自定义 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);
}
}
}
正确的方法是实施定制外观和感觉 https://stackoverflow.com/questions/3954616/look-and-feel-in-java仅有的。但如果你想玩XxxTabbedPaneUI
,那么也许这个帖子 https://stackoverflow.com/questions/7054466/how-can-i-change-the-shape-of-a-jtabbedpane-tab/7056093#7056093可以帮助你。
for Nimbus http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html检查一下会更好aephyr http://code.google.com/p/aephyr/代码库
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)