-1

私はcontrolPanel( BoxLayout) を持っています:

controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));

次に、 2 つ作成してパネルFlowLayoutに追加します。contolPanel

JPanel fromDatePanel = new JPanel(new FlowLayout());
JPanel untilDatePanel = new JPanel(new FlowLayout());

fromDatePanel.add(new JLabel("From - "));
fromDatePanel.add(new JButton("..."));
untilDatePanel.add(new JLabel("Until - "));
untilDatePanel.add(new JButton("..."));

controlPanel.add(fromDatePanel);
controlPanel.add(untilDatePanel);

私はこれを得ています:

ここに画像の説明を入力

レイアウト間にギャップが生じるのはなぜですか? たとえば、を挿入すると、正常にJButton機能します(隙間なく挿入されます)。

2つの間のギャップをどのように取り除くことができFlowLayoutますか? (だからブルーギャップみたいになる)

4

3 に答える 3

2

GridBagLayoutを使用して(および を使用してGridBagConstraint)垂直レイアウト(垂直方向に中央揃え)を実現できますgridwidth=REMAINDER

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI() {
        JFrame frame = new JFrame();
        JPanel controlPanel = (JPanel) frame.getContentPane();
        controlPanel.setLayout(new GridBagLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        JPanel fromDatePanel = new JPanel(new FlowLayout());
        JPanel untilDatePanel = new JPanel(new FlowLayout());

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel, gbc);
        controlPanel.add(untilDatePanel, gbc);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGridBagLayout testMultiplePanels = new TestGridBagLayout();
                testMultiplePanels.initUI();

            }
        });
    }

}

に を追加した場合のJButtonとの違いについては、が考慮しているの実装の違いによるものです。は優先サイズを返しますが、は無限次元として解釈されるサイズを返します。JPanelBoxLayoutgetMaximumSize()BoxLayoutJButtonJPanelnullBoxLayout

を保持したい場合BoxLayoutは、オーバーライドJPanel.getMaximumSize()して返すことができますgetPreferredSize()

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI() {
        JFrame frame = new JFrame();
        JPanel controlPanel = (JPanel) frame.getContentPane();
        controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel fromDatePanel = new JPanel(new FlowLayout()) {
            @Override
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
        };
        JPanel untilDatePanel = new JPanel(new FlowLayout()) {
            @Override
            public Dimension getMaximumSize() {
                return super.getMaximumSize();
            }
        };

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel);
        controlPanel.add(untilDatePanel);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGridBagLayout testMultiplePanels = new TestGridBagLayout();
                testMultiplePanels.initUI();

            }
        });
    }

}
于 2013-06-06T11:37:05.640 に答える
2

については、非表示コンポーネントをフィラーとして使用するを参照してくださいBox.createVerticalGlue()

于 2013-06-06T10:30:16.330 に答える
1

ここに画像の説明を入力

私は使用BorderLayoutし、それは働いた。

        JPanel controlPanel = new JPanel();
        //  controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        controlPanel.setLayout(new BorderLayout());

        JPanel fromDatePanel = new JPanel(new FlowLayout());
        JPanel untilDatePanel = new JPanel(new FlowLayout());

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel, BorderLayout.NORTH);
        controlPanel.add(untilDatePanel, BorderLayout.CENTER);

編集-1:

各パネル内にネストされたパネルを追加できます。

ネストされたパネル

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        // controlPanel.setLayout(new BorderLayout());

        JPanel fromDatePanel = new JPanel(new FlowLayout());
        JPanel untilDatePanel = new JPanel(new FlowLayout());
        JPanel thirdPnl = new JPanel(new FlowLayout());
        JPanel fourthPnl = new JPanel(new FlowLayout());

        JPanel thrdForthPnl = new JPanel(new BorderLayout()); 

        fourthPnl.add(new JLabel("Fourth"));
        fourthPnl.add(new JButton("4.1"));

        thirdPnl.add(new JLabel("Third"));
        thirdPnl.add(new JButton("3.1"));

        thrdForthPnl.add(thirdPnl, BorderLayout.NORTH);
        thrdForthPnl.add(fourthPnl, BorderLayout.CENTER);

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel, BorderLayout.NORTH);
        controlPanel.add(untilDatePanel, BorderLayout.CENTER);
        controlPanel.add(thrdForthPnl, BorderLayout.SOUTH);
于 2013-06-06T10:59:38.503 に答える