有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing如何向Java JScrollPane动态添加组件

我试图用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));

            }

        }

共 (1) 个答案

  1. # 1 楼答案

    Wrapping P2 inside JScrollPane also does not work.

    是的,确实如此,因为这就是它的工作方式。如果你花时间通读How to use scroll panes,检查一下例子,甚至可以参考JavaDocs,它会为你提供基本信息,让你的UI启动并运行

    enter image description here

    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);
        }
    
    }
    

    警告之词GroupLayout真的不是为了手工编码,而是为UI编辑器设计的