Guest User

Untitled

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