我有controlPanel
(BoxLayout
):
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
现在我构建两个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
,它可以正常工作(它插入它们没有间隙)。
如何消除两个FlowLayout
之间的差距? (所以它就像蓝色的差距)
答案 0 :(得分:2)
请参阅Using Invisible Components as Filler关于Box.createVerticalGlue()
。
答案 1 :(得分: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
时JPanel
和BoxLayout
之间的差异,这是由于getMaximumSize()
的实施差异所致。 BoxLayout
。 JButton
会返回首选尺寸,而JPanel
将返回null
,BoxLayout
将其解释为无限尺寸。
如果您想保留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();
}
});
}
}
答案 2 :(得分: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);