Codex

CaseConverter

Jul 2nd, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class CaseConverter extends JApplet
  5.      implements ActionListener
  6. {
  7.     private JTextField inputField;
  8.     private final String UPPERCASE = "UPPERCASE";
  9.     private final String LOWERCASE = "lowercase";
  10.     private final String CLEAR = "Clear";
  11.  
  12.      /*
  13.      * Called by the browser or applet viewer to inform this JApplet that it
  14.      * has been loaded into the system. It is always called before the first
  15.      * time that the start method is called.
  16.      */
  17.     public void init()
  18.     {
  19.         // GUI elements are added to the applet's content pane, so get it for us.
  20.         Container contentPane = getContentPane();
  21.        
  22.         // set a layout with some spacing
  23.         contentPane.setLayout(new BorderLayout(12,12));
  24.        
  25.         // add the title label
  26.         JLabel title = new JLabel("Case Converter - A demo applet");
  27.         contentPane.add(title, BorderLayout.NORTH);
  28.        
  29.         // create the center part with prompt and text field and add it
  30.         JPanel centerPanel = new JPanel();
  31.         JLabel prompt = new JLabel("Enter a string:");
  32.         centerPanel.add(prompt);
  33.         inputField = new JTextField(16);
  34.         centerPanel.add(inputField);
  35.        
  36.         contentPane.add(centerPanel, BorderLayout.CENTER);
  37.  
  38.         // make a panel for the buttons
  39.         JPanel buttonPanel = new JPanel();
  40.        
  41.         // add the buttons to the button panel
  42.         JButton uppercase = new JButton(UPPERCASE);
  43.         uppercase.addActionListener(this);
  44.         buttonPanel.add(uppercase);
  45.        
  46.         JButton lowercase = new JButton(LOWERCASE);
  47.         lowercase.addActionListener(this);
  48.         buttonPanel.add(lowercase);
  49.        
  50.         JButton clear = new JButton(CLEAR);
  51.         clear.addActionListener(this);
  52.         buttonPanel.add(clear);
  53.        
  54.         // add the buttons panel to the content pane
  55.         contentPane.add(buttonPanel, BorderLayout.SOUTH);
  56.     }
  57.  
  58.    
  59.  
  60.     /*
  61.      * ActionListener Interface method.
  62.      * Called when action events occur with registered components that
  63.      * can fire action events.
  64.      */
  65.     public void actionPerformed(ActionEvent evt)
  66.     {
  67.         String command = evt.getActionCommand();
  68.         // if clear button pressed
  69.         if(CLEAR.equals(command))
  70.             inputField.setText("");
  71.         // uppercase button pressed
  72.         else if(UPPERCASE.equals(command))
  73.             inputField.setText(inputField.getText().toUpperCase());
  74.         // lowercase button pressed
  75.         else if(LOWERCASE.equals(command))
  76.             inputField.setText(inputField.getText().toLowerCase());
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment