169 和 AWT 组件相关的动作- II
|
![BACKWARD](../../image/backward.gif)
|
- 假设我们在一个特定的前文中举例的称为Red, Green, Blue 的Container中有一串按钮.
则一个较好的方法应该是
- public boolean action(Event evt, Object arg) {
- if( evt.target instanceof Button)
- changeColor((String) arg); // changeColor Supplied by the user processes
color defined by text string used in defining buttons
- return true; // tell runtime that this event fully processed
- }
- void changeColor(String bname) { // A suitable user routine to be
called by above action
- if( bname.equals("Red")) setBackground(Color.red);
- else if (bname.equals("Green")) setBackground(Color.green);
- else if (bname.equals("Blue")) setBackground(Color.blue);
- else setBackground(Color.pink); // our favorite color
}
Copyright: NPACT |
![BACKWARD](../../image/backward.gif)
|