JTabbedPane为什么只有当我有多个标签时才有额外的填充? (代码和图片) [英] JTabbedPane why is there extra padding only when I have multiple tabs? (code and picture)
问题描述
我有一个JTabbed窗格,其中包含不同数量的选项卡.当突片的数量大于4,我得到额外的间距/填充在每个标签面板的底部.下图显示了这一点(在左侧看不到额外的间距,在右侧看不到额外的间距).
I have a JTabbed pane, which has a varying number of tabs. When the number of tabs is greater than 4, I get extra spacing/padding at the bottom of each tab panel. The picture below shows this (on the left you see the extra spacing, on the right you see no extra spacing).
这是我用来获取这些图片的确切代码:
Here is the exact code I used to get those pictures:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class DialogTest {
public static void main(String[] args) {
new DialogTest();
}
public DialogTest() {
JDialog dialog = new MyDialog();
dialog.pack();
dialog.setVisible(true);
}
class MyDialog extends JDialog {
public MyDialog() {
super(null, ModalityType.APPLICATION_MODAL);
final JTabbedPane tabs = new JTabbedPane();
final int numTabs = Integer.parseInt(JOptionPane.showInputDialog("Number of tabs:"));
setPreferredSize(new Dimension(400, 200));
for (int i = 1; i <= numTabs; i++) {
tabs.addTab("Tab"+i, new MyPanel(i));
}
setLayout(new BorderLayout());
add(tabs, BorderLayout.NORTH);
}
}
class MyPanel extends JPanel {
public MyPanel(int text) {
final JLabel label = new JLabel("THIS IS A PANEL" + text);
label.setFont(label.getFont().deriveFont(18f));
label.setBackground(Color.cyan);
label.setOpaque(true);
add(label);
setBackground(Color.red);
}
}
}
我尝试了很多事情,包括许多不同的布局管理器.我一生都无法摆脱多余的空间.任何帮助都会很棒.
I've tried numerous things including many different layout managers. I can't for the life of me get rid of that extra spacing. Any help would be great.
推荐答案
final JTabbedPane tabs = new JTabbedPane();
tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); // ADD THIS!
另一个示例如此操作的原因是,窗格将选项卡包装到下一行&假定一旦我们超出了可能在一行中自然显示的选项卡的数量,就必须增加首选大小以包括多余的选项卡行.
The reason the other example behaves as it does is that the pane wraps the tabs to the next line & presumes that once we have gone beyond as many tabs as it might naturally display in a single line, it must increase the preferred size to include that extra line of tabs.
这篇关于JTabbedPane为什么只有当我有多个标签时才有额外的填充? (代码和图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!