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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5183687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:52:10  来源:igfitidea点击:

Java: remove margin / padding on a JTabbedPane

javaswingjtabbedpanemargins

提问by florian

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.

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

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

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

So any suggestions are welcome. Thanks.

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

The image is here.

图片在这里.

I can't post images yet.

我还不能发图片。

回答by Tim Boudreau

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.

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

You canset this globally for all tabbed panes (caveat: Works on MetalLookAndFeel, will probablywork for Windows L&F as well, probably won'twork 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:

可以为所有选项卡式窗格全局设置此项(警告:适用于 MetalLookAndFeel,可能也适用于 Windows L&F,可能不适用于不基于 BasicLookAndFeel 的 GTK 或 Nimbus 外观和感觉)。这将更改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.

您可能还想确保您的 JTabbedPane 具有 EmptyBorder(0,0,0,0) 并且您放入其中的组件也是如此。

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 doesrespond to these properties
  • 如果您不关心您的选项卡式窗格看起来与本机应用程序选项卡式窗格不同,那么(令人不快的)替代方法是编写您自己的 TabbedPaneUI
  • 设置UI委托单JTabbedPane的你想看起来像这样MetalTabbedPaneUI或一些其他UI委托回应这些特性

回答by FredCooke

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.

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

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

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

回答by Aaron Digulla

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

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