It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Layout Manager problem

Layout Manager problem

Postby cascade on Sun Feb 04, 2007 6:48 pm

Hi people...i'm new to java and at the moment i'm using the .awt layout managers for an applet project i'm doing. However i've tryed everything i know to get a specific display and it's becoming very annoying to do.
The display i'm trying to acheive is a keypad at the bottom of the applet (from 0 to 9, and a to z) and two text fields above the keypad, one at the top(labelled - input), and one below( labeled - result). any help will be appreciated. (ps. nevermind about the calculations as i havent finished that yet - its just the display i cant do) thanks in advance

the code i've got so far is:
Code: Select all
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;

public class RecogniseNumber3 extends java.applet.Applet
                    implements ActionListener
{
    final int IDLE = 0;
    final int BUILDING_REAL = 1;
    final int BUILDING_INT = 2;
    final int INVALID = 3;
    int state;//current user holds state
    Panel ip;//contains user buttons
    TextField input;//displays current number
    TextField result;//displays result of number check
    StringBuffer currentinput;//input characters stored here
    char currentchar;

     
   
   
    public void init()
    {
       
       
        setLayout(new BorderLayout());
        input = new TextField(24);
        input.setEditable(false);//read only
        add("North", input);
        result = new TextField(24);
        result.setEditable(false);//read only
        add("Center", result);       
        ip = new Panel();
        ip.setLayout(new GridLayout(4,3,5,5));
        Button b;
        for (char input='a'; input < 'z' + 1; input++)
        {
            b = new Button ("" + input);
            b.addActionListener(this);
            ip.add(b);
        }//end for
        for (char input='0'; input < '9' + 1; input++ )
        {
            b = new Button ("" + input);
            b.addActionListener(this);
            ip.add(b);
        }//end for
        b = new Button(".");
        b.addActionListener(this);
        ip.add(b);
       
        b = new Button("Finish");
        b.addActionListener(this);
        ip.add(b);
       
        add("South", ip);
        currentinput = new StringBuffer();
        state = IDLE;//initial state
    }//end init
   
    public void handleChar()
    {
        switch(state)
            {
            case IDLE:
                    result.setText("");//clear result of any display
                    input.setText("");//clear any previous display
                    if (currentchar == '.')//decimal point so must be real
                    {
                        state = BUILDING_REAL;
                        currentinput.append(currentchar);
                        //add to the input string
                       
                        input.setText(currentinput.toString());
                        //refresh output window
                       
                        break;
                    }
                    else//must be a digit
                    {
                        state = BUILDING_INT;
                        currentinput.append(currentchar);
                        input.setText(currentinput.toString());
                        break;
                    }
            case BUILDING_REAL:
                if (currentchar == '.')//cant have two
                {
                    state = INVALID;
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
                else
                {
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
            case BUILDING_INT:
                if (currentchar == '.')
                {
                    state = BUILDING_REAL;
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
                else
                {
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
            case INVALID:
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
        }//end switch
    }//end handleChar
       
   

    public void finish()
    {
        switch(state)
        {
            case BUILDING_REAL:
                 result.setText("Valid");
                 state = IDLE;
                 currentinput = new StringBuffer();
                 //discard old string bufffer
                 break;
                 
            case BUILDING_INT:
                 result.setText("The number is an integer");
                 state = IDLE;
                 currentinput = new StringBuffer();
                 break;
                 
            case INVALID:
                 result.setText("Invalid");
                 state = IDLE;
                 currentinput = new StringBuffer();
        }//end switch
    }//end finish
 
    public void actionPerformed(ActionEvent event)
    {
        String command = event.getActionCommand();
        if (event.getSource() instanceof Button)
        {
            if (command.equals("Finish"))
            {
                finish();
            }
            else
            {
                currentchar = command.charAt(0);
                handleChar();
            }
        }
    }
}

cascade
 
Posts: 2
Joined: Sun Feb 04, 2007 6:44 pm

Who is online

Users browsing this forum: No registered users and 0 guests