为什么这段代码会产生比例空的空间?

时间:2012-03-10 13:45:58

标签: java swing layout

为什么以下代码单独放在JPanel子类的构造函数中,会在split_pane左边但在mQueryField之上生成一个比例空的空格?

mMessageArea = new JTextArea ();
JScrollPane message_pane = new JScrollPane (mMessageArea);

mTableView = new JTable ();
mTableView.setFillsViewportHeight (true);
JScrollPane table_pane = new JScrollPane (mTableView);

JSplitPane split_pane = new JSplitPane (JSplitPane.VERTICAL_SPLIT,
        message_pane, table_pane);

mQueryField = new JTextField ();
mQueryField.setMaximumSize (new Dimension (Short.MAX_VALUE, 20));

setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (split_pane);
add (mQueryField);

编辑 - SSCCE:

import java.awt.Dimension;    
import javax.swing.*;

public class SSCCE extends JPanel
{

    private JTextArea mMessageArea;
    private JTable mTableView;
    private JTextField mQueryField;

    public SSCCE ()
    {
        mMessageArea = new JTextArea ();
        JScrollPane message_pane = new JScrollPane (mMessageArea);

        mTableView = new JTable ();
        mTableView.setFillsViewportHeight (true);
        JScrollPane table_pane = new JScrollPane (mTableView);

        JSplitPane split_pane = new JSplitPane (JSplitPane.VERTICAL_SPLIT,
                message_pane, table_pane);

        mQueryField = new JTextField ();
        mQueryField.setMaximumSize (new Dimension (Short.MAX_VALUE, 20));

        setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
        add (split_pane);
        add (mQueryField);
    }

    public static void main (String[] args)
    {
        JFrame frame = new JFrame ();
        frame.add (new SSCCE ());
        frame.setVisible (true);
        frame.pack ();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }

}

2 个答案:

答案 0 :(得分:2)

也许是因为JSplitPane的最大尺寸不够大。您可以将其设置或嵌套在使用BorderLayout的JPanel中:

  JSplitPane split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
        message_pane, table_pane);
  JPanel splitHolder = new JPanel(new BorderLayout());
  splitHolder.add(split_pane);

  mQueryField = new JTextField();
  mQueryField.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));

  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  add(splitHolder);
  add(mQueryField);
顺便说一句,SSCCE为1+。它使您的问题更多更容易。

修改
我错了,问题只是对齐,因为JTextFields默认为Component.CENTER_ALIGNMENT的x对齐。设置解决方案将X对齐设置为左侧:

  JSplitPane split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
        message_pane, table_pane);
  System.out.println("split_pane.getAlignmentX() before:"
        + split_pane.getAlignmentX());
  split_pane.setAlignmentX(LEFT_ALIGNMENT); // NOT REALLY NEEDED
  System.out.println("split_pane.getAlignmentX() after:"
        + split_pane.getAlignmentX());

  mQueryField = new JTextField();
  mQueryField.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
  System.out.println("mQueryField.getAlignmentX() before: "
        + mQueryField.getAlignmentX());
  mQueryField.setAlignmentX(LEFT_ALIGNMENT);
  System.out.println("mQueryField.getAlignmentX() after: "
        + mQueryField.getAlignmentX());

答案 1 :(得分:2)

JSplitPane在方法pack()后知道自己的大小,使用BorderLayout

可以解决此问题