import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame implements ActionListener{
    JButton jb;
    FlowLayoutDemo(){
    super("FlowLayoutDemo布局");
   // Container c=f.getContentPane();
    FlowLayout flow=new FlowLayout(FlowLayout.CENTER,10,10);
    getContentPane().setLayout(flow);
    for(int i=0;i<=9;i++){
    jb=new JButton(""+i);
    jb.addActionListener(this);
    add(jb);
    }
    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==jb){System.exit(0);}
    }
    public static void main(String args[]){
    JFrame f=new FlowLayoutDemo();
    f.setSize(300,200);
    f.setVisible(true);
    }
}
请问各位大侠:为什么只有最后一个按钮响应事件?怎么能让所有按钮都响应此事件呢?请帮忙指正,谢谢!

解决方案 »

  1.   

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;public class FlowLayoutDemo extends JFrame implements ActionListener{
    JButton[] jb = new JButton[10]; FlowLayoutDemo(){
    super("FlowLayoutDemo布局");
    // Container c=f.getContentPane();
    FlowLayout flow = new FlowLayout(FlowLayout.CENTER,10,10);
    getContentPane().setLayout(flow);
    for(int i = 0;i <= 9;i++){
    jb[i] = new JButton("" + i);
    jb[i].addActionListener(this);
    add(jb[i]);
    }
    } public void actionPerformed(ActionEvent e){
    if(e.getSource() == jb[0]){
    System.out.println("jb0");
    }
    if(e.getSource() == jb[9]){
    System.exit(0);
    }
    } public static void main(String args[]){
    JFrame f = new FlowLayoutDemo();
    f.setSize(300,200);
    f.setVisible(true);
    }
    }
      

  2.   

    你这样肯定不行,建议 改成 jb1,jb2,jb3,然后一个一个加listner然后 public void actionPerformed(ActionEvent e){
        if(e.getSource()==jb){System.exit(0);}
        } 
    一个一个的判断 ,呵呵
      

  3.   

    因为jb指向的是JButton("9");import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class FlowLayoutDemo extends JFrame implements ActionListener{
    JButton jb;
    FlowLayoutDemo(){
    super("FlowLayoutDemo布局");
    // Container c=f.getContentPane();
    FlowLayout flow = new FlowLayout(FlowLayout.CENTER,10,10);
    getContentPane().setLayout(flow);
    for(int i = 0;i <= 9;i++){
    jb = new JButton("" + i);
    jb.addActionListener(this);
    add(jb);
    }
    }
    public void actionPerformed(ActionEvent e){
    JButton jb = (JButton)e.getSource();
    if(jb.getActionCommand().equals("9")){
    System.exit(0);
    }
    if(jb.getActionCommand().equals("1")){
    System.out.println("1");
    }
    }
    public static void main(String args[]){
    JFrame f = new FlowLayoutDemo();
    f.setSize(300,200);
    f.setVisible(true);
    }
    }
      

  4.   

    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    class FlowLayoutDemo extends JFrame implements ActionListener{ 
        JButton jb[]=new JButton[10]; 
        FlowLayoutDemo(){ 
        super("FlowLayoutDemo布局"); 
      // Container c=f.getContentPane(); 
        FlowLayout flow=new FlowLayout(FlowLayout.CENTER,10,10); 
        getContentPane().setLayout(flow); 
        for(int i=0;i <=9;i++){ 
        jb[i]=new JButton(""+i); 
        jb[i].addActionListener(this); 
        add(jb[i]); 
        } 
        } 
        public void actionPerformed(ActionEvent e){ 
         for (int i = 0; i < jb.length; i++) {
         if(e.getSource()==jb[i]){System.exit(0);} 
            } 
    }
        
        public static void main(String args[]){ 
        JFrame f=new FlowLayoutDemo(); 
        f.setSize(300,200); 
        f.setVisible(true); 
        } 
    }