Java:删除 JTabbedPane 上的边距/填充

发布于 2024-10-19 21:34:16 字数 379 浏览 7 评论 0原文

我想知道如何删除 JtabbedPane 和 JFrame 内容窗格之间以及 JTabbedPane 和其内部 JPanel 之间的边距。 我圈出了我想要删除的边距。 绿线在这里显示 jtabbedpane 内的 jpanel 之间的间隙。

我尝试查看一些名为 setMargin 的方法,但 JTabbedPane 上不存在该方法。我还检查了不同布局(根内容窗格、我的 jpanel 等)上的 HgapVgap (两者 = 0)。

所以欢迎任何建议。 谢谢。

图片在这里

我还不能发布图像。

I'd like to know how to remove the margins between my JtabbedPane and my JFrame content pane and between my JTabbedPane and its internal JPanel.
I circled the margins I want to remove.
the green line is here to show the gap between the jpanel inside the jtabbedpane.

I tried to look at some method named setMargin but it doesn't exist on a JTabbedPane. I also checked the Hgap and Vgap (both = 0) on the different layout (root content pane, my jpanel, etc).

So any suggestions are welcome.
Thanks.

The image is here.

I can't post images yet.

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

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

发布评论

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

评论(3

·深蓝 2024-10-26 21:34:16

由外观和感觉来决定选项卡式窗格内的组件周围有多少空间 - 通常它会根据桌面的默认设置来执行此操作。 JTabbedPane 没有设置内部组件周围插图的方法。

可以为所有选项卡式窗格进行全局设置(注意:适用于 MetalLookAndFeel,可能也适用于 Windows L&F,但可能不会 em> 适用于 GTK 或 Nimbus 外观和感觉,它们不基于 BasicLookAndFeel)。这将改变虚拟机中所有选项卡式窗格的外观:

UIManager.getDefaults().put("TabbedPane.contentBorderInsets", new Insets(0,0,0,0));
UIManager.getDefaults().put("TabbedPane.tabsOverlapBorder", true);

您可能还想确保您的 JTabbedPane 具有 EmptyBorder(0,0,0,0) 以及您放入其中的组件。

如果这在您的目标桌面上不起作用,替代方案是

  • 如果您不关心选项卡式窗格看起来与本机应用程序选项卡式窗格不同,则(令人不愉快的)替代方案是编写您自己的 TabbedPaneUI,
  • 为单个选项卡式窗格设置 UI 委托JTabbedPane 您希望 MetalTabbedPaneUI 或其他一些确实响应这些属性的 UI 委托看起来像这样

It is up to the look and feel to decide how much space is around components inside a tabbed pane - generally it will do this based on whatever is the default for your desktop. JTabbedPane does not have methods for setting the insets around internal components.

You can set this globally for all tabbed panes (caveat: Works on MetalLookAndFeel, will probably work for Windows L&F as well, probably won't work for GTK or Nimbus look and feel which are not based on BasicLookAndFeel). This will change the appearance of all tabbed panes in the VM:

UIManager.getDefaults().put("TabbedPane.contentBorderInsets", new Insets(0,0,0,0));
UIManager.getDefaults().put("TabbedPane.tabsOverlapBorder", true);

You probably also want to make sure your JTabbedPane has an EmptyBorder(0,0,0,0) and so do the components you put in it.

If this doesn't work on your target desktop, the alternatives are

  • if you don't care about your tabbed panes looking different from native application tabbed panes, the (unpleasant) alternative is to write your own TabbedPaneUI
  • set the UI delegate for the single JTabbedPane you want to look like this to MetalTabbedPaneUI or some other UI delegate that does respond to these properties
树深时见影 2024-10-26 21:34:16

我刚刚遇到了同样的问题,没有其他人说似乎是一个完整的解决方案,所以我想出了这个:

    import javax.swing.plaf.basic.BasicTabbedPaneUI;

    tabbedPane.setUI(new BasicTabbedPaneUI() {
        private final Insets borderInsets = new Insets(0, 0, 0, 0);
        @Override
        protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
        }
        @Override
        protected Insets getContentBorderInsets(int tabPlacement) {
            return borderInsets;
        }
    });

它的工作原理是相同的,无需覆盖 PaintContentBorder 方法,但是这样做可能会使 UI 在调整大小或类似的过程中稍微更有效,因为标准方法似乎委托给许多其他方法。

在 WindowMaker 中的适用于 Linux 的 Oracle Java 6 u43、使用 Mac Java 6 u37 的 Mac OS X 10.6.7 和使用 Java 7 u07 的 Windows 7 上进行了测试,希望这对某人有帮助:-)

I just struck the same problem, and nothing anyone else said seemed to be a complete solution, so I came up with this:

    import javax.swing.plaf.basic.BasicTabbedPaneUI;

    tabbedPane.setUI(new BasicTabbedPaneUI() {
        private final Insets borderInsets = new Insets(0, 0, 0, 0);
        @Override
        protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
        }
        @Override
        protected Insets getContentBorderInsets(int tabPlacement) {
            return borderInsets;
        }
    });

It works the same without overriding the paintContentBorder method, however doing so probably makes the UI slightly more efficient during resizes or similar, as the standard one seems to delegate out to a number of other methods.

Tested on Oracle Java 6 u43 for Linux in WindowMaker, Mac OS X 10.6.7 with Mac Java 6 u37 and Windows 7 with Java 7 u07, hopefully this helps someone :-)

沒落の蓅哖 2024-10-26 21:34:16

通过在 UI 元素上设置边框来添加边距。查看 JTabbedPane 边框的设置。

Margins are added by setting borders on UI elements. Have a look at the settings of the border of your JTabbedPane.

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