java - 在选项卡式 Pane 标题上的标签和 jButton 之间添加空格

标签 java swing tabs jtabbedpane

enter image description here

正如您在上图中看到的,我有一个选项卡式 Pane 。在选项卡标题上,我有一个 JLabel (选项卡测试)和一个 JButton (X)。它们彼此相邻放置,但我希望它们有一个小间隙,以便看起来自然。

我尝试过使用 Box,但它具有相同的背景和文本,使其看起来也不自然。 Box 没有 setBorders 方法。

这是使用 Box 时的样子:

enter image description here

这是我的代码:

        System.out.println("NewTableEvent!!!!");
        final String tittle = table.getTabName();

        JButton jButtonClose = new JButton("X");
        jButtonClose.setBorderPainted(false);
        jButtonClose.setBorder(null);

        JPanel tabComponent = new JPanel(new BorderLayout());
        tabComponent.add(new JLabel(tittle), BorderLayout.WEST);
        tabComponent.setToolTipText("Close this tab.");
        Component box = Box.createRigidArea(new Dimension(25,0));
        tabComponent.add(box, BorderLayout.CENTER);
        tabComponent.add(jButtonClose, BorderLayout.EAST);


//        rightTabbedPane.addTab(null, table.getTable());
        rightTabbedPane.addTab(null, new JPanel());

        // Get total tabs
        final int totalTabs = rightTabbedPane.getComponentCount();
        System.out.println("Total tabs: " + totalTabs);

        // Set the custom tab component
        rightTabbedPane.setTabComponentAt(0, tabComponent);

那么,如何为 JLabelJButton 腾出空间并保持该距离的背景为中性?

最佳答案

我无法测试它,但我相信您只需告诉该框在将其添加到 tabComponent 之前不要不透明:

box.setOpaque(false);

希望这对您有用。

编辑

您可以设置标签和按钮周围的边框来实现此目的:

JLabel label = new JLabel(tittle);
tabComponent.add(label);
//add more space between the label and the button
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
tabComponent.add(jButtonClose);

演示地址:http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html

关于java - 在选项卡式 Pane 标题上的标签和 jButton 之间添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25371790/

相关文章:

javascript - 使用 ajaxSubmit 替换 DOM 部分时,我应该如何处理 tinyMCE?

java - 在eclipse中编译一个项目,得到war或ear或jar文件

java - 如何使用 Junit 和 Java 模拟和编写 Azure 服务总线主题的单元测试?

java - 我的 JScrollPane 不会在 JPanel 上垂直滚动

arrays - Array[Nothing with java.lang.Object] 在 Scala 2.9.1 中需要

java - 获取事件源而不是编写它

javascript - 将数据从选项卡 1 传递到选项卡 2

java - 错误-java.lang.ClassNotFoundException : oracle. jdbc.driver.OracleDriver

java - 模仿java中的printStackTrace()方法

Xcode 4.1行为-自动关闭标签页吗?