1

使用 GroupLayout 和下面的代码,我注意到 leftPanel 没有更新它的首选大小。我认为这应该是自动的,并且似乎可以在其他布局管理器中使用。如何使用 GroupLayout 获得这种行为?

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
4

2 回答 2

1

组件的首选大小取决于添加到其中的组件的首选大小。

是的,JList 的首选大小会随着您向列表中添加项目而改变。但是,JList 被添加到 JScrollPane,因此它是 JScrollPane 的首选大小,用于确定 JPanel 的首选大小。

由于您使用 IDE 创建 GUI,因此 JScrollPane 的首选大小似乎在设计时确定并硬编码到约束中。JScrollPane 的首选大小不会随着框架大小的调整或向 JList 添加项目而改变。JScrollPane 和 JPanel 的“大小”将随着框架大小的调整而改变。

于 2010-09-10T03:39:26.877 回答
1

并非所有 LayoutManager 在进行布局时都会考虑 Component.preferredSize()。我所知道的两个是 FlowLayout 和 BoxLayout (其他可能,但他们可能不会)。

于 2010-09-09T23:50:10.077 回答