home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch06 / Checkbox1.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  3.7 KB  |  191 lines

  1. import java.awt.*;
  2.  
  3.   import java.applet.*;
  4.  
  5.   public class Checkbox1 extends Applet
  6.  {
  7.  
  8.      void displayButton_Clicked(java.awt.event.[ccc]
  9. ActionEvent event)
  10.  {
  11.  
  12.         // clear the results area
  13.  
  14.         results.setText("");
  15.  
  16.        // display the gender
  17.  
  18.        Checkbox current = genderGroup.getSelectedCheckbox();
  19.  
  20.        results.append(current.getLabel() + "\r\n");
  21.  
  22.  
  23.        // check each of the sports
  24.  
  25.        if (artCheckbox.getState() == true)
  26.  
  27.          results.append("Art\r\n");
  28.  
  29.        if (psychologyCheckbox.getState() == true)
  30.  
  31.           results.append("Psychology\r\n");
  32.  
  33.        if (historyCheckbox.getState() == true)
  34.  
  35.           results.append("History\r\n");
  36.  
  37.        if (musicCheckbox.getState() == true)
  38.  
  39.           results.append("Music\r\n");
  40.  
  41.        if (scienceCheckbox.getState() == true)
  42.  
  43.           results.append("Science\r\n");
  44.  
  45.     }
  46.  
  47.  
  48.     public void init() {
  49.  
  50.        // Call parents init method.
  51.  
  52.        super.init();
  53.  
  54.  
  55.        // This code is automatically generated by Visual Cafe
  56.  
  57.        // when you add components to the visual environment.
  58.  
  59.        // It instantiates and initializes the components. To
  60.  
  61.        // modify the code, use only code syntax that matches
  62.  
  63.        // what Visual Cafe can generate, or Visual Cafe may
  64.  
  65.        // be unable to back parse your Java file into its 
  66.  
  67.        // visual environment.
  68.  
  69.        //{{INIT_CONTROLS
  70.  
  71.        setLayout(null); 
  72.  
  73.        resize(442,354);
  74.  
  75.        label1 = new java.awt.Label("Gender:",Label.RIGHT);
  76.  
  77.        label1.setBounds(72,24,84,28);
  78.  
  79.        add(label1);
  80.  
  81.        genderGroup = new CheckboxGroup();
  82.  
  83.        maleButton = new java.awt.Checkbox("Male",genderGroup,
  84.  
  85.              true);
  86.  
  87.        maleButton.setBounds(168,24,58,21);
  88.  
  89.        add(maleButton);
  90.  
  91.        femaleButton = new java.awt.Checkbox("Female",
  92.  
  93.              genderGroup, false); 
  94.  
  95.        femaleButton.setBounds(240,24,71,21);
  96.  
  97.        add(femaleButton);
  98.  
  99.        historyCheckbox = new java.awt.Checkbox("History");
  100.  
  101.        historyCheckbox.setBounds(36,96,60,21);
  102.  
  103.        add(historyCheckbox);
  104.  
  105.        psychologyCheckbox = new java.awt.Checkbox("Psychology");
  106.  
  107.        psychologyCheckbox.setBounds(101,96,85,21);
  108.  
  109.        add(psychologyCheckbox);
  110.  
  111.        artCheckbox = new java.awt.Checkbox("Art");
  112.  
  113.        artCheckbox.setBounds(190,96,40,21);
  114.  
  115.        add(artCheckbox);
  116.  
  117.        musicCheckbox = new java.awt.Checkbox("Music");
  118.  
  119.        musicCheckbox.setBounds(238,96,56,21);
  120.  
  121.        add(musicCheckbox);
  122.  
  123.        scienceCheckbox = new java.awt.Checkbox("Science");
  124.  
  125.        scienceCheckbox.setBounds(298,96,93,21);
  126.  
  127.        add(scienceCheckbox);
  128.  
  129.        displayButton = new java.awt.Button("Display");
  130.  
  131.        displayButton.setBounds(48,204,100,33);
  132.  
  133.        add(displayButton);
  134.  
  135.        results = new java.awt.TextArea();
  136.  
  137.        results.setBounds(180,144,246,198);
  138.  
  139.        add(results);
  140.  
  141.        Action lAction = new Action();
  142.  
  143.        displayButton.addActionListener(lAction); 
  144.  
  145.     }
  146.  
  147.  
  148.     java.awt.Label label1;
  149.  
  150.     java.awt.Checkbox maleButton;
  151.  
  152.     CheckboxGroup genderGroup;
  153.  
  154.     java.awt.Checkbox femaleButton;
  155.  
  156.     java.awt.Label label2;
  157.  
  158.     java.awt.Checkbox historyCheckbox;
  159.  
  160.     java.awt.Checkbox psychologyCheckbox;
  161.  
  162.     java.awt.Checkbox artCheckbox;
  163.  
  164.    java.awt.Checkbox musicCheckbox;
  165.  
  166.    java.awt.Checkbox scienceCheckbox; 
  167.  
  168.    java.awt.Button displayButton;
  169.  
  170.    java.awt.TextArea results;
  171.  
  172.  
  173.  
  174.    class Action implements java.awt.event.ActionListener {
  175.  
  176.       public void actionPerformed(java.awt.event.ActionEvent
  177.  
  178.             event) {
  179.  
  180.          Object object = event.getSource();
  181.  
  182.          if (object == displayButton)
  183.  
  184.             displayButton_Clicked(event);
  185.  
  186.       }
  187.  
  188.    }
  189.  
  190. }
  191.