Advertisement
Guest User

Untitled

a guest
Jul 8th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. /*ColorChangeTest.java*/
  2. import javax.swing.JFrame;
  3. import java.awt.Dimension;
  4. public class ColorChangeTest{
  5. public static void main( String[] args ){
  6. ColorChange colorChange = new ColorChange();
  7. colorChange.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  8. colorChange.setVisible( true );
  9. Dimension minimumSize = new Dimension( 400, 200 );
  10. colorChange.setMinimumSize( minimumSize );
  11. colorChange.pack();
  12. }//end of main
  13. }//end of class
  14.  
  15.  
  16. /*ColorChange.java*/
  17. import java.awt.FlowLayout;
  18. import java.awt.Color;
  19. import javax.swing.JFrame;
  20. import java.awt.event.ItemListener;
  21. import java.awt.event.ItemEvent;
  22. import javax.swing.JRadioButton;
  23. import javax.swing.ButtonGroup;
  24.  
  25. public class ColorChange extends JFrame{
  26. //declare button variables
  27. private JRadioButton redButton;
  28. private JRadioButton blueButton;
  29. private JRadioButton greenButton;
  30. private JRadioButton magentaButton;
  31. private ButtonGroup radioGroup;
  32. //use example of font
  33. public ColorChange(){
  34. super("colours test");
  35. setLayout( new FlowLayout());//set layout
  36. //radio buttons
  37. redButton = new JRadioButton( "Red", true );
  38. blueButton = new JRadioButton( "Blue", false );
  39. greenButton = new JRadioButton( "Green", false );
  40. magentaButton = new JRadioButton( "Magenta", false );
  41. //add to JFrame
  42. add( redButton );
  43. add( blueButton );
  44. add( greenButton );
  45. add( magentaButton );
  46. //add to the buttongroup
  47. radioGroup = new ButtonGroup();
  48. radioGroup.add( redButton );
  49. radioGroup.add( blueButton );
  50. radioGroup.add( greenButton );
  51. radioGroup.add( magentaButton );
  52. //register events
  53. redButton.addItemListener(new RadioButtonHandler(Color.RED));
  54. blueButton.addItemListener(new RadioButtonHandler(Color.BLUE));
  55. greenButton.addItemListener(new RadioButtonHandler(Color.GREEN));
  56. magentaButton.addItemListener(new RadioButtonHandler(Color.MAGENTA));
  57. //set the initial red colour
  58. getContentPane().setBackground( Color.RED );
  59. }//end of constructor
  60.  
  61. //handling events
  62. private class RadioButtonHandler implements ItemListener{
  63. private Color colour;
  64.  
  65. public RadioButtonHandler( Color targetColour){//constructor taking a color object
  66. colour = targetColour;
  67. }//end of constructor
  68.  
  69. public void itemStateChanged( ItemEvent event ){
  70. getContentPane().setBackground( colour );
  71. }//end of method
  72.  
  73. }//end of inner class
  74.  
  75. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement