【问题标题】:JTabbedPane how to have multiple rows of tabs, without the extra spacing/padding at the bottom of each tab panel?JTabbedPane 如何拥有多行选项卡,而每个选项卡面板底部没有额外的间距/填充?
【发布时间】:2019-12-27 14:28:51
【问题描述】:

JTabbedPane why is there extra padding only when I have multiple tabs? (code and picture)的某种跟进

我有很多标签,所以我希望有多行标签以避免过度滚动。这使得 SCROLL_TAB_LAYOUT 不受欢迎,因为它创建了单行选项卡(如左图所示)。

使用 WRAP_TAB_LAYOUT 可以让我拥有多行选项卡。但是,我在每个选项卡面板的底部都有额外的空间(由右图中的大红色区域显示)。

有没有办法获得多行制表符,而没有 WRAP_TAB_LAYOUT 丑陋的额外空格?

这里是一些创建 JTabbedPane 的代码。有大量标签(20+),两种布局都不令人满意。

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();
            tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); // change to WRAP_TAB_LAYOUT for other layout
            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);
        }   
    }
}

【问题讨论】:

  • 这是您在其他地方的评论中询问的内容吗?
  • 是的,它是(来自链接的帖子)。
  • 更多相关信息在这里:stackoverflow.com/questions/34944950/…
  • 有必要添加到BorderLayout.NORTH吗?这导致了您看到的一些影响。

标签: java swing jpanel jtabbedpane


【解决方案1】:

通过将大小设置为JDialog,您可以定义一个需要被添加到其中的组件填充的区域。
看来需要JDialog的内容来定义区域,所以首先想到的是去掉setPreferredSize(new Dimension(400, 200));,让布局管理器定义合适的大小。

接下来,您需要使用其布局管理器将内容 (MyPanel) 设置为所需的大小和布局。在以下 MRE GridBagLayout 用于此目的:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
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();
            //tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
            tabs.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);

            for (int i = 1; i <= 12; i++) {  //12 as an arbitrary test value
                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);

            setBackground(Color.RED);
            GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.columnWidths = new int[]{400, 0}; //affects parent width 
            gridBagLayout.rowHeights = new int[]{100, 0};   //affects parent height
            gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
            gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
            setLayout(gridBagLayout);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 0;

            add(label, gbc);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多