//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); } } }
Comments
Leave a comment