如何将组件放置在面向右侧的 JTabbedPane 中的选项卡下方

发布于 2024-10-27 17:41:30 字数 490 浏览 5 评论 0原文

因此,我偶然发现 JTabbedPane 中选项卡的左右放置(即 setTabPlacement(JTabbedPane.RIGHT)),我喜欢它的外观。我需要的是利用选项卡下方留下的空间。我目前有一列 JButton,但它们被推到一边,留下了很多空白。

关于如何做到这一点有什么想法吗?某种自定义覆盖层或其他什么?

这是屏幕截图。在代码中,我基本上有一个水平对齐的 Box,JTabbedPane 位于 JTree 上,然后是按钮列。

boxOfEverything.add(tabbedPane);
boxOfEverything.add(boxColumnButtons);

屏幕截图

So I just stumbled across placement of tabs in a JTabbedPane to the right and left (i.e. setTabPlacement(JTabbedPane.RIGHT)) which I love the look of. What I need is to utilize the space this leaves beneath the tabs. I currently have a column of JButtons, but they get pushed to the side, leaving a lot of blank space.

Any thoughts on how to do this? Some kind of custom overlay or something?

Here's a screenshot. In the code I basically have one horizontally aligned Box, with the JTabbedPane over a JTree, then the column of buttons after that.

boxOfEverything.add(tabbedPane);
boxOfEverything.add(boxColumnButtons);

Screenshot here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2024-11-03 17:41:30

我制作了这个社区维基,因为这个答案不是我的。 @cheesecamera 似乎在另一个 论坛并在那里得到了答案。我复制了答案,以便来这里寻找答案的人可以得到答案。

这个想法是使用 swing 的glasspane

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

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.RIGHT);
    JPanel panel = new JPanel(new GridLayout(0, 1));

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

I made this community wiki because this answer is not mine. @cheesecamera seems to have posted the same question on another forum and got an answer there. I copied the answer so that people coming here looking for an answer can get an answer.

The idea is to use swing's glasspane.

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

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.RIGHT);
    JPanel panel = new JPanel(new GridLayout(0, 1));

    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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文