首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

一个ActionEvent的小程序,该怎么处理

2012-01-03 
一个ActionEvent的小程序我想传一下参importjava.awt.*publicclassButtonEventextendsFrame{publicButton

一个ActionEvent的小程序
我想传一下参
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( "-------您点击的是取消按钮-------- ");
}

    }
}
关键在“*******”的两行   怎么写才对?



[解决办法]
String command = e.getActionCommand();
//System.out.println(command);
if( "1 ".equals(command))// **************************
{
System.out.println( "-------您点击的是确定按钮-------- ");
}
if( "2 ".equals(command))// ****************************


{
System.out.println( "-------您点击的是取消按钮-------- ");
}
[解决办法]
我的eclipse不支持中文,改成英文了。


// ButtonEvent.java

import 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.java

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.getSource() == frame.butt1)// **************************
{
System.out.println( "-------Ok button has been clicked-------- ");
}
if (e.getSource() == frame.butt2)// ****************************
{
System.out.println( "-------Cancel button has been clicked-------- ");
}

}
}



[解决办法]
把 ButtonClickEvent 作为ButtonEvent的内部类实现就行了

热点排行