当我的jpanel所包含的jscrollpane在grouplayout中所做的jpanel首选尺寸为什么不变?

StackOverflow https://stackoverflow.com/questions/3680531

  •  02-10-2019
  •  | 
  •  

使用Grouplayout和下面的代码,我注意到左派没有更新其首选尺寸。我认为这应该是自动的,并且似乎在其他布局经理中工作。如何通过Groplayout获得这种行为?

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SizeTesting {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);

        JSplitPane splitPane = new JSplitPane();

        final JPanel leftPanel = new JPanel();
        final DefaultListModel listModel = new DefaultListModel();
        final JList list = new JList(listModel);
        final JScrollPane jsp = new JScrollPane(list);
        leftPanel.add(jsp);

        splitPane.setLeftComponent(leftPanel);

        GroupLayout panel1Layout = new GroupLayout(leftPanel);
        leftPanel.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup()
                .addComponent(jsp, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
        );
        panel1Layout.setVerticalGroup(
        panel1Layout.createParallelGroup()
            .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jsp, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))
        );



        JPanel rightPanel = new JPanel();

        JButton button = new JButton("Add Long Item");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("Preferred Size of List BEFORE: " + list.getPreferredSize());
                System.out.println("Preferred Size of ScorllPane BEFORE: " + jsp.getPreferredSize());
                System.out.println("Preferred size of LeftPanel BEFORE: " + leftPanel.getPreferredSize());
                System.out.println();

                listModel.addElement("Really long, new Item in List");
                System.out.println("Preferred Size of List AFTER: " + list.getPreferredSize());
                System.out.println("Preferred Size of ScorllPane AFTER: " + jsp.getPreferredSize());
                System.out.println("Preferred size of LeftPanel AFTER: " + leftPanel.getPreferredSize());
            }
        });
        rightPanel.add(button);
        splitPane.setRightComponent(rightPanel);

        listModel.addElement("Test");
        listModel.addElement("Test2");
        listModel.addElement("Test3");

        frame.add(splitPane);
        frame.setVisible(true);
    }
} // end class SizeTesting
有帮助吗?

解决方案

组件的首选尺寸基于添加到它的组件的首选大小。

是的,当您将项目添加到列表中时,JLIST的首选大小将更改。但是,将JLIST添加到JSCrollpane中,因此其首选的JSCrollpane尺寸用于确定JPANEL的首选尺寸。

由于您正在使用IDE来创建GUI,就像在设计时确定JSCrollpane的首选大小,并进行了硬编码。 JSCrollpane的首选尺寸不会随着调整框架的大小或将项目添加到JLIST而不会更改。随着调整框架的大小,JSCrollpane和Jpanel的“大小”将更改。

其他提示

并非所有的LayoutManagers在执行布局时都会考虑component.preferredsize()。我知道的两个是Flowlayout和Boxlayout(其他可能,但可能不是)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top