Home | Java Menu | Java Applets | Java Tips | E-mail Us
View Java Applet


/* ButtonLabel.java
  *
  * A Java Applet demonstrating the use of buttons, labels, checkboxes, and text areas.
  *
  * Authored by Khim Theng
  * khim_theng@completeesolutions.com
  *
  * Last modified on 05/11/2000
  */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ButtonLabel extends Applet implements ActionListener, ItemListener
{
    Button changeBtn, clearBtn;  //Change and clear buttons
    Label titleLabel, authorLabel, instructionLabel, resultLabel;  //Label variables
    TextArea originalTa, resultTa;  //TextArea variables
    Checkbox italicCb, boldCb;  //Italic and bold checkboxes

    public void init()
    {
        //Assign RGB colors to the foreground and background
        Color color = new Color(33, 115, 221);
        Color fColor = new Color(0, 0, 128);
        setBackground(color);
        setForeground(fColor);

        titleLabel = new Label("Font Change Program", Label.CENTER);
        titleLabel.setFont(new Font("Serif", Font.BOLD, 20));

        authorLabel = new Label("Copyright \u00A9 2000 by Complete e Solutions", Label.CENTER);
        authorLabel.setFont(new Font("Monospaced", Font.PLAIN, 11));

        instructionLabel = new Label("  Please enter your text message:", Label.LEFT);
        instructionLabel.setFont(new Font("Serif", Font.BOLD, 15));

        resultLabel = new Label("  Here is your new font version:", Label.LEFT);
        resultLabel.setFont(new Font("Serif", Font.BOLD, 15));
        
        //Create No Change and Clear All buttons
        changeBtn = new Button("No Change");
        clearBtn = new Button("Clear All");

        //Create Italic and Bold check boxes
        italicCb = new Checkbox("Italic");
        boldCb = new Checkbox("Bold");

        setLayout(new BorderLayout());

        //Create Panel top to hold the title, instruction, and editable text area
        Panel top = new Panel();
            top.setLayout(new BorderLayout());
            top.add(titleLabel, "North");
            top.add(instructionLabel, "Center");
            top.add(originalTa = new TextArea(3, 18), "South");
            originalTa.setBackground(Color.white);
            originalTa.setFont(new Font("Serif",Font.PLAIN, 15));

        add(top, "North");  //Add Panel top to the BorderLayout

        //Create Panel mid to hold buttons and checkboxes
        Panel mid = new Panel();
            mid.setLayout(new BorderLayout());
            Panel fontTypes = new Panel();
                fontTypes.setLayout(new FlowLayout());
                fontTypes.add(changeBtn);
                fontTypes.add(new Label(" ")); //Add spaces between button and checkboxes
                fontTypes.add(italicCb);
                fontTypes.add(boldCb);
            mid.add(fontTypes, "North");

            //Create Panel cBt to hold Clear All button
            Panel cBt = new Panel();
                cBt.add(clearBtn);
            mid.add(cBt, "South");

        add(mid, "Center");  //Add Panel mid to the BorderLayout

        //Create Panel bottom to hold the result text area and author label
        Panel bottom = new Panel();
            bottom.setLayout(new BorderLayout());
            bottom.add(resultLabel, "North");
            bottom.add(resultTa = new TextArea(4, 18), "Center");
            resultTa.setEditable(false); //Set result text area to noneditable
            resultTa.setBackground(Color.gray);
            bottom.add(authorLabel, "South");

        add(bottom, "South");  //Add Panel bottom to the BorderLayout

        //Register listener with the buttons and checkboxes
        changeBtn.addActionListener(this);
        clearBtn.addActionListener(this);
        italicCb.addItemListener(this);
        boldCb.addItemListener(this);

        originalTa.requestFocus();  //For the focus in the original text area

        setVisible(true);
    }  //End of init

    public void actionPerformed (ActionEvent ae)
    {
         if(ae.getSource() instanceof Button)
        {
             if (ae.getActionCommand().equals("No Change"))
            {
                String hold = originalTa.getText().trim();
                if (hold.equals(""))
                {
                    resultTa.setBackground(Color.gray);
                }
                else
                {
                    resultTa.setBackground(Color.white);
                    resultTa.setFont(new Font("Serif",Font.PLAIN,15));
                    italicCb.setState(false);  //Uncheck Italic checkbox
                    boldCb.setState(false);  //Uncheck Bold checkbox
                    resultTa.setText(originalTa.getText());
                }
            }
            else
            {
                originalTa.setText("");   //Clear the user text area
                resultTa.setText(""); //Clear the result text area
                italicCb.setState(false);  //Uncheck Italic checkbox
                boldCb.setState(false);  //Uncheck Bold checkbox
                resultTa.setBackground(Color.gray);
            }
        }
    }  //End of actionPerformed

    public void itemStateChanged(ItemEvent ie)
    {
        if(ie.getSource() instanceof Checkbox)
        {
            boolean italicAnswer = italicCb.getState();  //Get state of the Italic checkbox
            boolean boldAnswer = boldCb.getState();  //Get state of the Bold checkbox

            //Test if the original text area is empty
            String hold = originalTa.getText().trim();
            if (hold.equals(""))
            {
                resultTa.setBackground(Color.gray);
                italicCb.setState(false);
                boldCb.setState(false);
            }
            else
            {
                resultTa.setBackground(Color.white);
            }

            if (italicAnswer == true && boldAnswer == false)  //Set font to italic
            {
                resultTa.setFont(new Font("Serif",Font.ITALIC, 15));
                resultTa.setText(originalTa.getText());
            }
            else if (italicAnswer == true && boldAnswer == true)  //Set font to italic and bold
            {
                resultTa.setFont(new Font("Serif",Font.ITALIC+Font.BOLD, 15));
                resultTa.setText(originalTa.getText());
            }
            else if (italicAnswer == false && boldAnswer == true)  //Set font to bold
            {
                resultTa.setFont(new Font("Serif",Font.BOLD, 15));
                resultTa.setText(originalTa.getText());
            }
            else  //Set font to plain
            {
                resultTa.setFont(new Font("Serif",Font.PLAIN, 15));
                resultTa.setText(originalTa.getText());
            }
        }
    }  //End of itemStateChanged

}  //End of ButtonLabel
                
View Java Applet

Home | Java Menu | Java Applets | Java Tips | Legal
© 1999-2003 Complete e Solutions, Inc.