用Java格式化面板内容

时间:2013-11-10 13:31:51

标签: java swing jpanel

我有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);

2 个答案:

答案 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();
  }

}