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