home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 November / CMCD1104.ISO / Software / Freeware / Programare / bluej / bluejsetup-200.exe / {app} / examples / appletdemo / CaseConverter.java < prev    next >
Encoding:
Java Source  |  2004-09-15  |  3.5 KB  |  105 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. /**
  6.  * Class CaseConverter - A simple applet that takes input from a text field 
  7.  * and converts to upper or lower case in response to user button selection. 
  8.  * Works well with a width of 300 and height of 120.
  9.  *  
  10.  * Aug 2004: Updated from Applet to JApplet (mik)
  11.  *
  12.  * @author Bruce Quig 
  13.  * @author Michael Kolling
  14.  * 
  15.  * @version 2004-08-04
  16.  */
  17. public class CaseConverter extends JApplet
  18.      implements ActionListener
  19. {
  20.     private JTextField inputField;
  21.     private final String UPPERCASE = "UPPERCASE";
  22.     private final String LOWERCASE = "lowercase";
  23.     private final String CLEAR = "Clear";
  24.  
  25.      /**
  26.      * Called by the browser or applet viewer to inform this JApplet that it
  27.      * has been loaded into the system. It is always called before the first 
  28.      * time that the start method is called.
  29.      */
  30.     public void init()
  31.     {
  32.         // GUI elements are added to the applet's content pane, so get it for us.
  33.         Container contentPane = getContentPane();
  34.         
  35.         // set a layout with some spacing
  36.         contentPane.setLayout(new BorderLayout(12,12));
  37.         
  38.         // add the title label
  39.         JLabel title = new JLabel("Case Converter - A BlueJ demo applet");
  40.         contentPane.add(title, BorderLayout.NORTH);
  41.        
  42.         // create the center part with prompt and text field and add it
  43.         JPanel centerPanel = new JPanel();
  44.         JLabel prompt = new JLabel("Enter a string:");
  45.         centerPanel.add(prompt);
  46.         inputField = new JTextField(16);
  47.         centerPanel.add(inputField);
  48.         
  49.         contentPane.add(centerPanel, BorderLayout.CENTER);
  50.  
  51.         // make a panel for the buttons
  52.         JPanel buttonPanel = new JPanel();
  53.         
  54.         // add the buttons to the button panel
  55.         JButton uppercase = new JButton(UPPERCASE);
  56.         uppercase.addActionListener(this);
  57.         buttonPanel.add(uppercase);
  58.         
  59.         JButton lowercase = new JButton(LOWERCASE);
  60.         lowercase.addActionListener(this); 
  61.         buttonPanel.add(lowercase);
  62.         
  63.         JButton clear = new JButton(CLEAR);
  64.         clear.addActionListener(this);
  65.         buttonPanel.add(clear);
  66.         
  67.         // add the buttons panel to the content pane
  68.         contentPane.add(buttonPanel, BorderLayout.SOUTH);
  69.     }
  70.  
  71.     /**
  72.      * Returns information about this applet. 
  73.      * An applet should override this method to return a String containing 
  74.      * information about the author, version, and copyright of the JApplet.
  75.      *
  76.      * @return a String representation of information about this JApplet
  77.      */
  78.     public String getAppletInfo()
  79.     {
  80.         return "Title: Case Converter  \n" +
  81.                "Author: Bruce Quig  \n" +
  82.                "A simple applet that converts text to upper or lower case. ";
  83.     }
  84.  
  85.     /**
  86.      * ActionListener Interface method.
  87.      * Called when action events occur with registered components that
  88.      * can fire action events.
  89.      * @param  ae   the ActionEvent object created by the event
  90.      */
  91.     public void actionPerformed(ActionEvent evt)
  92.     {
  93.         String command = evt.getActionCommand();
  94.         // if clear button pressed
  95.         if(CLEAR.equals(command))
  96.             inputField.setText("");
  97.         // uppercase button pressed
  98.         else if(UPPERCASE.equals(command))
  99.             inputField.setText(inputField.getText().toUpperCase());
  100.         // lowercase button pressed
  101.         else if(LOWERCASE.equals(command))
  102.             inputField.setText(inputField.getText().toLowerCase());
  103.     }
  104. }
  105.