将组件放在JTabbedPane中选项卡的右侧

时间:2016-10-13 13:01:45

标签: java swing

好的,我试图修改以下链接中的现有答案以满足我的需求。在我的情况下,我需要设置JTabbedPane.TOP,但随后按钮消失。

我对Swing不太熟悉,所以请有人告诉我。以下是链接的完整工作代码示例。

How to place components beneath tabs in right oriented JTabbedPane

    public class RightTabPaneButtonPanel {

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {
        new RightTabPaneButtonPanel().makeUI();
      }
    });
  }



    public void makeUI() {
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setTabPlacement(JTabbedPane.TOP);
        JPanel panel = new JPanel(new GridLayout(1, 0));

    for (int i = 0; i < 3; i++) {
      JPanel tab = new JPanel();
      tab.setName("tab" + (i + 1));
      tab.setPreferredSize(new Dimension(400, 400));
      tabbedPane.add(tab);

      JButton button = new JButton("B" + (i + 1));
      button.setMargin(new Insets(0, 0, 0, 0));
      panel.add(button);
    }

    JFrame frame = new JFrame();
    frame.add(tabbedPane);
    frame.pack();
    Rectangle tabBounds = tabbedPane.getBoundsAt(0);

    Container glassPane = (Container) frame.getGlassPane();
    glassPane.setVisible(true);
    glassPane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.NONE;
    int margin = tabbedPane.getWidth() - (tabBounds.x + tabBounds.width);
    gbc.insets = new Insets(0, 0, 0, margin);
    gbc.anchor = GridBagConstraints.SOUTHEAST;

    panel.setPreferredSize(new Dimension((int) tabBounds.getWidth() - margin,
            panel.getPreferredSize().height));
    glassPane.add(panel, gbc);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

2 个答案:

答案 0 :(得分:2)

以下是使用OverlayLayout

的方法
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class TabbedPaneWithComponent
{
    private static void createAndShowUI()
    {
        JPanel panel = new JPanel();
        panel.setLayout( new OverlayLayout(panel) );

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("1", new JTextField("one"));
        tabbedPane.add("2", new JTextField("two"));
        tabbedPane.setAlignmentX(1.0f);
        tabbedPane.setAlignmentY(0.0f);

        JCheckBox checkBox = new JCheckBox("Check Me");
        checkBox.setOpaque( false );
        checkBox.setAlignmentX(1.0f);
        checkBox.setAlignmentY(0.0f);

        JPanel right = new JPanel( new FlowLayout(FlowLayout.RIGHT, 5, 0) );
        right.setOpaque( false );
        right.setAlignmentX(1.0f);
        right.setAlignmentY(0.0f);
        right.add( new JCheckBox("Check Me") );
        right.add( new JCheckBox("Or Check Me") );
        right.setMaximumSize( right.getPreferredSize() );


        panel.add( checkBox );
//      panel.add( right );
        panel.add(tabbedPane);

        JFrame frame = new JFrame("TabbedPane With Component");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setLocationByPlatform( true );
        frame.setSize(400, 100);
        frame.setVisible( true );
        System.out.println(checkBox.getPreferredSize() + " : " + right.getPreferredSize());
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

答案 1 :(得分:1)

我想这就是你想要的:

GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.NORTHEAST;
panel.setPreferredSize(new Dimension(panel.getPreferredSize().width,
            (int) tabBounds.getHeight()));
glassPane.add(panel, gbc);