Graphic User Interface

<< Off Topic Fun | OtherProjectsTrailIndex | Field >>

Getting text and button clicking actions from the User


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Class StaffDirectory - Applet for browsing and searching 
 * employee Information
 * 
 * @author Chris Thiel, OFMCap
 * @version 28 March 2009
 */
public class BusyBox extends JApplet implements ActionListener
{
    // the stuff that communicates with the user


     Button addButton;
     Button clearButton;
     Button capButton;
     Button lcButton;
     TextArea txtArea;  
     TextField txtFld;

     String message;

    public void init()
    {


        txtArea=new TextArea("Added the following:",25,70);
        txtArea.setEditable(false);
        add(txtArea);

        setLayout(new FlowLayout());
        clearButton = new Button("Clear");
        clearButton.addActionListener(this);
        capButton = new Button("Capitalize");
        capButton.addActionListener(this);
        lcButton = new Button("Lower Case");
        lcButton.addActionListener(this);
        add(clearButton);
        add(capButton);
        add(lcButton);
        setSize(700,500);

        txtFld = new TextField(30);
        add(txtFld);
        addButton = new Button("Add To Message");
        addButton.addActionListener(this);

        add(addButton);
        message="No Text Heref";

        }

    }


    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {
        // simple text displayed on applet
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        g.drawString(message, 20, 20);
        g.setColor(Color.blue);
        g.drawString("type text and press buttons", 500, 20);
    }

    /**
     * Here is the implementaqtion of the ActionPerformed interface
     * 
     * This interface requred us to define a specific method called
     * actionPerformed, which will handle any ActionEvent which
     * is generated when our user pressed a button.
     */
    public void actionPerformed (ActionEvent ev){
        Object source = ev.getSource();
        if (source == addButton){
            textArea.append( txtField.getText() );
            txtField.setText("");
        }
        if (source == clearButton){
            txtField.setText("");
            textArea.setText("");

        }
        if (source == capButton){

        }
        if (source == lcButton){

        }
        repaint();
    }

}