我有panel
,我希望内容垂直显示。为此,我正在使用这行代码:
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
当我使用textfields
时,我希望他们占用一行。但是通过使用上面指定的代码行,面板内的textfields
最大化,当我更改窗口大小时,textfield
的大小会相应地更改。我也试过删除它,但随后面板内的内容水平显示,不适合面板。
以下是结果的图像:
以下是创建面板内容的代码:
//Creates the form for data and button to save it.
final TextField name;
final TextField eMail;
final TextField dateOfBirth;
final TextField address;
JLabel nameLabel = new JLabel("Name:");
name = new TextField ();
JLabel eMailLabel = new JLabel("E-mail:");
eMail = new TextField ();
JLabel dateOfBirthLabel = new JLabel("Date of birth:");
dateOfBirth = new TextField ();
JLabel addressLabel = new JLabel("Address:");
address = new TextField ();
rightPanel.add(nameLabel);
rightPanel.add(name);
rightPanel.add(eMailLabel);
rightPanel.add(eMail);
rightPanel.add(dateOfBirthLabel);
rightPanel.add(dateOfBirth);
rightPanel.add(addressLabel);
rightPanel.add(address);
rightPanel.add(Box.createRigidArea(new Dimension(20, 20)));
JButton save = new JButton("Save");
rightPanel.add(save);
答案 0 :(得分:1)
你需要用某些东西填充面板的其余部分,使用swing.Box
import javax.swing.Box;
rightPanel.add(Box.createVerticalGlue());
将框放在您想要空间的位置
有关该主题的更多信息http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler
您还需要限制输入框的最大高度(对于每个输入调用 - 可能是create方法):
input.setMaximumSize(new Dimension(Integer.MAX_VALUE, input.getMinimumSize().height));
答案 1 :(得分:0)
您可以将面板放在BorderLayout的NORTH部分 - 在BorderLayout的EAST或CENTER部分中,面板向上/向下拉伸,但不在北部。当然,您可以将它放在带有BorderLayout的自己的JPanel中,然后将THAT JPanel放在另一个布局中,无论您想要什么。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.TextField;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RightPanel extends JFrame
{
TextField name;
TextField eMail;
TextField dateOfBirth;
TextField address;
JPanel rightPanel = new JPanel();
public static void main(String[] args)
{
RightPanel rp = new RightPanel();
rp.createUI();
rp.setVisible(true);
}
public void createUI()
{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//Creates the form for data and button to save it.
JLabel nameLabel = new JLabel("Name:");
name = new TextField ();
JLabel eMailLabel = new JLabel("E-mail:");
eMail = new TextField ();
JLabel dateOfBirthLabel = new JLabel("Date of birth:");
dateOfBirth = new TextField ();
JLabel addressLabel = new JLabel("Address:");
address = new TextField ();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
rightPanel.add(nameLabel);
rightPanel.add(name);
rightPanel.add(eMailLabel);
rightPanel.add(eMail);
rightPanel.add(dateOfBirthLabel);
rightPanel.add(dateOfBirth);
rightPanel.add(addressLabel);
rightPanel.add(address);
rightPanel.add(Box.createRigidArea(new Dimension(20, 20)));
JButton save = new JButton("Save");
rightPanel.add(save);
JPanel doNotStretchPanel = new JPanel();
doNotStretchPanel.setLayout(new BorderLayout());
doNotStretchPanel.add(rightPanel, BorderLayout.NORTH);
this.add(doNotStretchPanel, BorderLayout.EAST);
pack();
}
}