import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo extends JFrame implements ActionListener{ /**
 * @param args
 */
public FrameDemo(){
 JButton button1=  new JButton();
 JButton button2 = new JButton();
 JButton button3 = new JButton();
 JButton button4 = new JButton();
 JButton button5 = new JButton();
 JButton button6 = new JButton();

Container container = getContentPane();

container.setLayout(new FlowLayout());

JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(button3);
getContentPane().add(button4);
getContentPane().add(button5);
getContentPane().add(button6);

JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());


container.add(p1);
container.add(p2);

button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
}


public void actionPerformed(ActionEvent e)
{

if(e.getSource()== button1)
System.out.println("Button1 is clicked");

if(e.getSource()== button2)
System.out.println("Button1 is clicked");

if(e.getSource()== button3)
System.out.println("Button1 is clicked");

if(e.getSource()== button4)
System.out.println("Button1 is clicked");

if(e.getSource()== button5)
System.out.println("Button1 is clicked");

if(e.getSource()== button6)
System.out.println("Button1 is clicked");


}

public static void main(String[] args) {
// TODO Auto-generated method stub
FrameDemo frame = new FrameDemo();

frame.setSize(300,400);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}}
为什么报错,说
public void actionPerformed(ActionEvent e)
{

if(e.getSource()== button1)
System.out.println("Button1 is clicked");

if(e.getSource()== button2)
System.out.println("Button1 is clicked");

if(e.getSource()== button3)
System.out.println("Button1 is clicked");

if(e.getSource()== button4)
System.out.println("Button1 is clicked");

if(e.getSource()== button5)
System.out.println("Button1 is clicked");

if(e.getSource()== button6)
System.out.println("Button1 is clicked");


}

中的buttoni(i=1,2.....)不能解释

解决方案 »

  1.   

    作用域的问题,你这些button都在构造函数里声明定义的,在actionPerformed方法里当然是不可见的
    把这些作为类的私有变量就可以了
      

  2.   

    完整的程序如下:(我运行过了,应该是你要的效果把)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class FrameDemo extends JFrame implements ActionListener{ /**
     * @param args
     */
                    static JButton button1=  new JButton("button1");
    static JButton button2 = new JButton("button2");
    static JButton button3 = new JButton("button3");
    static JButton button4 = new JButton("button4");
    static JButton button5 = new JButton("button5");
    static JButton button6 = new JButton("button6");
    public FrameDemo(){
     

    Container container = getContentPane();

    container.setLayout(new FlowLayout());

    JPanel p1 = new JPanel();
    p1.setLayout(new FlowLayout());
    getContentPane().add(button1);
    getContentPane().add(button2);
    getContentPane().add(button3);
    getContentPane().add(button4);
    getContentPane().add(button5);
    getContentPane().add(button6);

    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout());


    container.add(p1);
    container.add(p2);

    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    }


    public void actionPerformed(ActionEvent e)
    {

    if(e.getSource()== button1)
    System.out.println("Button1 is clicked");

    if(e.getSource()== button2)
    System.out.println("Button2 is clicked");

    if(e.getSource()== button3)
    System.out.println("Button3 is clicked");

    if(e.getSource()== button4)
    System.out.println("Button4 is clicked");

    if(e.getSource()== button5)
    System.out.println("Button5 is clicked");

    if(e.getSource()== button6)
    System.out.println("Button6 is clicked");


    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    FrameDemo frame = new FrameDemo();

    frame.setSize(300,400);
    frame.setVisible(true);
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }}
      

  3.   

    楼上的把域定义为static其实不是一件好事吧?
      

  4.   

    晕,自己改了改,编译过去了,是个GUI布局的问题~~~~~~~~~对了,谁能给我详细介绍一下作用域的作用??我一直搞不太懂