Write in Java and use sleep () to automatically move and change the window size.

Source: Internet
Author: User

I use two classes to write this small program. It is to make the code clearer and enhance its readability.

The autoframe class is just a window class used to display basic windows.

Autoframe code:

 1 import javax.swing.JFrame; 2  3 public class AutoFrame extends JFrame{ 4  5     /* Define the global variable */ 6     int WIDTH = 500, HEIGHT = 400; // define the initial size 7     int X = 0, Y = 0; // define the initial location 8      9     /* The constructor */10     public AutoFrame() {11         // TODO Auto-generated constructor stub12     }13     14     /* Display the window function */15     public void showFrame()16     {17         this.setSize(WIDTH, HEIGHT);18         this.setLocation(X, Y);19         this.setTitle("MY AUTO WINDOW");20         21         this.setVisible(true); // set the window is visible22         this.setDefaultCloseOperation(EXIT_ON_CLOSE); // set the window can close with close button23     }24 }

 

Windowaction is a real window handler class.

The windowaction class calls the automatic movement and automatic size change actions of the real window of the thread.

Windowaction code:

 1 public class WindowAction extends Thread { 2  3     static AutoFrame af = new AutoFrame(); // Create a object of AutoFrame 4     private int TIMES = 100; // define a limit times for the cycle 5  6     public void run() { 7         int times = TIMES; // define the cycle times is TIMES 8  9         int x = af.X, y = af.Y, w = af.WIDTH, h = af.HEIGHT; // Save the old values10         int i;11         for (i = 0; i <= times; ++i) { // cycle to change location of the window12             af.showFrame();13             af.setLocation(++af.X, ++af.Y); // reset location of the window14             System.out.println("times = " + i + ":");15             try {16                 sleep(100);17                 System.out.println("The frist thread sleep one second...\n");18             } catch (InterruptedException e) {19                 // TODO Auto-generated catch block20                 e.printStackTrace();21             }22         }23         24         for (i = 0; i <= times; ++i) { // cycle to change location of the window25             af.showFrame();26             af.setSize(--af.WIDTH, --af.HEIGHT); // reset size of the window27             System.out.println("times = " + i + ":");28             try {29                 sleep(100); // sleep 0.1 second30                 System.out.println("The second thread sleep one second...\n");31             } catch (InterruptedException e) {32                 // TODO Auto-generated catch block33                 e.printStackTrace();34             }35         }36     }37 38     public static void main(String[] args) {39         new WindowAction().start(); // call the function who in thread to start the thread40     }41 }

 

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.