正如您在上图中看到的,我有一个选项卡式 Pane 。在选项卡标题上,我有一个 JLabel
(选项卡测试)和一个 JButton
(X)。它们彼此相邻放置,但我希望它们有一个小间隙,以便看起来自然。
我尝试过使用 Box
,但它具有相同的背景和文本,使其看起来也不自然。 Box
没有 setBorders
方法。
这是使用 Box
时的样子:
这是我的代码:
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);
那么,如何为 JLabel
和 JButton
腾出空间并保持该距离的背景为中性?
最佳答案
我无法测试它,但我相信您只需告诉该框在将其添加到 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/