Swing 动态JPanel加法

Swing 动态JPanel加法,swing,jpanel,java,Swing,Jpanel,Java,我正在尝试创建一个动态Swing GUI。单击“添加/删除”按钮时,我需要添加/删除JPanels。我无法动态添加JPanels。最初JPanel加载,但JPanel的数组无法工作。我该怎么做 import java.awt.*; import java.awt.event.*; import java.io.*; import javax.print.attribute.standard.JobHoldUntil; import javax.swing.*; public class Ac

我正在尝试创建一个动态Swing GUI。单击“添加/删除”按钮时,我需要添加/删除
JPanels
。我无法动态添加
JPanels
。最初
JPanel
加载,但
JPanel的数组无法工作。我该怎么做

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.*;

public class AccessoryFileChooser2 extends JFrame {
  JFileChooser chooser = null;
  JLabel statusbar;
  JLabel file;
  JCheckBox checkBox;
  int count;
  int increment;
  JPanel [] panel = new JPanel[10]; //array

  public AccessoryFileChooser2() {
    setSize(350, 200);
    count=0;
    increment=1;
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    final Container c = getContentPane();

    c.setLayout(new BorderLayout());
    checkBox =new JCheckBox("");
    String fileName = "Choose File Name";
   file = new JLabel(fileName);
    final JButton accButton = new JButton("Browse");
    final JButton add = new JButton("Add");
    final JButton validate = new JButton("Validate");
    final JButton delete = new JButton("Delete");

    accButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        int option = chooser.showOpenDialog(AccessoryFileChooser2.this);
        if (option == JFileChooser.APPROVE_OPTION) {
          statusbar.setText(
           (
            chooser.getSelectedFile().getPath()));
        }
      }
    });

    statusbar = new JLabel("Output of your selection will go here");
    chooser = new JFileChooser();


   final JPanel panels =new JPanel();

   //JPanel panel2 =new JPanel();
   panel[count]=new JPanel();
   panel[count].add(checkBox);
   panel[count].add(file);
   panel[count].add(accButton );
   panel[count].add(statusbar);
   c.add(panel[count],BorderLayout.CENTER);
   panels.add(add);
   panels.add(delete);
   panels.add(validate);

   add.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {
          count=count+1;;
          increment=increment+1;;
          panel[count]=new JPanel();
           System.out.println("You clicked the ADD button");

           panel[count].add(checkBox);
           panel[count].add(file);
           panel[count].add(accButton );
           panel[count].add(statusbar);
           panel[count].revalidate();
           panel[count].repaint();
           panel[count].updateUI();
           c.add(panel[count]);

       }
   });

   delete.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {
           increment--;
           System.out.println("You clicked the Delete button");
           System.out.println(checkBox.isSelected());
           for (int i = 0; i < panel.length; i++) {
               JCheckBox box=(JCheckBox) panel[i].getComponent(0);
               if(box.isSelected()){
   c.remove(panel[i]);
   }
        }

       }
   });

   validate.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {
           System.out.println("You clicked the Validate button");
       }
   });


   c.add(panels,BorderLayout.SOUTH);

  }

  public static void main(String args[]) {
    AccessoryFileChooser2 afc = new AccessoryFileChooser2();
    afc.setVisible(true);
  }
}
import java.awt.*;
导入java.awt.event.*;
导入java.io.*;
导入javax.print.attribute.standard.JobHoldUntil;
导入javax.swing.*;
公共类AccessoryFileChooser2扩展了JFrame{
JFileChooser-chooser=null;
JLabel状态栏;
JLabel文件;
JCheckBox复选框;
整数计数;
整数增量;
JPanel[]panel=newjpanel[10];//数组
公共访问文件选择器2(){
设置大小(350200);
计数=0;
增量=1;
setDefaultCloseOperation(关闭时退出);
最终容器c=getContentPane();
c、 setLayout(新的BorderLayout());
复选框=新的JCheckBox(“”);
String fileName=“选择文件名”;
文件=新的JLabel(文件名);
最终JButton accButton=新JButton(“浏览”);
最终JButton add=新JButton(“add”);
最终JButton validate=新JButton(“validate”);
最终JButton delete=新JButton(“delete”);
accButton.addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae){
int option=chooser.showOpenDialog(AccessoryFileChooser2.this);
if(option==JFileChooser.APPROVE\u选项){
statusbar.setText(
(
chooser.getSelectedFile().getPath());
}
}
});
statusbar=new JLabel(“您选择的输出将转到此处”);
chooser=newjfilechooser();
最终JPanel面板=新JPanel();
//JPanel panel2=新的JPanel();
panel[count]=新的JPanel();
面板[计数]。添加(复选框);
面板[计数]。添加(文件);
面板[计数]。添加(ACC按钮);
面板[计数]。添加(状态栏);
c、 添加(面板[计数],边框布局.中间);
面板。添加(添加);
面板。添加(删除);
面板。添加(验证);
add.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e)
{
计数=计数+1;;
增量=增量+1;;
panel[count]=新的JPanel();
System.out.println(“您单击了添加按钮”);
面板[计数]。添加(复选框);
面板[计数]。添加(文件);
面板[计数]。添加(ACC按钮);
面板[计数]。添加(状态栏);
面板[count]。重新验证();
面板[count]。重新绘制();
面板[count].updateUI();
c、 添加(面板[计数]);
}
});
delete.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e)
{
增量--;
System.out.println(“您单击了删除按钮”);
System.out.println(checkBox.isSelected());
对于(int i=0;i
添加
c.validate()
c.Add(面板[计数])之后以更新GUI


还建议检查
JPanel
s的当前数量,因为这样,您将很快获得
ArrayIndexOutOfBoundsException

我在以下方面取得了成功:

repaint();
validate();

在运行时更改gui之后。当我在组件/容器上创建、删除、移动面板时,我调用这两种方法。

从可见GUI添加或删除组件时,一般代码应为:

panel.add(...);
panel.revalidate();
panel.repaint();
重新验证基本上调用布局管理器


重新喷漆可确保组件在布局发生变化时重新喷漆。

不正确,有很多新手错误

1/因为与您为
JPanels

JPanel [] panel = new JPanel[10]; 
您需要为其
JComponents
创建
Array
,因为一个JComponent只能添加一次

编辑:部分修订/更改代码显示此问题

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

public class AccessoryFileChooser2 extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFileChooser chooser = null;
    private JLabel statusbar;
    private JLabel file;
    private JCheckBox checkBox;
    private int count;
    private int increment = 10;
    private JPanel parentPanel = new JPanel();
    private JPanel panels = new JPanel();
    private JPanel[] panel = new JPanel[increment]; //array
    private JButton accButton = new JButton("Browse");
    private JButton addButton = new JButton("Add");
    private JButton validate = new JButton("Validate");
    private JButton delete = new JButton("Delete");

    public AccessoryFileChooser2() {
        count = 0;
        parentPanel.setLayout(new GridLayout(10, 1, 10, 10));
        checkBox = new JCheckBox("");
        String fileName = "Choose File Name";
        file = new JLabel(fileName);
        accButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                int option = chooser.showOpenDialog(AccessoryFileChooser2.this);
                if (option == JFileChooser.APPROVE_OPTION) {
                    statusbar.setText((chooser.getSelectedFile().getPath()));
                }
            }
        });
        statusbar = new JLabel("Output of your selection will go here");
        chooser = new JFileChooser();
        panel[count] = new JPanel();
        panel[count].add(checkBox);
        panel[count].add(file);
        panel[count].add(accButton);
        panel[count].add(statusbar);
        panel[count].setPreferredSize(new Dimension(450, 40));
        panel[count].setBorder(new LineBorder(Color.BLACK, 1));
        parentPanel.add(panel[count]);
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (count < increment - 1) {
                    count += 1;
                    panel[count] = new JPanel();
                    System.out.println("You clicked the ADD button");
                    panel[count].add(checkBox);
                    panel[count].add(file);
                    panel[count].add(accButton);
                    panel[count].add(statusbar);
                    panel[count].revalidate();
                    panel[count].repaint();
                    //panel[count].updateUI();
                    parentPanel.add(panel[count]);
                    parentPanel.revalidate();
                    parentPanel.repaint();
                    pack();
                    if (count == increment - 1) {
                        addButton.setEnabled(false);
                    }
                }
            }
        });
        delete.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("You clicked the Delete button");
                System.out.println(checkBox.isSelected());
                /*for (int i = 0; i < parentPanel.getComponentCount(); i++) {
                JCheckBox box = (JCheckBox) panel[i].getComponent(0);
                if (box.isSelected()) {
                parentPanel.remove(panel[i]);
                }
                }*/
            }
        });
        validate.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("You clicked the Validate button");
            }
        });
        panels.setLayout(new GridLayout(1, 3, 10, 10));
        panels.add(addButton);
        panels.add(delete);
        panels.add(validate);
        add(parentPanel, BorderLayout.CENTER);
        add(panels, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        AccessoryFileChooser2 afc = new AccessoryFileChooser2();
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.LineBorder;
公共类AccessoryFileChooser2扩展了JFrame{
私有静态最终长serialVersionUID=1L;
private JFileChooser chooser=null;
专用JLabel状态栏;
专用JLabel文件;
私有JCheckBox复选框;
私人整数计数;
私有整数增量=10;
private JPanel parentPanel=new JPanel();
私有JPanel面板=新JPanel();
private JPanel[]panel=new JPanel[increment];//数组
私有JButton accButton=新JButton(“浏览”);
私有JButton addButton=新JButton(“添加”);
私有JButton validate=新JButton(“validate”);
私有JButton delete=新JButton(“delete”);
公共访问文件选择器2(){
计数=0;
setLayout(新的GridLayout(10,1,10,10));
复选框=新的JCheckBox(“”);
String fileName=“选择文件名”;
文件=新的JLabel(文件名);
accButton.addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae){
int option=chooser.showOpenDialog(AccessoryFileChooser2.this);
if(option==JFileChooser.APPROVE\u选项){
statusbar.setText((chooser.getSelectedFile().getPath());
}
}
});
statusbar=new JLabel(“您选择的输出将转到此处”);
chooser=newjfilechooser();
panel[count]=新的JPanel();
面板[计数]。添加(复选框);
面板[count]。添加(fil)