Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   JAVA help (http://zelaron.com/forum/showthread.php?t=39116)

platnum 2006-04-09 07:18 PM

JAVA help
 
I'm having some trouble in my java class at school. We are making a calculator program. I have the display and buttons and everything good. The only thing i need help on is getting the numbers up to the text field after i press the button. We arn't worrying about multiplication, addition or anything yet, so i dont think this will be too hard. The numbers just have to appear up in the text field. This is what i have so far.

Code:

import java.applet.*;
import java.awt.*;


public class Calculator extends Applet {

  private TextField screen;

  public void init () {
 
    this.setLayout(new GridLayout(3, 1, 3, 3));
    Panel A1 = new Panel();
    this.add(A1);
    Panel A2 = new Panel();
    this.add(A2);
    Panel A3 = new Panel();
    this.add(A3);
    A1.setLayout(new GridLayout(2, 1));
    screen = new TextField(12);
    A1.add(screen);
    Panel B1 = new Panel();
    B1.setLayout(new GridLayout(1, 4, 3, 3));
    B1.add(new Button("C"));
    B1.add(new Button("="));
    B1.add(new Button("/"));
    B1.add(new Button("*"));
    A1.add(B1);
    A2.setLayout(new GridLayout(2, 4, 3, 3));
    A2.add(new Button("7"));
    A2.add(new Button("8"));
    A2.add(new Button("9"));
    A2.add(new Button("C"));
    A2.add(new Button("4"));
    A2.add(new Button("5"));
    A2.add(new Button("6"));
    A2.add(new Button("+"));
    A3.setLayout(new GridLayout(1, 2, 3, 3));
    // 1, 2 and 0
    Panel B2 = new Panel();
    B2.setLayout(new GridLayout(2, 1, 3, 3));
    // 1 and 2
    Panel C1 = new Panel();
    C1.setLayout(new GridLayout(1, 2, 3, 3));
    C1.add(new Button("1"));
    C1.add(new Button("2"));
    B2.add(C1);
    B2.add(new Button("0"));
    // 3, . and =
    Panel B3 = new Panel();
    B3.setLayout(new GridLayout(1, 2, 3, 3));
    // 3 and .
    Panel C2 = new Panel();
    C2.setLayout(new GridLayout(2, 1, 3, 3));
    C2.add(new Button("3"));
    C2.add(new Button("."));
    B3.add(C2);
    B3.add(new Button("="));
    A3.add(B2);
    A3.add(B3);

  }

  public Insets getInsets() {
    return new Insets(5, 5, 5, 5);
  }

}


Demosthenes 2006-04-09 11:06 PM

To start out, you're going to need some sort of EventListener to acknowledge when you have cliked a button. I would set that up next.

platnum 2006-04-10 05:31 AM

Ok, so I rethought the whole thing and this is what I came up with. Anyone see any problems?

Code:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class CalculatorSUPER extends Applet implements ActionListener, KeyListener
{
final int MAX_INPUT_LENGTH = 17;
final int INPUT_MODE = 0;
final int RESULT_MODE = 1;
final int ERROR_MODE = 2;
int displayMode;
Label displayLabel;
boolean clearOnNextDigit;
double lastNumber;
char lastOperator;
public CalculatorSUPER()
        {
        super();
        setLayout (new BorderLayout());

        Panel buttonPanel = new Panel();

        Panel numberPanel = new Panel();
        numberPanel.setLayout(new GridLayout(4, 3));       

        addButtonToPanel (numberPanel, new Button("7"), Color.blue);
        addButtonToPanel (numberPanel, new Button("8"), Color.blue);
        addButtonToPanel (numberPanel, new Button("9"), Color.blue);
        addButtonToPanel (numberPanel, new Button("4"), Color.blue);
        addButtonToPanel (numberPanel, new Button("5"), Color.blue);
        addButtonToPanel (numberPanel, new Button("6"), Color.blue);
        addButtonToPanel (numberPanel, new Button("1"), Color.blue);
        addButtonToPanel (numberPanel, new Button("2"), Color.blue);
        addButtonToPanel (numberPanel, new Button("3"), Color.blue);
        addButtonToPanel (numberPanel, new Button("0"), Color.blue);


        buttonPanel.add(numberPanel);



        Panel controlPanel = new Panel();
        controlPanel.setLayout(new GridLayout(4, 1));       


        addButtonToPanel (controlPanel, new Button("C"), Color.cyan);


        buttonPanel.add(controlPanel);

        displayLabel = new Label("");
        displayLabel.setAlignment(Label.CENTER);
        add(displayLabel, "North");



        add(buttonPanel);
        addKeyListener(this);
        requestFocus();

        }
void setDisplayString(String s)
        {
        displayLabel.setText(s);
        }
String getDisplayString ()
        {
        return displayLabel.getText();
        }

void clearLastEntry()
        {
        setDisplayString("");
        clearOnNextDigit = true;
        displayMode = INPUT_MODE;
        }

void addButtonToPanel(Panel panel, Button button, Color backgroundColour)
        {
        panel.add(button);
        button.setBackground(backgroundColour);
        button.addKeyListener(this);
        button.addActionListener(this);
        }
public void actionPerformed (ActionEvent e)
        {
        processButton(e.getActionCommand());       
        }
void processButton(String command)
        {
        if (command.equals("0")) addDigit(0);
        if (command.equals("1")) addDigit(1);
        if (command.equals("2")) addDigit(2);
        if (command.equals("3")) addDigit(3);
        if (command.equals("4")) addDigit(4);
        if (command.equals("5")) addDigit(5);
        if (command.equals("6")) addDigit(6);
        if (command.equals("7")) addDigit(7);
        if (command.equals("8")) addDigit(8);
        if (command.equals("9")) addDigit(9);

        if (command.equals("C")) clearLastEntry();
        }

void addDigit(int digit)
        {

        if (clearOnNextDigit)
                setDisplayString("");


        String inputString = getDisplayString();
        if ((!inputString.equals("0") || digit > 0)  && inputString.length() < MAX_INPUT_LENGTH)
                {
                setDisplayString(inputString + digit);
                }
        displayMode = INPUT_MODE;
        clearOnNextDigit = false;
        }
       

public void keyPressed(KeyEvent e)
        {
        }
public void keyReleased(KeyEvent e)
        {
        }
       
public void keyTyped(KeyEvent e)
        {
        String command;
        char keyChar = e.getKeyChar();
  if (keyChar == KeyEvent.VK_ESCAPE)
                {
                command = new String("C");
                }
        else
                {
               
                byte bytes[] = {(byte)keyChar};
                command = new String(bytes);
                }
        processButton(command);
        }
}



All times are GMT -6. The time now is 08:36 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.