本文介绍了如何在JTabbedPane中处理选项卡标题的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am preparing a window with some horizontal tabs using JTabbedPane, Tabs And window are prepared properly.

现在,我需要根据自己的需求逐级设置这些选项卡(我的逻辑会根据需要设置的级别返回整数值).

Now I need to setup these tabs in level basis, based on my requirement (My logic returns integer value based on that I need to setup levels ).

级别看起来像:

能否请您指教?

推荐答案

只看截图:

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

这篇关于如何在JTabbedPane中处理选项卡标题的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 21:42