我正在使用 JTabbedPane 准备一个带有一些水平选项卡的窗口,选项卡和窗口已正确准备。
现在我需要根据我的要求在级别的基础上设置这些选项卡(我的逻辑返回基于我需要设置级别的整数值)。
水平看起来像:
你能给点建议吗?
最佳答案
只看截图:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TabHeightTest {
public JComponent makeUI() {
JTabbedPane tabbedPane = new JTabbedPane(
JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() {
@Override protected int calculateTabHeight(
int tabPlacement, int tabIndex, int fontHeight) {
return 32;
}
@Override protected void paintTab(
Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
Rectangle iconRect, Rectangle textRect) {
if(tabIndex==0) {
rects[tabIndex].height = 20 + 1;
rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
} else if(tabIndex==1) {
rects[tabIndex].height = 26 + 1;
rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
}
super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
}
});
tabbedPane.addTab("000", new JLabel("aaaaaaaaaaa"));
tabbedPane.addTab("111", new JScrollPane(new JTree()));
tabbedPane.addTab("222", new JSplitPane());
return tabbedPane;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new TabHeightTest().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
关于java - 如何处理 JTabbedPane 中选项卡标题的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18054448/