gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:10 24 4
gpt4 key购买 nike

为什么以下代码单独放置在 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);
}

}

最佳答案

可能是因为 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());

关于java - 为什么这段代码会产生成比例的空白空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9646928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com