/Users/lyon/j4p/src/gui/run/RunColorToggleButton.java

1    package gui.run; 
2     
3    import gui.ClosableJFrame; 
4     
5    import java.awt.*; 
6    import java.awt.event.ActionEvent; 
7     
8    /** 
9     * DocJava, Inc. http://www.docjava.com 
10    * Programmer: dlyon Date: Mar 3, 2004 Time: 
11    * 7:52:32 PM 
12    */ 
13   public abstract class RunColorToggleButton 
14           extends RunButton { 
15       private boolean selected = false; 
16    
17       public static void main(String[] args) { 
18           ClosableJFrame cf = new ClosableJFrame(); 
19           Container c = cf.getContentPane(); 
20           c.setLayout(new FlowLayout()); 
21           c.add(new RunColorToggleButton("[ok") { 
22                       public void run() { 
23                           System.out.println( 
24                                   getText()+ 
25                                   "isSlected=" + isSelected()); 
26                       } 
27                   }); 
28    
29           c.add(new RunColorToggleButton("[cancel") { 
30               public void run() { 
31                             System.out.println( 
32                                   getText()+ 
33                                   "isSlected=" + isSelected()); 
34               } 
35           }); 
36           cf.setSize(200, 200); 
37           cf.show(); 
38       } 
39       public void setSelected(boolean b){ 
40           if (b == selected) return; 
41           selected = b; 
42           toggleColor(); 
43       } 
44    
45       public void actionPerformed(ActionEvent e) { 
46           selected = !selected; 
47           toggleColor(); 
48           super.actionPerformed(e); 
49       } 
50    
51       private void toggleColor() { 
52           Color foreground = getForeground(); 
53           setForeground(getBackground()); 
54           setBackground(foreground); 
55       } 
56    
57       public boolean isSelected() { 
58           return selected; 
59       } 
60    
61       public RunColorToggleButton(String s) { 
62           super(s); 
63           setBackground(Color.red); 
64           setForeground(Color.green); 
65       } 
66    
67    
68   } 
69