code_junkie

Switching on a string/implementing button actions

Nov 11th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. class HelloComponent3 extends JComponent
  2. implements MouseMotionListener, ActionListener
  3. {
  4. int messageX = 75, messageY= 175;
  5.  
  6. String theMessage;
  7. String redString = "red", blueString = "blue", greenString = "green";
  8. String magentaString = "magenta", blackString = "black", resetString = "reset";
  9.  
  10. JButton resetButton;
  11. JRadioButton redButton, blueButton, greenButton, magentaButton, blackButton;
  12. ButtonGroup colorButtons;
  13.  
  14. public HelloComponent3(String message) {
  15.  
  16. theMessage = message;
  17.  
  18. //intialize the reset button
  19. resetButton = new JButton("Reset");
  20. resetButton.setActionCommand(resetString);
  21. resetButton.addActionListener(this);
  22.  
  23. //intialize our radio buttons with actions and labels
  24. redButton = new JRadioButton("Red");
  25. redButton.setActionCommand(redString);
  26. ...
  27.  
  28. redButton.addActionListener(this);
  29. blueButton.addActionListener(this);
  30. ...
  31.  
  32. public void actionPerformed(ActionEvent e){
  33.  
  34. if (e.getActionCommand().equals(resetString)) {
  35. messageX = 75; messageY = 175;
  36. setForeground(Color.black);
  37. blackButton.setSelected(true);
  38. repaint();
  39. return;
  40. }
  41.  
  42. if ( e.getActionCommand().equals(redString) ) {
  43. setForeground(Color.red);
  44. repaint();
  45. return;
  46. }
  47.  
  48. if ( e.getActionCommand().equals(blueString) ) {
  49. setForeground(Color.blue);
  50. repaint();
  51. return;
  52. }
  53.  
  54. if ( e.getActionCommand().equals(greenString) ) {
  55. setForeground(Color.green);
  56. repaint();
  57. return;
  58. }
  59.  
  60. if ( e.getActionCommand().equals(magentaString) ) {
  61. setForeground(Color.magenta);
  62. repaint();
  63. return;
  64. }
  65.  
  66. if ( e.getActionCommand().equals(blackString) ) {
  67. setForeground(Color.black);
  68. repaint();
  69. return;
  70. }
  71. }
  72.  
  73. resetButton.addActionListener(this);
  74.  
  75. resetButton.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent evt) {
  77. resetButtonActionPerformed(evt);
  78. }
  79. });
  80.  
  81. public void resetButtonActionPerformed(ActionEvent evt) {
  82. messageX = 75; messageY = 175;
  83. setForeground(Color.black);
  84. blackButton.setSelected(true);
  85. repaint();
  86. }
  87.  
  88. resetButton = new JButton(new ResetAction());
  89. redButton = new JButton(new ColorAction("Red", Color.red));
  90.  
  91. private class ResetAction extends AbstractAction {
  92. public ResetAction() {
  93. super("Reset");
  94. }
  95.  
  96. public void actionPerformed(ActionEvent e) {
  97. messageX = 75; messageY = 175;
  98. setForeground(Color.black);
  99. blackButton.setSelected(true);
  100. repaint();
  101. }
  102. }
  103.  
  104. private class ResetAction extends AbstractAction {
  105. private Color color;
  106.  
  107. public ColorAction(String title, Color color) {
  108. super(title);
  109. this.color = color;
  110. }
  111.  
  112. public void actionPerformed(ActionEvent e) {
  113. setForeground(color);
  114. repaint();
  115. }
  116. }
  117.  
  118. addColorButton("Green" , Color.GREEN );
  119. addColorButton("Red" , Color.RED );
  120. addColorButton("Yellow", Color.YELLOW);
  121. addColorButton("Blue" , Color.BLUE );
  122. ...
  123.  
  124. private void addColorButton(String label, Color color) {
  125. JRadioButton button = new JRadioButton(label);
  126. button.addActionListener(new ActionListener() {
  127. public void actionPerformed(ActionEvent event) {
  128. target.setForeground(color);
  129. target.repaint();
  130. }
  131. });
  132. colorGroup.add(button);
  133. panel.add(button);
  134. }
Add Comment
Please, Sign In to add comment