请问在JPanel中怎样动态删除他上面的控件。假设JPanel(使用box-y的layout)只添加了JButton控件,这些按钮都添加了actionLIstener当点击某个按钮时就删除被点击的按钮,就以删除JButton为例,我尝试用循环和getComponent(int  i)方法获得JPanel中的JButton,将getComponent方法得到的按钮的text属性与事件参数e的actionCommand比较如果相等则找到要删除的按钮,然后用panel的remove方法删除之。但是这种做法却不能实现我的目的,如果有多个按钮则第一次会删除最后一个(即使我点击第一个按钮),再点击其他按钮就会产生异常,总之就不能删除我点击的那个按钮,为什么呢?源程序如下:
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.WindowConstants;
public class NewJPanel extends javax.swing.JPanel {
private JButton jButton1;
private static JPanel p;
int i=0;
/**
* Auto-generated main method to display this 
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();

frame.getContentPane().add(NewJPanel.p=new NewJPanel());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public NewJPanel() {
super();
initGUI();
jButton1.addActionListener(new listener());
}

private void initGUI() {
try {
BoxLayout thisLayout = new BoxLayout(
this,
javax.swing.BoxLayout.Y_AXIS);
this.setLayout(thisLayout);
this.setPreferredSize(new java.awt.Dimension(193, 300));
{
jButton1 = new JButton();
this.add(jButton1);
jButton1.setText("jButton1");
}
} catch (Exception e) {
e.printStackTrace();
}
}

class listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jButton1)
{
JButton jb=new JButton(""+i);
jb.addActionListener(new relistener());

NewJPanel.p.add(jb);
p.updateUI();
// jButton1.setText(""+i);
i++;
}
}
}

class relistener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton j;
System.out.println(p.getComponentCount());
System.out.println(e.getActionCommand());
for(int i=0;i<p.getComponentCount();i++)
{
if(((JButton)p.getComponent(i)).getText().equals(e.getActionCommand()));
break;
}
System.out.println(((JButton)p.getComponent(i)).getText());
p.remove(i);
p.updateUI();
System.out.println(p.getComponentCount());
}
}}

解决方案 »

  1.   

    package ui;import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JPanel;import framework.Console;public class C extends JApplet { private JButton b1=new JButton("b1");

    private JButton b2=new JButton("b2");

    private JButton b3=new JButton("b3");

    private JButton b4=new JButton("b4");

    private JButton b5=new JButton("b5");

    private JPanel jp=new JPanel();

    ActionListener al=new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    JButton jb=(JButton)arg0.getSource();
    jp.remove(jb);
    jp.repaint();
    }

    };

    public void init(){
    Container c=getContentPane();
    b1.addActionListener(al);
    b2.addActionListener(al);
    b3.addActionListener(al);
    b4.addActionListener(al);
    b5.addActionListener(al);
    jp.add(b1);
    jp.add(b2);
    jp.add(b3);
    jp.add(b4);
    jp.add(b5);

    c.add(jp);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Console.run(new C(), 800, 600);
    }}
      

  2.   

    Console类就是运行这个applet用的:
    package framework;import javax.swing.JApplet;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class Console {
    public static String title(Object o){
    String t=o.getClass().toString();
    if(t.indexOf("class")!=-1){
    t=t.substring(6);
    }
    return t;
    }

    public static void run(JFrame frame,int width,int height){
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width,height);
    frame.setVisible(true);
    }

    public static void run(JApplet applet,int width,int height){
    JFrame frame=new JFrame(title(applet));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet);
    frame.setSize(width, height);
    applet.init();
    applet.start();
    frame.setVisible(true);
    }

    public static void run(JPanel panel,int width,int height){
    JFrame frame=new JFrame(title(panel));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel);
    frame.setSize(width,height);
    frame.setVisible(true);
    }
    }