"Fundamentals of Java language Programming"--event-driven programming--setactioncommand ()

Source: Internet
Author: User



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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.