我想传一下参
import java.awt.*;
public class ButtonEvent extends Frame{
  public ButtonEvent(String title) {
    super(title);
  }
  public static void main(String[] args) {
    //创建窗体对象 frame 并设置标题
    ButtonEvent frame = new ButtonEvent("This is My Button Event");
    //设置窗体的布局管理器为流式的
    frame.setLayout(new FlowLayout());
    //创建两个按钮对象 butt1 , butt2
    Button butt1 = new Button("确定");
    Button butt2 = new Button("取消");
    //分别为 butt1 和 butt2 添加监听器
    butt1.addActionListener(new ButtonClickEvent(frame));
    butt2.addActionListener(new ButtonClickEvent(frame));
    //向窗体添加按钮组件
    frame.add(butt1);
    frame.add(butt2);
    butt1.setActionCommand("1");
    butt2.setActionCommand("2");
    //设置窗体大小
    frame.setSize(200,300);
    //显示窗体
    frame.show();
  }
}//////////////////////////////////////
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonClickEvent implements ActionListener{
    ButtonEvent frame;
    public ButtonClickEvent(ButtonEvent frame1)
    {
     frame=frame1;
    }
public void actionPerformed(ActionEvent e) {
    //根据不同的事件源的名称触发相应的事件
  /*  if(e.getActionCommand().equals("确定"))
    {
      System.out.println("-------您点击的是确定按钮--------");
    }
    if(e.getActionCommand().equals("取消"))
    {
      System.out.println("-------您点击的是取消按钮--------");
    }*/
if(e.get.getSource()==butt1)//**************************
{
System.out.println("-------您点击的是确定按钮--------");
}
if(e.getSource()==butt2)//****************************
{
System.out.println("-------您点击的是取消按钮--------");
}  }
}
关键在“*******”的两行 怎么写才对?

解决方案 »

  1.   

    String command = e.getActionCommand();
              //System.out.println(command);
    if("1".equals(command))// **************************
    {
    System.out.println("-------您点击的是确定按钮--------");
    }
    if("2".equals(command))// ****************************
    {
    System.out.println("-------您点击的是取消按钮--------");
    }
      

  2.   

    那请问如何用getsource()方法?该如何改?
      

  3.   

    我的eclipse不支持中文,改成英文了。
    // ButtonEvent.javaimport java.awt.*;public class ButtonEvent extends Frame {
    Button butt1 = new Button("OK");
    Button butt2 = new Button("Cancel");
    public ButtonEvent(String title) {
    super(title);
    setLayout(new FlowLayout());
    butt1.addActionListener(new ButtonClickEvent(this));
    butt2.addActionListener(new ButtonClickEvent(this));
    add(butt1);
    add(butt2);
    butt1.setActionCommand("1");
    butt2.setActionCommand("2");
    setSize(200, 300);
    setVisible(true);
    } public static void main(String[] args) {
    new ButtonEvent("This is My Button Event");
    }}// ButtonClickEvent.javaimport java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;public class ButtonClickEvent implements ActionListener {
    ButtonEvent frame; public ButtonClickEvent(ButtonEvent frame1) {
    frame = frame1;
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == frame.butt1)// **************************
    {
    System.out.println("-------Ok button has been clicked--------");
    }
    if (e.getSource() == frame.butt2)// ****************************
    {
    System.out.println("-------Cancel button has been clicked--------");
    } }
    }
      

  4.   

    把 ButtonClickEvent 作为ButtonEvent的内部类实现就行了
      

  5.   

    感谢  
     Dan1980() 
    大虾的指点~~~ 还有楼上的大虾讲清楚点
    怎么给你们分啊