Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. import javax.swing.event.*;
  7.  
  8. public class ChangeBackGround extends JFrame {
  9.  
  10. JButton button;
  11. Random rand;
  12. Color color;
  13.  
  14. ChangeBackGround(String title) {
  15. super(title);
  16.  
  17. rand = new Random();
  18.  
  19. setSize(400, 400);
  20. setLayout(new FlowLayout());
  21. setVisible(true);
  22.  
  23. getContentPane().setBackground(Color.RED);
  24.  
  25. button = new JButton("Change Background Color");
  26. add(button);
  27. button.addActionListener(new ActionListener() {
  28. @Override
  29. public void actionPerformed(ActionEvent ae) {
  30. color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
  31. getContentPane().setBackground(color);
  32. System.out.println("Color Value : " + color);
  33. }
  34. });
  35. }
  36.  
  37. public static void main(String[] args) {
  38. new ChangeBackGround("Change Background With Swing");
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement