JTabbedPane之间的间距

时间:2014-07-29 03:28:56

标签: java swing netbeans drag-and-drop jtabbedpane

在此JTabbedPane屏幕截图中

screenshot

我正在使用Netbeans的DnD,我一直在修补它只是为了在标签之间放置空格。到目前为止,我仍然无法得到我想要的东西。

有办法吗?

1 个答案:

答案 0 :(得分:4)

我不确定其他方式,但我认为您必须在TabbedPaneUI上安装自己的自定义JTabbedPane

示例:

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class JTabbedPaneWithSpaces implements Runnable
{
  public static void main(String[] args) throws Exception
  {
    SwingUtilities.invokeLater(new JTabbedPaneWithSpaces());
  }

  public void run()
  {
    JTabbedPane pane = new JTabbedPane();
    pane.setUI(new SpacedTabbedPaneUI());
    pane.addTab("One", new JPanel());
    pane.addTab("Two", new JPanel());
    pane.addTab("Threeeeeee", new JPanel());
    pane.addTab("Four", new JPanel());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pane);
    frame.setSize(500, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public class SpacedTabbedPaneUI extends BasicTabbedPaneUI
  {
    @Override
    protected LayoutManager createLayoutManager()
    {
      return new BasicTabbedPaneUI.TabbedPaneLayout()
      {
        @Override
        protected void calculateTabRects(int tabPlacement, int tabCount)
        {
          final int spacer = 20; // should be non-negative
          final int indent = 4;

          super.calculateTabRects(tabPlacement,tabCount);

          for (int i = 0; i < rects.length; i++)
          { 
            rects[i].x += i * spacer + indent;
          } 
        }
      };
    }
  }
}