DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2005
    Posts
    1

    Calculator help plz..i really need help!

    ok so im making a Calculator..and i got the graphic part..and id ont know how to do the math..
    i got 2 files one for the graphic..and the other for the math..for like the caluction
    import java.awt.*;
    import java.awt.event.*;
    public class Calculatorsgui extends Frame implements WindowListener
    {
    protected Button plus, subract, times, divide, exitButton;
    protected TextField input1, input2, answer;
    private Calculatorsgui controller;

    public Calculatorsgui ()
    {
    setTitle ("Calculator"); // Set the frame's name

    setLayout (new FlowLayout (FlowLayout.CENTER));

    setSize (180, 180); // Set the frame's size


    // SimpleController is a class that handles the button clicks
    CalculatorsController controller = new CalculatorsController (this);

    // Adding an empty non-editable text field


    Label l = new Label ("Number1");
    add ("South", l);

    input1 = new TextField (10);
    input1.setEditable (true);
    add ("Center", input1);

    Label a = new Label ("Number2");
    add ("South", a);

    input2 = new TextField (10);
    input2.setEditable (true);
    add ("Center", input2);

    // add buttons to the frame and connect the controller to it
    plus = new Button ("+");
    plus.addActionListener (controller);
    add (plus);

    subract = new Button ("-");
    subract.addActionListener (controller);
    add (subract);

    times = new Button ("*");
    times.addActionListener (controller);
    add (times);

    divide = new Button ("/");
    divide.addActionListener (controller);
    add (divide);


    Label b = new Label ("The Answer");
    add ("South", b);

    answer = new TextField (10);
    answer.setEditable (false);
    add ("Center", answer);



    exitButton = new Button ("exit");
    exitButton.addActionListener (controller);
    add (exitButton);

    // Make the frame handle window events

    addWindowListener (this);

    show (); // Show the frame or use setVisible (true)

    } // Constructor


    public void paint (Graphics g)
    {
    // Place the drawing code here
    } // paint method


    public void windowDeiconified (WindowEvent event)
    {
    }


    public void windowIconified (WindowEvent event)
    {
    }


    public void windowActivated (WindowEvent event)
    {
    }


    public void windowDeactivated (WindowEvent event)
    {
    }


    public void windowClosed (WindowEvent event)
    {
    }


    public void windowClosing (WindowEvent event)
    {
    System.exit (0);
    }


    public void windowOpened (WindowEvent event)
    {
    }


    public static void main (String[] args)
    {
    new Calculatorsgui (); // Create a SimpleExample frame
    } // main method
    }



    then for my sencond file..i have no idea wat to do!this is wat i got..i need adding..and divide..and subtract

    import java.awt.*;
    import java.awt.event.*;
    public class CalculatorsController implements ActionListener
    {
    Calculatorsgui view;

    public CalculatorsController (Calculatorsgui view)
    {
    this.view = view;
    }


    public void actionPerformed (ActionEvent event)
    {

    // You can get the source object of an event

    Object source = event.getSource ();

    //You can compare the event source with actual objects.

    if (source == view.exitButton)
    System.exit (0);


    so if anyone can do thing for me..u be my hero..no JOKES!!!!!!!!!! PLEASE HELP ..im on my knees

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    First, you cannot add components to a flowlayout container using borderlayout constraints (north, south,
    east,west and center), and if you use borderlayout
    you cannot add more than 5 components, one for each
    place.

    If you want to respond to buttonclicks then the usual
    approach is to let the frame implement the
    ActionListener interface. You don't have to make a
    special class for listening to events, any class can do that, as long as it implements the event's listener interface.

    Leaving out the number-keypad solution your code could
    be done like this:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    class Calculatorsgui extends Frame implements ActionListener {
      protected Button plus, subract, times, divide, exitButton;
      protected TextField input1, input2, answer;
    
    
      public static void main(String[] args) {
        Calculatorsgui view = new Calculatorsgui();
      }
      /**
       * Constructor
       */
      public Calculatorsgui() {
        setTitle("Calculator"); // Set the frame's name
        setLayout(new FlowLayout(FlowLayout.CENTER));
        setSize(180, 180); // Set the frame's size
    
    // Adding an empty non-editable text field
    
        Label lb1 = new Label("Number1");
        add(lb1);
        input1 = new TextField(10);
        add(input1);
    
        Label lb2 = new Label("Number2");
        add(lb2);
    
        input2 = new TextField(10);
        input2.setEditable(true);
        add(input2);
    
    // add buttons to the frame and connect the controller to it
        plus = new Button("+");
        plus.addActionListener(this);
        add(plus);
    
        subract = new Button("-");
        subract.addActionListener(this);
        add(subract);
    
        times = new Button("*");
        times.addActionListener(this);
        add(times);
    
        divide = new Button("/");
        divide.addActionListener(this);
        add(divide);
    
        Label lbl3 = new Label("The Answer");
        add(lbl3);
    
        answer = new TextField(10);
        answer.setEditable(false);
        add(answer);
    
        exitButton = new Button("exit");
        exitButton.addActionListener(this);
        add(exitButton);
    
        setVisible(true); // show() is deprecated....
    
      }
      private void calculate (Button btn) {
        try {
          double num1 = Double.parseDouble(input1.getText().trim());
          double num2 = Double.parseDouble(input2.getText().trim());
          if (btn==plus) {
            answer.setText(Double.toString(num1+num2));
          } else if (btn==subract) {
            answer.setText(Double.toString(num1-num2));
          } else if (btn==times) {
            answer.setText(Double.toString(num1*num2));
          } else if (btn==divide) {
            if (num2==0.0d) {
              answer.setText("div by 0 !");
            } else {
              answer.setText(Double.toString(num1/num2));
            }
          }
        }
        catch (NumberFormatException ex) {
          answer.setText("*bad input*");
        }
      }
      public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == exitButton) {
          System.exit(0);
        } else {
          calculate((Button)source);
        }
    
      }
    }
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links