为什么我的 JPanel 的首选大小没有改变,而其包含的 JScrollPane 在 GroupLayout 中却发生了变化?

发布于 2024-09-18 15:41:36 字数 2690 浏览 7 评论 0原文

使用 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

Using GroupLayout and the code below, I noticed that the leftPanel is not updating it's preferred size. I thought this was supposed to be automatic and appears to work in other layout managers. How can I get that behaviour with 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

客…行舟 2024-09-25 15:41:36

组件的优选大小基于添加到其中的组件的优选大小。

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

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

The preferred size of a component is based on the preferred size of the components added to it.

Yes, the preferred size of the JList will change as you add items to the list. However, the JList is added to a JScrollPane, so its the preferred size of the JScrollPane that is used to determine the preferred size of the JPanel.

Since you are using an IDE to create your GUI is looks like the preferred size of the JScrollPane is determined at design time and hardcoded into the constraint. The preferred size of the JScrollPane will not change as the frame is resized or as you add items to the JList. The "size" of the JScrollPane and JPanel will change as the frame is resized.

人生百味 2024-09-25 15:41:36

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

Not all LayoutManagers take Component.preferredSize() into account when doing their layout. The two that I know of that do are FlowLayout and BoxLayout (others might, but they might not).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文