在程序运行时,网格布局的JPanel的网格已经确定下来,要如何才能往里面再添加一个新的按钮呢?代码如下
public class AddButton extends JFrame implements ActionListener{ private JPanel jp;
private ArrayList<String> lst=null;
private JButton addBtn;
public AddButton(){
lst=new ArrayList<String>();
lst.add("按钮1");
lst.add("按钮2");
jp=new JPanel(new GridLayout(lst.size(),1));
for(int i=0;i<lst.size();i++){
JButton jb=new JButton(lst.get(i));
jp.add(jb);
}
addBtn=new JButton("添加一个按钮");
addBtn.setActionCommand("add");
addBtn.addActionListener(this);
this.add(jp);
this.add(addBtn,BorderLayout.SOUTH);
this.setSize(400, 300);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
AddButton ab=new AddButton();
} @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.equals("add")){
lst.add("按钮3");
JButton jb=new JButton(lst.get(2));
//如何添加到jp中去??
jp.add(jb);
this.repaint();
jp.validate();
                           //期望的效果是网格布局的jp能变成new JPanel(new GridLayout(3,1))的效果
}
}}
期望的效果是网格布局的jp能变成new JPanel(new GridLayout(3,1))的效果
这个该如何实现?
如果需要重新调用构造函数也可以,将list作为构造函数的参数传递进去 也行的

解决方案 »

  1.   

    public void actionPerformed(ActionEvent arg0) {
    JButton jb=new JButton("按钮3");
    jp.add(jb);
    jp.setLayout(new GridLayout(3,1));
    //jp.repaint();
    jp.validate();}
    上面的代码没问题下面的这行没起作用,条件不对
    // if(arg0.equals("add")){
      

  2.   

    怎么没人回复呀 楼上的说的条件的是我大意  写错了,应该是if(arg0.getActionCommand().equals("add"))
    但这个不是问题所在,你肯定是只随便看了看 运行一下就会知道根本没有效果 不能添加到BUTTON
      

  3.   

    期望的效果是网格布局的jp能变成new JPanel(new GridLayout(3,1))的效果
    请问要如何才能有这个效果啊?
      

  4.   

    我有一个比较蠢的方法,先把jp里面的button都删掉,然后调整jp的网格布局,最后把button加回来import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;public class AddButton extends JFrame implements ActionListener{ private JPanel jp;
    private ArrayList<String> lst=null;
    private JButton addBtn;

    public AddButton(){
    lst=new ArrayList<String>();
    lst.add("按钮1");
    lst.add("按钮2");
    jp=new JPanel(new GridLayout(lst.size(),1));
    for(int i=0;i<lst.size();i++){
    JButton jb=new JButton(lst.get(i));
    jp.add(jb);
    }
    addBtn=new JButton("添加一个按钮");
    addBtn.setActionCommand("add");
    addBtn.addActionListener(this);
    this.add(jp);
    this.add(addBtn,BorderLayout.SOUTH);
    this.setSize(400, 300);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setVisible(true);
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    AddButton ab=new AddButton();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if(arg0.getActionCommand().equals("add")){
    System.out.println("add");
    lst.add("按钮3");
    //JButton jb=new JButton(lst.get(2));
    //如何添加到jp中去??
    //jp.add(jb);
    jp.removeAll();
    jp.setLayout(new GridLayout(lst.size(), 1));
    for (int i = 0; i < lst.size(); i++)
    {
    jp.add(new JButton(lst.get(i)));
    }
    this.repaint();
    jp.validate();
      //期望的效果是网格布局的jp能变成new JPanel(new GridLayout(3,1))的效果
    }
    }}
      

  5.   


    public void actionPerformed(ActionEvent arg0) {
    if (arg0.getActionCommand().equals("add")){
    lst.add("按钮3");
    jp.setLayout(new GridLayout(lst.size(),1));//这样就行了啊,楼主想什么呢
    JButton jb = new JButton(lst.get(2));
    // 如何添加到jp中去??
    jp.add(jb);
    this.repaint();
    jp.validate();
    // 期望的效果是网格布局的jp能变成new JPanel(new GridLayout(3,1))的效果
    }
    }
      

  6.   


    你怎么肯定:我是只随便看了看 没运行?
    你自己去掉 if(arg0.getActionCommand().equals("add")) 试试