Write a thread to change the color of a form

Source: Internet
Author: User

Write a thread to change the color of the form, 1, use runnable to create a thread that implements the color of the form to switch between black and white continuously. Using an inner class to create threads, the color of the window is constantly switched between black and white.

First, create class testrunnable, inherit from JFrame, and implement Runnable interface.

Second, covering the Runable interface of the Run method, the implementation of the form color switch, in the Testrunnable class, covering the Runable class, covering the Runnable interface of the Run method. In this method, you first create the object panel for the JPanel class and place it on the form, and then use the while (true) loop to toggle the panel's color in the loop to achieve the color change of the form.

Show form, start thread, in the main method of the TestRun class, first set the form display, and then start the thread,

Create threads using an inner class, create threads using an inner class, and implement a window's color shift between black and white, as shown in the code below:

Import Java.awt.Color;

Import Javax.swing.JFrame;
Import Javax.swing.JPanel;

Test runnable
public class Testrunnable extends JFrame implements Runnable {
public static void Main (string[] args) {
testrunnable r = new Testrunnable ();
R.setsize (300,300);
R.setvisible (TRUE);
R.setdefaultcloseoperation (Jframe.exit_on_close);
Thread t = new Thread (r);
T.start ();
}
@Override
public void Run () {
int i = 0;
JPanel panel = new JPanel ();
Panel.setsize (300,300);
This.setcontentpane (panel);
while (true) {
i = i==0?1:0;
if (i==0) {
Panel.setbackground (Color.Black);
}else{
Panel.setbackground (Color.White);
}
}
}
}

Write a thread to change the color of a form

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.