"Fundamentals of Java language Programming"--animation of event-driven programming--timer class

Source: Internet
Author: User

When using the Timer Class guide package, be careful not to guide the wrong package, there are several different timer classes

Package Chapter16;import Java.awt.graphics;import Java.awt.gridlayout;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jframe;import Javax.swing.jpanel;import javax.swing.Timer;@ Suppresswarnings ("Serial") public class Animationdemo extends JFrame {public Animationdemo () {//Create a Movingmessagepanel for displaying a moving messagethis.setlayout (new GridLayout (2, 1)); Add (New Movingmessagepanel (" Message moving), add (New Movingmessagepanel ("2410!", 500)); public static void Main (string[] args) {Animationdemo frame = new Animationdemo (); Frame.settitle ("Animationdemo"); Frame.setlocationrelativeto (NULL); Center the Frameframe.setdefaultcloseoperation (jframe.exit_on_close); frame.setsize (280); Frame.setvisible ( true);} Static Class Movingmessagepanel extends JPanel {private String message = "Welcome to Java";p rivate int xcoordinate = 0;pri vate int ycoordinate = 20;public Movingmessagepanel (String message, int delay) {this.message = message;//Create a timertimer timer = new Timer (delay, new TimerListener ()); Timer.start ();} public void Paintcomponent (Graphics g) {super.paintcomponent (g); if (Xcoordinate > GetWidth ()) {xcoordinate =-20;} Xcoordinate + = 5;g.drawstring (message, xcoordinate, ycoordinate);} Class TimerListener implements ActionListener {/** Handle actionevent */public void actionperformed (ActionEvent e) {Repai NT ();}}}}


Implementing messages moving through windows





"Fundamentals of Java language Programming"--animation of event-driven programming--timer class

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.