本文介绍了如何将组件动态添加到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

I am trying to make a GUI application in Java but I am having trouble in adding/updating components dynamically in JScrollPane. I have two JPanels (P1 and P2) in which P1 has a form to set parameters for application and the P2 contains some GUI components, which are updated dynamically based on the values in the P1. I need a JScrollPane on P2 to scroll, so I added JScrollPane in P2. I added both P1 and P2 to a main panel "main" and then added the main panel to a frame. But the components are not updated in P2. Can someone suggest what is the problem? I have called revalidate(), repaint() and some other methods but GUI is not updated. Below is a sample code I have written just to illustrate my problem. I need GroupLayout in my application so here I also used 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));

            }

        }


推荐答案

是的,因为这是它的工作方式。如果您花时间阅读,检查示例,甚至可以参考它将为您提供启动和运行UI所需的基本信息。

Yes it does, because that's the way it work. If you take the time to read through the How to use scroll panes, examine the examples and maybe even consult the JavaDocs it would provide you with the basic information you'd need to get you UI up and running.

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编辑设计的。

Word of warning GroupLayout really isn't meant for hand coding, it's really designed for UI editors.

这篇关于如何将组件动态添加到Java JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 23:12