java - 如何处理 JTabbedPane 中选项卡标题的高度

标签 java swing size awt jtabbedpane

我正在使用 JTabbedPane 准备一个带有一些水平选项卡的窗口,选项卡和窗口已正确准备。

现在我需要根据我的要求在级别的基础上设置这些选项卡(我的逻辑返回基于我需要设置级别的整数值)。

水平看起来像:

enter image description here

你能给点建议吗?

最佳答案

只看截图:

enter image description here

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/

相关文章:

java - 如何在导出的 JAR 中使用引用的库

java - 从 ArrayList 重新绘制类的实例

java - 在运行 Swing 应用程序中替换 AWT EventQueue 的安全方法

java - 使 GridBagLayout JPanel 大小适合内容,而不是容器?

java - 实现前获取JPanel的大小

c# - Web API JSON 属性长度限制?

java - 在java中创建唯一值的列表

Java TreeMap 迭代器对于 String 键无法正常工作

java - Kafka 不会消耗所有产生的数据

java - 窗口大小小于应有的大小