Calculator +textfield help Howto add?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Calculator +textfield help Howto add?

  1. #1
    Join Date
    Sep 2004
    Posts
    59

    Calculator +textfield help Howto add?

    Hi, ive a question,


    How can i add a textfield to show the digits that i pressed from a button?

    and how do i use getsource to add get the correct digit from the buttons?

    this is my code:


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

    public class RekenMachine
    extends Frame
    implements WindowListener
    {
    public RekenMachine (String title)
    {
    super (title);
    addWindowListener (this);
    setLayout (new BorderLayout());


    Panel p = new Panel() ;
    p.setLayout (new GridLayout (4,3));
    Button b1 = new Button ("1");
    Button b2 = new Button ("2");
    Button b3 = new Button ("3");
    Button b4 = new Button ("4");
    Button b5 = new Button ("5");
    Button b6 = new Button ("6");
    Button b7 = new Button ("7");
    Button b8 = new Button ("8");
    Button b9 = new Button ("9");
    Button Clear = new Button ("Clear");
    Button b0 = new Button ("0");
    Button Stop = new Button ("Stop");

    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    p.add(b6);
    p.add(b7);
    p.add(b8);
    p.add(b9);
    p.add(Clear);
    p.add(b0);
    p.add(Stop);
    add (p);

    }

    public void windowClosing (WindowEvent e)
    {
    System.exit (0);
    }
    public void windowActivated (WindowEvent e) { }
    public void windowClosed (WindowEvent e) { }
    public void windowDeactivated (WindowEvent e) { }
    public void windowDeiconified (WindowEvent e) { }
    public void windowIconified (WindowEvent e) { }
    public void windowOpened (WindowEvent e) { }

    public static void main (String [ ] argv)
    {
    RekenMachine ld = new RekenMachine ("RekenMachine");
    ld.setSize (300, 400);
    ld.setVisible (true);
    }

    public void actionPerformed (ActionEvent e)
    {
    // if (event.getSource() == b1)
    // Button b1.getText();
    }

    public void ActionListener (ActionEvent e)

    {



    }

    }


    thnx
    My own java video tutorials, feel free to watch it.
    http://www.engineeringserver.com/for...amming-b311.0/ Need java help? For beginning and intermediate java coders!

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    See my comments:

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class RekenMaskin extends Frame
        implements WindowListener, ActionListener { // actionlistener for buttons
      TextField disp=null;
      public RekenMaskin (String title) {
      super (title);
      addWindowListener (this);
      setLayout (new BorderLayout());
    
    
      disp=new TextField(); // add textfield for display
      Panel p = new Panel() ;
      p.setLayout (new GridLayout (4,3));
      Button b1 = new Button ("1");
      Button b2 = new Button ("2");
      Button b3 = new Button ("3");
      Button b4 = new Button ("4");
      Button b5 = new Button ("5");
      Button b6 = new Button ("6");
      Button b7 = new Button ("7");
      Button b8 = new Button ("8");
      Button b9 = new Button ("9");
      Button Clear = new Button ("Clear");
      Button b0 = new Button ("0");
      Button Stop = new Button ("Stop");
    
      b0.addActionListener(this); // add this frame to the buttons actionlisteners
      b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b4.addActionListener(this);
      b5.addActionListener(this);
      b6.addActionListener(this);
      b7.addActionListener(this);
      b8.addActionListener(this);
      b9.addActionListener(this);
      Clear.addActionListener(this);
      Stop.addActionListener(this);
    
      p.add(b1);
      p.add(b2);
      p.add(b3);
      p.add(b4);
      p.add(b5);
      p.add(b6);
      p.add(b7);
      p.add(b8);
      p.add(b9);
      p.add(Clear);
      p.add(b0);
      p.add(Stop);
      add (p,BorderLayout.CENTER);
      add(disp, BorderLayout.NORTH);
    
      }
    
      public void windowClosing (WindowEvent e) {
        System.exit (0);
      }
      public void windowActivated (WindowEvent e) { }
      public void windowClosed (WindowEvent e) { }
      public void windowDeactivated (WindowEvent e) { }
      public void windowDeiconified (WindowEvent e) { }
      public void windowIconified (WindowEvent e) { }
      public void windowOpened (WindowEvent e) { }
    
      public static void main (String [ ] argv) {
        RekenMaskin ld = new RekenMaskin ("RekenMaskin");
        ld.setSize (300, 400);
        ld.setVisible (true);
      }
    
      public void actionPerformed (ActionEvent e) {
        String s=e.getActionCommand();
        if (s.equals("Clear")) {
          this.disp.setText("");
        } else if (s.equals("Stop")) {
          this.dispose();
        } else {
          String ss=this.disp.getText().trim()+s;
          disp.setText(ss);
        }
      }
    // whats this code doing here...?
    public void ActionListener (ActionEvent e)
    
    {
    
    
    
    }
    
    }
    eschew obfuscation

  3. #3
    Join Date
    Sep 2004
    Posts
    59
    Originally posted by sjalle
    See my comments:

    code
    [/code]
    omfg thank you :-) i cant believe ive missed that.

    You saved my day ^^

    again, thank you.
    My own java video tutorials, feel free to watch it.
    http://www.engineeringserver.com/for...amming-b311.0/ Need java help? For beginning and intermediate java coders!

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center