确定,我正在尝试通过下面的链接修改现有答案,以满足我的需要。就我而言,我需要设置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);
  }
}

最佳答案

我想这就是你想要的:

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

关于java - 将组件放在JTabbedPane中选项卡的右侧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40021783/

10-10 22:29