简体   繁体   English

JScrollPane中的GridBagLayout无法正确调整大小

[英]GridBagLayout within JScrollPane not resizing properly

I have a JPanel with a GridBagLayout inside of a JScrollPane. 我在JScrollPane内部有一个GridBagLayout的JPanel。 I also have an 'add' button within the JPanel which, when clicked, will be removed from the JPanel, adds a new instance of a separate component to the JPanel, then adds itself back to the JPanel. 我在JPanel中还有一个“添加”按钮,当单击该按钮时,将从JPanel中删除该按钮,将一个单独组件的新实例添加到JPanel,然后将其自身添加回JPanel。 This sort of makes a growing list of components, followed by the 'add' button. 这种类型的组件列表越来越多,其次是“添加”按钮。

Adding new components works fine, the JPanel stretches to accommodate the new components, and the JScrollPane behaves as expected, allowing you to scroll through the entire length of the JPanel. 添加新组件效果很好,JPanel可以扩展以容纳新组件,并且JScrollPane的行为与预期的一样,允许您滚动整个JPanel的长度。

This is how the add works: 这是添加的工作方式:

jPanel.remove(addButton);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIVE;
jPanel.add(new MyComponent(), c);
jPanel.add(addButton, c);
jPanel.validate();
jPanel.repaint();`

Removal works by clicking a button inside the added components themselves. 单击添加的组件本身内部的按钮即可进行删除。 They remove themselves from the JPanel just fine. 他们可以将自己从JPanel中删除。 However, the JPanel keeps it's stretched-out size, re-centering the list of components. 但是,JPanel会保持其扩展大小,从而将组件列表重新居中。

This is how removal works: 这是删除的工作方式:

Container parent = myComponent.getParent();
parent.remove(myComponent);
parent.validate();
parent.repaint();`

The question is, why does my GridBagLayout JPanel resize when adding components, but not when removing components? 问题是,为什么添加组件时我的GridBagLayout JPanel会调整大小,而删除组件时却不调整呢?

You have to revalidate and repaint the JScrollPane, here is an example: 您必须重新验证并重新绘制JScrollPane,这是一个示例:

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

public class SwingTest {

    public static void main(String[] args) {
        final JPanel panel = new JPanel(new GridBagLayout());

        for (int i = 0; i < 25; i++) {
            JTextField field = new JTextField("Field " + i, 20);

            GridBagConstraints constraints = new GridBagConstraints();
            constraints.gridy = i;

            panel.add(field, constraints);
        }

        final JScrollPane scrollPane = new JScrollPane(panel);

        JButton removeButton = new JButton("Remove Field");
        removeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (panel.getComponentCount() >= 1) {
                    panel.remove(panel.getComponentCount() - 1);
                    scrollPane.revalidate();
                    scrollPane.repaint();
                }
            }
        });


        JFrame frame = new JFrame("Swing Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setLocation(200, 200);
        frame.getContentPane().add(scrollPane);
        frame.getContentPane().add(removeButton, BorderLayout.SOUTH);

        frame.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM