Guest User

Untitled

a guest
May 20th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.*;
  7.  
  8. public class GUI {
  9.  
  10. private JFrame frame;
  11. private JButton button;
  12.  
  13. public static void main(String[] args){
  14. SwingUtilities.invokeLater(new Runnable(){
  15. public void run(){
  16. GUI gui = new GUI();
  17. gui.setup();
  18. }
  19.  
  20. });
  21. }
  22.  
  23. public GUI(){
  24. frame = new JFrame("SET COLOR");
  25. button = new JButton("CHANGE");
  26. button.addActionListener(new ColorChanged(frame));
  27. }
  28.  
  29. public void setup(){
  30. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31. frame.setVisible(true);
  32. frame.setSize(500,500);
  33. frame.setResizable(false);
  34. GridLayout layout = new GridLayout(7,1);
  35. frame.setLayout(layout);
  36. frame.add(button);
  37. frame.getContentPane().setBackground(Color.orange);
  38. }
  39. /*
  40. class colorChanged implements ActionListener{
  41. public void actionPerformed(ActionEvent evt){
  42. frame.getContentPane().setBackground(Color.cyan);
  43. }
  44. }
  45. */
  46. }
  47.  
  48.  
  49.  
  50. /***************** CLASSE ESTERNA **********************/
  51.  
  52. import java.awt.Color;
  53. import java.awt.event.ActionEvent;
  54. import java.awt.event.ActionListener;
  55.  
  56. import javax.swing.JFrame;
  57.  
  58.  
  59.  
  60. public class ColorChanged implements ActionListener {
  61.  
  62. private JFrame frame;
  63.  
  64. public ColorChanged(JFrame frame){
  65. this.frame = frame;
  66. }
  67.  
  68. public void actionPerformed(ActionEvent ext){
  69. if(frame.getContentPane().getBackground()== Color.orange)
  70. frame.getContentPane().setBackground(Color.cyan);
  71. else
  72. frame.getContentPane().setBackground(Color.orange);
  73. }
  74. }
Add Comment
Please, Sign In to add comment