Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class CaseConverter extends JApplet
- implements ActionListener
- {
- private JTextField inputField;
- private final String UPPERCASE = "UPPERCASE";
- private final String LOWERCASE = "lowercase";
- private final String CLEAR = "Clear";
- /*
- * Called by the browser or applet viewer to inform this JApplet that it
- * has been loaded into the system. It is always called before the first
- * time that the start method is called.
- */
- public void init()
- {
- // GUI elements are added to the applet's content pane, so get it for us.
- Container contentPane = getContentPane();
- // set a layout with some spacing
- contentPane.setLayout(new BorderLayout(12,12));
- // add the title label
- JLabel title = new JLabel("Case Converter - A demo applet");
- contentPane.add(title, BorderLayout.NORTH);
- // create the center part with prompt and text field and add it
- JPanel centerPanel = new JPanel();
- JLabel prompt = new JLabel("Enter a string:");
- centerPanel.add(prompt);
- inputField = new JTextField(16);
- centerPanel.add(inputField);
- contentPane.add(centerPanel, BorderLayout.CENTER);
- // make a panel for the buttons
- JPanel buttonPanel = new JPanel();
- // add the buttons to the button panel
- JButton uppercase = new JButton(UPPERCASE);
- uppercase.addActionListener(this);
- buttonPanel.add(uppercase);
- JButton lowercase = new JButton(LOWERCASE);
- lowercase.addActionListener(this);
- buttonPanel.add(lowercase);
- JButton clear = new JButton(CLEAR);
- clear.addActionListener(this);
- buttonPanel.add(clear);
- // add the buttons panel to the content pane
- contentPane.add(buttonPanel, BorderLayout.SOUTH);
- }
- /*
- * ActionListener Interface method.
- * Called when action events occur with registered components that
- * can fire action events.
- */
- public void actionPerformed(ActionEvent evt)
- {
- String command = evt.getActionCommand();
- // if clear button pressed
- if(CLEAR.equals(command))
- inputField.setText("");
- // uppercase button pressed
- else if(UPPERCASE.equals(command))
- inputField.setText(inputField.getText().toUpperCase());
- // lowercase button pressed
- else if(LOWERCASE.equals(command))
- inputField.setText(inputField.getText().toLowerCase());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment