如何将组件动态添加到 Java JScrollPane

use*_*493 4 java user-interface swing

我正在尝试用 Java 制作 GUI 应用程序,但在 JScrollPane 中动态添加/更新组件时遇到问题。我有两个 JPanel(P1 和 P2),其中 P1 有一个表单来设置应用程序参数,P2 包含一些 GUI 组件,这些组件根据 P1 中的值动态更新。我需要在 P2 上使用 JScrollPane 来滚动,所以我在 P2 中添加了 JScrollPane。我将 P1 和 P2 添加到主面板“main”中,然后将主面板添加到框架中。但是组件在 P2 中没有更新。有人可以建议有什么问题吗?我已经调用了 revalidate()、repaint() 和其他一些方法,但是 GUI 没有更新。下面是我编写的示例代码,只是为了说明我的问题。我的应用程序中需要 GroupLayout 所以这里我也使用了 GroupLayout

        import java.awt.event.ActionEvent;
        import javax.swing.GroupLayout;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.JScrollPane;


        public class JframeExample extends JFrame {

            private final JPanel P1;
            private final JPanel P2;
            private final JPanel main;
            private final JScrollPane scrol;
            private final JButton jButton;
            private final JButton jButton2;

            public JframeExample() {
                P1 = new JPanel();
                P2 = new JPanel();
                main = new JPanel();
                jButton = new JButton("Add");
                jButton2 = new JButton("Remove");
                scrol = new JScrollPane();
                initialize();
                this.add(main);
                this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                this.setSize(400, 400);
                this.setVisible(true);

            }

            public static void main(String[] args) {
                JframeExample jframeExample = new JframeExample();

            }

            private void addPressed(ActionEvent evt) {
                System.out.println("Add Pressed");
                scrol.add(new JButton());
                revalidate();
            }

            private void removePressed(ActionEvent evt) {
                System.out.println("Remove Pressed");
                scrol.removeAll();
                revalidate();
            }

            private void initialize() {
                GroupLayout layout = new GroupLayout(P1);
                P1.setLayout(layout);
                layout.setAutoCreateGaps(true);
                layout.setAutoCreateContainerGaps(true);
                GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
                hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton).addComponent(jButton2));

                layout.setHorizontalGroup(hGroup);
                GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
                vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jButton))
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jButton2));
                layout.setVerticalGroup(vGroup);
                P2.add(scrol);
                jButton.addActionListener((ActionEvent evt) -> {
                    addPressed(evt);
                });
                jButton2.addActionListener((ActionEvent evt) -> {
                    removePressed(evt);
                });
                GroupLayout layoutMain = new GroupLayout(main);
                main.setLayout(layoutMain);
                layoutMain.setAutoCreateGaps(true);
                layoutMain.setAutoCreateContainerGaps(true);
                layoutMain.setHorizontalGroup(layoutMain.createSequentialGroup()
                        .addComponent(P1).addComponent(P2));
                layoutMain.setVerticalGroup(layoutMain
                        .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(P1)
                        .addComponent(P2));

            }

        }
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 5

将 P2 包裹在 JScrollPane 中也不起作用。

是的,确实如此,因为这就是它的工作方式。如果您花时间阅读如何使用滚动窗格,检查示例,甚至查阅JavaDocs,它会为您提供启动和运行 UI 所需的基本信息。

在此处输入图片说明

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class JframeExample extends JFrame {

    private final JPanel P1;
    private final JPanel P2;
    private final JPanel main;
    private final JScrollPane scrol;
    private final JButton jButton;
    private final JButton jButton2;

    public JframeExample() {
        P1 = new JPanel();
        P2 = new JPanel();
        main = new JPanel();
        jButton = new JButton("Add");
        jButton2 = new JButton("Remove");
        scrol = new JScrollPane(P2);
        initialize();
        this.add(main);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        this.setSize(400, 400);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        JframeExample jframeExample = new JframeExample();

    }

    private void addPressed(ActionEvent evt) {
        System.out.println("Add Pressed");
        P2.add(new JButton());
        revalidate();
    }

    private void removePressed(ActionEvent evt) {
        System.out.println("Remove Pressed");
        P2.removeAll();
        revalidate();
    }

    private void initialize() {
        main.setLayout(new GridLayout(1, 2));
        main.add(P1);
        main.add(scrol);
        jButton.addActionListener((ActionEvent evt) -> {
            addPressed(evt);
        });
        jButton2.addActionListener((ActionEvent evt) -> {
            removePressed(evt);
        });
        P1.add(jButton);
        P1.add(jButton2);
    }

}
Run Code Online (Sandbox Code Playgroud)

警告字GroupLayout真的不是为了手工编码,它真的是为 UI 编辑器设计的。

  • 尝试在 `add/removePressed` 方法中使用 `P2.revalidate()` 和 `P2.repaint()` 而不是仅仅使用 `revalidate` (2认同)