Use of Setactioncommand () and Getactioncommand ()
/** * Function: event handling mechanism * */package Com.test3;import java.awt.borderlayout;import java.awt.color;import java.awt.Graphics; Import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JPanel; @SuppressWarnings ("Serial") public class Demo9_3 extends Jframe{mypanel P1 = new Mypanel (); JButton button1 = new JButton ("Black"); JButton button2 = new JButton ("Red");p ublic Demo9_3 () {Add (Button1,borderlayout.north); Add (Button2,borderlayout.south ), add (p1);p 1.setBackground (Color.Black), Button1.addactionlistener (new ActionListener () {//anonymous inner class @overridepublic void actionperformed (ActionEvent e) {p1.setbackground (Color.Black); repaint ();}); Button2.addactionlistener (new ActionListener () {//anonymous inner class @overridepublic void actionperformed (ActionEvent e) {if ( E.getactioncommand (). Equals ("Red")) {//51 The string in the line Setactioncommand method if it equals the string in the row equal () handle It P1.setbackground ( color.red); System.out.println ("Getactioncommand method is uesd successful");}Repaint ();}}); Button2.setactioncommand ("Red"); SetSize (+); setdefaultcloseoperation (jframe.exit_on_close); setvisible ( true);} public static void Main (string[] args) {new Demo9_3 ();} Class Mypanel extends Jpanel{public void paintcomponent (Graphics g) {super.paintcomponent (g);}}
"Fundamentals of Java language Programming"--event-driven programming--setactioncommand ()