[Java] ActionEvent 事件处理
2011-04-13 13:48
369 查看
import java.awt.*;
import java.awt.event.*;
public class ActionEventTest{
public static void main(String[] args){
//创建frame and Button
Frame f = new Frame("ActionEventTest");
Button bStart = new Button("Start");
Button bStop = new Button("Stop");
//给button组件加班ActionListener,
bStart.addActionListener(new ButtonActionEvent());
//设置bStop按钮的ActionCommand为 "Game Over"
bStop.setActionCommand("Game Over!");
bStop.addActionListener(new ButtonActionEvent());
//设置frame的布局并添加button
f.setLayout(new BorderLayout());
f.add(bStart,BorderLayout.NORTH);
f.add(bStop,BorderLayout.SOUTH);
//设置frame属性
f.setLocationByPlatform(true);
f.pack();
f.setVisible(true);
}
}
//创建 ActionListener 类,用接口的方法
class ButtonActionEvent implements ActionListener{
//重写 actionPerformed()方法
public void actionPerformed(ActionEvent e){
//输出ActionCommand的内容
System.out.println(e.getActionCommand());
}
}
//actionCommand属性用于记录按钮/菜单类组件所激发的ActionEvent事件相关指令信息
import java.awt.event.*;
public class ActionEventTest{
public static void main(String[] args){
//创建frame and Button
Frame f = new Frame("ActionEventTest");
Button bStart = new Button("Start");
Button bStop = new Button("Stop");
//给button组件加班ActionListener,
bStart.addActionListener(new ButtonActionEvent());
//设置bStop按钮的ActionCommand为 "Game Over"
bStop.setActionCommand("Game Over!");
bStop.addActionListener(new ButtonActionEvent());
//设置frame的布局并添加button
f.setLayout(new BorderLayout());
f.add(bStart,BorderLayout.NORTH);
f.add(bStop,BorderLayout.SOUTH);
//设置frame属性
f.setLocationByPlatform(true);
f.pack();
f.setVisible(true);
}
}
//创建 ActionListener 类,用接口的方法
class ButtonActionEvent implements ActionListener{
//重写 actionPerformed()方法
public void actionPerformed(ActionEvent e){
//输出ActionCommand的内容
System.out.println(e.getActionCommand());
}
}
//actionCommand属性用于记录按钮/菜单类组件所激发的ActionEvent事件相关指令信息
相关文章推荐
- [Java] ActionEvent 事件处理
- Java基础之处理事件——使用动作Action(Sketcher 6 using Action objects)
- JAVA事件处理中e.getSource()和e.getActionCommand()方法的区别
- JAVA--第12周实验--任务1--编写一个Panel的子类MyPanel,要求MyPanel的实例作为其按钮的ActionEvent事件的监视器
- JAVA--第十二周任务之1.编写一个应用程序,要求编写一个Panel的子类MyPanel,MyPanel中有一个文本框和一个按钮,要求MyPanel的实例作为其按钮的ActionEvent事件的监视
- 事件处理,ActionLister、ActionEvent(求一个数的平方)
- 【Java学习笔记】50:DocumentEvent事件的处理
- java java事件处理机制 event
- 第五十节 java学习——动作事件(ActionEvent)
- Java基础之处理事件——使窗口处理自己的事件(Skethcer 1 handing its own closing event)
- 使用handleEvent()方法,action()方法和mouseDown()方法处理事件的JAVA Application程序.
- java十二周实验.编写一个应用程序,要求编写一个Panel的子类MyPanel,MyPanel中有一个文本框和一个按钮,要求MyPanel的实例作为其按钮的ActionEvent事件的监视器,当单击
- 【Java学习笔记】49:ItemEvent事件的处理
- 多点触控事件处理 ev.getAction() & MotionEvent.ACTION_MASK
- --java-- 十二周 【任务一】编写一个Panel的子类MyPanel,MyPanel中有一个文本框和一个按钮,要求MyPanel的实例作为其按钮的ActionEvent事件的监视器
- 第2周:Java事件处理
- 第2周:Java事件处理
- commonJS — 自定义事件处理(for CustomEvent)
- Android PopWindow ACTION_OUTSIDE 事件处理
- java的事件处理