home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / desvs7nu / src / buttontest.java next >
Encoding:
Java Source  |  1996-08-14  |  3.1 KB  |  121 lines

  1.  
  2. /** 
  3.  * Simple applet to test JanneButton class
  4.  */
  5. import java.awt.*;
  6. import java.awt.image.*;
  7. import java.applet.Applet;
  8.  
  9. public class ButtonTest extends Applet {
  10.    Image joe;
  11.    JanneButton button3;
  12.    // used by ctrl panel...
  13.    Checkbox isEnabled;
  14.    Checkbox isVisible;
  15.    Checkbox imageUsed;
  16.    Checkbox labelUsed;
  17.    Checkbox resizeImage;
  18.    TextField labelText;
  19.    
  20.    public void init() {
  21.       // test setting of background
  22.       //setBackground(new Color(0xEED5B7));
  23.  
  24.       // Create a grid where we add the buttons
  25.       setLayout(new BorderLayout());
  26.       Panel grid = new Panel();
  27.       grid.setLayout(new GridLayout(0, 2));
  28.       add("Center", grid);
  29.       add("East", ctrlPanel());
  30.       
  31.       // get the image
  32.       joe = getImage(getDocumentBase(), "images/joe.gif");
  33.  
  34.       // create buttons (one standard awt to compare...)
  35.       // The 3'rd button is controlled by a ctrl panel
  36.       grid.add(new Button("button1"));
  37.       grid.add(new JanneButton(joe, "button2"));
  38.       grid.add(button3 = new JanneButton(joe, "button3", false, false));
  39.       grid.add(new JanneButton("button4"));
  40.  
  41.       show();
  42.     }
  43.  
  44.    public Panel ctrlPanel() {
  45.       Panel ctrlPanel = new Panel();
  46.       ctrlPanel.setLayout(new BorderLayout());
  47.  
  48.       Panel p1 = new Panel();
  49.       p1.setLayout(new BorderLayout());
  50.       p1.add("North", new Label(""));
  51.       p1.add("Center", new Label("Control Button 3:"));
  52.       p1.add("South", new Label(""));
  53.       ctrlPanel.add("North", p1);
  54.       
  55.       Panel p2 = new Panel();
  56.       p2.setLayout(new GridLayout(0, 2, 4, 4));
  57.  
  58.       p2.add(new Label("Enabled:"));
  59.       isEnabled = new Checkbox("", null, true);
  60.       p2.add(isEnabled);
  61.       
  62.       p2.add(new Label("Visible:"));
  63.       isVisible = new Checkbox("", null, true);
  64.       p2.add(isVisible);
  65.       
  66.       p2.add(new Label("Display Image:"));
  67.       imageUsed = new Checkbox("", null, true);
  68.       p2.add(imageUsed);
  69.  
  70.       p2.add(new Label("Display Label:"));
  71.       labelUsed = new Checkbox("", null, false);
  72.       p2.add(labelUsed);
  73.  
  74.       p2.add(new Label("Resize Image:"));
  75.       resizeImage = new Checkbox("", null, false);
  76.       p2.add(resizeImage);
  77.  
  78.       p2.add(new Label("String Label:"));
  79.       labelText = new TextField("button3", 8);
  80.       p2.add(labelText);
  81.  
  82.       ctrlPanel.add("Center", p2);
  83.       Panel p3 = new Panel();
  84.       p3.setLayout(new FlowLayout());
  85.       p3.add(new JanneButton("Update"));
  86.       ctrlPanel.add("South", p3);
  87.       
  88.       return ctrlPanel;
  89.    }
  90.    
  91.    
  92.    /**
  93.     * Called when we press the buttons
  94.     */
  95.    public boolean action(Event evt, Object arg) {
  96.       if (evt.target instanceof Checkbox) {
  97.       }
  98.       else if(evt.target instanceof JanneButton &&
  99.           ("Update".equals(arg))) {
  100.      // update button 3
  101.      if (isEnabled.getState()) 
  102.         button3.enable();
  103.      else
  104.         button3.disable();
  105.      button3.show(isVisible.getState());
  106.      if (imageUsed.getState())
  107.         button3.setImage(joe);
  108.      else
  109.         button3.setImage(null);
  110.      button3.setResizeImage(resizeImage.getState());
  111.      button3.setShowLabel(labelUsed.getState());
  112.      button3.setLabel(labelText.getText());
  113.       }
  114.       else
  115.      System.out.println("Button selected: " + (String)arg);
  116.       return true;
  117.    }
  118.  
  119. }
  120.  
  121.