Last active
February 20, 2019 16:50
-
-
Save theArjun/ea66244a1db477210bd4178522a05d17 to your computer and use it in GitHub Desktop.
Change Random Background Color with Swing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.*; | |
| import javax.swing.*; | |
| import java.awt.event.*; | |
| import java.util.Random; | |
| import javax.swing.event.*; | |
| public class ChangeBackGround extends JFrame { | |
| JButton button; | |
| Random rand; | |
| Color color; | |
| ChangeBackGround(String title) { | |
| super(title); | |
| rand = new Random(); | |
| setSize(400, 400); | |
| setLayout(new FlowLayout()); | |
| setVisible(true); | |
| getContentPane().setBackground(Color.RED); | |
| button = new JButton("Change Background Color"); | |
| add(button); | |
| button.addActionListener(new ActionListener() { | |
| @Override | |
| public void actionPerformed(ActionEvent ae) { | |
| color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); | |
| getContentPane().setBackground(color); | |
| System.out.println("Color Value : " + color); | |
| } | |
| }); | |
| } | |
| public static void main(String[] args) { | |
| new ChangeBackGround("Change Background With Swing"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment