0

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

2 回答 2

1

你需要用一些东西填充面板的其余部分,使用 swing.Box

import javax.swing.Box;
rightPanel.add(Box.createVerticalGlue());

将盒子放在您希望空间出现的位置

有关该主题的更多信息http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler

您还需要限制输入框的最大高度(对于每个输入调用 - 可能是创建方法):

input.setMaximumSize(new Dimension(Integer.MAX_VALUE, input.getMinimumSize().height));
于 2013-11-10T13:46:28.780 回答
0

您可以将面板放在 BorderLayout 的 NORTH 部分——在 BorderLayout 的 EAST 或 CENTER 部分,面板向上/向下延伸,但不在北部。而且,当然,您可以将它与那个 BorderLayout 一起放在它自己的 JPanel 中,然后根据需要将那个 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();
  }

}
于 2013-11-10T15:26:14.277 回答