如何将组件动态添加到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)); } } 

在JScrollPane中包装P2也不起作用。

是的,它确实如此,因为它的工作方式。 如果您花时间阅读如何使用滚动窗格 ,请查看示例,甚至可以参考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); } } 

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