Advertisement
Guest User

TwoButtons

a guest
May 25th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class TwoButtons extends JFrame implements ActionListener
  6. {
  7. JButton redButton ;
  8. JButton grnButton ;
  9. JButton bluButton ;
  10. JButton gryButton ;
  11.  
  12. public TwoButtons(String title)
  13. {
  14. super( title );
  15.  
  16. redButton = new JButton("Red");
  17. grnButton = new JButton("Green");
  18. bluButton = new JButton("Blue");
  19. gryButton = new JButton("Gray");
  20.  
  21. redButton.setActionCommand( "red" );
  22. grnButton.setActionCommand( "green" );
  23. bluButton.setActionCommand( "blue" );
  24. gryButton.setActionCommand( "gray" );
  25.  
  26. redButton.addActionListener( this );
  27. grnButton.addActionListener( this );
  28. bluButton.addActionListener( this );
  29. gryButton.addActionListener( this );
  30.  
  31. setLayout( new FlowLayout() );
  32. add( redButton );
  33. add( grnButton );
  34. add( bluButton );
  35. add( gryButton );
  36.  
  37. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  38. }
  39.  
  40. public void actionPerformed( ActionEvent evt)
  41. {
  42. if ( evt.getActionCommand().equals( "red" ) )
  43. getContentPane().setBackground( Color.red );
  44. else if ( evt.getActionCommand().equals( "green" ) )
  45. getContentPane().setBackground( Color.green );
  46. else if ( evt.getActionCommand().equals( "blue" ) )
  47. getContentPane().setBackground( Color.blue );
  48. else
  49. getContentPane().setBackground( Color.gray );
  50.  
  51. repaint();
  52. }
  53.  
  54. public static void main ( String[] args )
  55. {
  56. TwoButtons demo = new TwoButtons( "Click a Button" ) ;
  57.  
  58. demo.setSize( 200, 150 );
  59. demo.setVisible( true );
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement