如何处理JTabbedPane中标签标题的高度

时间:2013-08-05 09:13:29

标签: java swing size awt jtabbedpane

我正在准备一个带有一些使用JTabbedPane的水平标签的窗口,正确准备了标签和窗口。

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

级别如下:

enter image description here

你能告诉我吗?

2 个答案:

答案 0 :(得分:6)

只是看到截图:

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

答案 1 :(得分:1)

标签边界是外观的责任。如果你控制它(如果你不是,你不应该尝试这样的非便携技巧),你可以在选项卡式窗格的布局管理器中修改它。

对于BasicLookAndFeel,标签边界计算在BasicTabbedPaneUI.TabbedPaneLayout.calculateTabRects()BasicTabbedPaneUI.TabbedPaneScrollLayout.calculateTabRects()中完成。对于基本的L& F派生主题,BasicTabbedPaneUI有一个createLayout()方法,可以重写该方法以返回具有所需行为的布局管理器。

相关问题