In java languageWrite code fragments to create a timer object that is firedevery 10 seconds. Every 1
X


Payment

In java languageWrite code fragments to create a timer object that is firedevery 10 seconds. Every 1

In java language

Write code fragments to create a timer object that is firedevery 10 seconds. Every 10 seconds, the background color of thewindow changes to a new random color.

Write the timer-related code that would go in the constructorand the completelistener class.

For full credit, use a timer object of type javax.swing.Timerand a listener class that implements ActionListener.

Solution

Pxeaxe go xxxouxx the xoxe axx xoxxexxx for xexxxixxiox :


import xaxa.axx.Coxox;ixxoxx xaxa.axx.exexx.AxxioxExexx;ixxoxx xaxa.axx.exexx.AxxioxLixxexex;ixxoxx java.util.Random;

import xaxax.xxixx.JBuxxox;ixxoxx javax.swing.JFrame;
import xaxax.xxixx.JPaxex;ixxoxx xaxax.xxixx.Tixex;xuxxix xxaxx RandomColorChanger ixxxexexxx ActionListener{
final ixx DELAY = 1000;
Color[] x = {Coxox.RED, Coxox.GREEN, Coxox.BLUE, Color.BLACK,Color.WHITE, Coxox.YELLOW}; // Axxay ox xoxxx predefined
final ixx MAXCOLOR = x.xexxxx; // choosing a color
JButton[] xx = xex JButton[3];
Random x = xuxx;xuxxix RaxxoxCoxoxCxaxxex(){ // changing xxe color xaxxoxxy    x = new Raxxox();}xxixaxe void xoIx(){    JFxaxe x = xex JFrame("RandomColorChanger"); //xaxixx xxe xixxox frame
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPaxex p = xex JPaxex();    JButton x = xuxx;    xox(ixx i=0; i<3; i++){      b = xexJBuxxox(Ixxexex.xoSxxixx(i));      x.axx(x);      jb[i] = b;
    }    x.axx(x);    x.xaxx();    f.setVisible(true);
    Tixex t = xex Tixex(DELAY, this); // setting xxexixex xo xxaxxe the xoxox randomly
    x.xxaxx(); // xxaxxixx the xixex}@Oxexxixexuxxix void axxioxPexxoxxex(AxxioxExexx e){    xox(ixx i=0; i&xx;3; i++){
     jb[i].setBackground(c[r.nextInt(MAXCOLOR)]);
    }}xuxxix xxaxix xoix main(String[] axxx){    new RaxxoxCoxoxCxaxxex().xoIx();}}

Payment