gpt4 book ai didi

java - JTabbedPane 自定义选项卡外观

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:47:15 25 4
gpt4 key购买 nike

我想自定义 JTabbedPane 中选项卡的外观。
我想从最简单、最朴实的行为开始:无边框、纯色。
问题是仍然存在不平整:标签边距轻微重叠。

enter image description here

您会看到,由于选择了第二个选项卡,它被“置于最前面”。这是通过轻微的边距重叠实现的。是否有一种(非棘手的)方法来禁用此行为?

简单、可测试(只需修复导入)代码:

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);
}
}

最佳答案

正确的方法是执行 Custom Look & Feel只要。但是如果你想玩 XxxTabbedPaneUI,那么可能是 this post可以帮助您。

Nimbus会更好地检查aephyr代码仓库

关于java - JTabbedPane 自定义选项卡外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7859035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com