Monday, June 27, 2011

Java - A simple dictionary using applet

 
/*This is a simple dictionary in which you can add words and their meanings and thereafter search for the same.*/

import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 *
 * @author dhanoopbhaskar
 */
public class Main extends javax.swing.JApplet implements java.awt.event.ActionListener {

    JTextField textField = null;
    JTextArea textArea = null;
    JButton addButton = null;
    JButton showButton = null;
    String[] words = null;
    String[] meanings = null;
    int ptr = -1;

    public Main() {
        initComponents();
        setLayout(null);       
        add(textArea);
        add(textField);
        add(addButton);
        add(showButton);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == addButton) {
            String word = textField.getText();
            String meaning = textArea.getText();

            words[++ptr] = word;
            meanings[ptr] = meaning;

            textArea.setText("");
            textField.setText("");           
        }
        if(e.getSource() == showButton) {
            String word = textField.getText();
            String meaning = "";
            boolean found = false;
            for(int i = 0 ; i <= ptr ; i++) {
                if(word.equalsIgnoreCase(words[i])) {
                    textArea.setText(meanings[i]);
                    found = true;
                }
            }

            if (found == false) {
                textArea.setText("Error: Not found !!!");
            }
        }
    }

    private void initComponents() {
        words = new String[20];
        meanings = new String[20];
        textField = new JTextField(20);
        textArea = new JTextArea();
        addButton = new JButton("Add");
        showButton = new JButton("Show");
        addButton.addActionListener(this);
        showButton.addActionListener(this);
        textField.setBounds(5, 5, 250, 30);
        textArea.setBounds(5, 40, 250, 100);
        addButton.setBounds(5, 150, 70, 20);
        showButton.setBounds(185, 150, 70, 20);
    }

}


0 comments:

Post a Comment

Contact Form

Name

Email *

Message *