Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Petr

Thread: Writing a caclulator

  1. #1
    michal_2 is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Writing a caclulator

    I would like to write a calculator. And there is a problem. I doi't know how set buttons in calculator positions like:

    Java Code:
    1 2 3
    4 5 6
    7 8 9
      0
    this is my code:
    Java Code:
    package pl.test.java;
     
    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Graphics;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    /* <applet code="Test" width=400 height=200></applet>*/
     
    public class Test extends Applet implements ActionListener{
     
            TextField wyswietlacz;
            Button one, two, three, four, five, six, seven, eight, nine, zero;
     
            public void init()
            {
                    wyswietlacz = new TextField(20);
                    one = new Button("1");
                    two = new Button("2");
                    three = new Button("3");
                    four = new Button("4");
                    five = new Button("5");
                    six = new Button("6");
                    seven = new Button("7");
                    eight = new Button("8");
                    nine = new Button("9");
                    zero = new Button("0");
     
                    wyswietlacz.setEditable(false);
     
                    one.addActionListener(this);
                    two.addActionListener(this);
                    three.addActionListener(this);
                    four.addActionListener(this);
                    five.addActionListener(this);
                    six.addActionListener(this);
                    seven.addActionListener(this);
                    eight.addActionListener(this);
                    nine.addActionListener(this);
                    zero.addActionListener(this);
     
                    add(wyswietlacz);
                    add(one);
                    add(two);
                    add(three);
                    add(four);
                    add(five);
                    add(six);
                    add(seven);
                    add(eight);
                    add(nine);
                    add(zero);
            }
     
            public void actionPerformed(ActionEvent ae)
            {
                    String str = ae.getActionCommand();
     
                    if (str.equals("1"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "1";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("2"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "2";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("3"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "3";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("4"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "4";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("5"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "5";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("6"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "6";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("7"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "7";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("8"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "8";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("9"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "9";
                            wyswietlacz.setText(st);
                    }
                    else if (str.equals("0"))
                    {
                            String st;
                            st = wyswietlacz.getText();
                            st += "0";
                            wyswietlacz.setText(st);
                    }
            }
     
            public void paint(Graphics g)
            {
            }
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default Re: Writing a caclulator

    sunde887 likes this.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Writing a caclulator

    I'm sorry, but I don't see where a JLayeredPane would come in handy for a calculator GUI. What the OP needs is to learn about layout managers: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Need Help W/ Writing to DB.
    By Sabinno in forum JDBC
    Replies: 2
    Last Post: 05-26-2011, 09:25 AM
  2. writing to ram
    By Timmins in forum New To Java
    Replies: 0
    Last Post: 04-03-2011, 08:52 AM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. Writing in XLS
    By selva.bics in forum Advanced Java
    Replies: 1
    Last Post: 11-09-2009, 09:09 AM
  5. writing to gui
    By rob in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •