60 745
Assignments Done
97,8%
Successfully Done
In April 2018

Answer to Question #33970 in Java | JSP | JSF for Tsaone Mokao

Question #33970
java code that would change background color of an interface after a specific interval
Expert's answer
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;

//main class
public class ChangeBackgroundColor extends JFrame {

//main function
public static void main(String[] args) {
new ChangeBackgroundColor("Change Background
Color").setVisible(true);
}
//Container
private Container mainContainer;
//Change Background Color
ChangeBackgroundColor(String title) {
super(title);
InitComponents();
}
//Init Components
private void InitComponents()
{
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//Close Window
this.setBounds(100, 100, 450, 450);//Set size of window
mainContainer = this.getContentPane();
mainContainer.setLayout(null);
mainContainer.setBackground(Color.RED);
//Create timer
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int r =new Random().nextInt(255);//set red color
int g =new Random().nextInt(255);//set green color
int b =new Random().nextInt(255);//set blue color
mainContainer.setBackground(new Color(r, g, b));//change color of interface
repaint();
}
});
timer.start();//start timer
}
//When Exit
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("exit"))
{
System.exit(0);
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions