Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Applet Calculator

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Applet Calculator

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

    public class Compute extends java.applet.Applet implements ActionListener{
    char sol;
    String s1, s2;
    double d1,d2,d3;
    String str=" ";

    Label lbl=new Label();

    Button one, two, three, four, five, six, seven, eight, nine, zero, dot, plus, min, div, mult, eq;
    Button cmd= new Button("Clear");

    public void init() {
    setLayout(null);
    setBackground(Color.BLACK);

    lbl=new Label();
    lbl.setBackground(Color.GRAY);
    add(lbl);
    lbl.setBounds(20, 20, 100, 25);

    one=new Button("1");
    add(one);
    one.setBounds(20, 50, 25, 25);
    one.addActionListener(this);

    two=new Button("2");
    add(two);
    two.setBounds(45, 50, 25, 25);
    two.addActionListener(this);

    three=new Button("3");
    add(three);
    three.setBounds(70, 50, 25, 25);
    three.addActionListener(this);

    four=new Button("4");
    add(four);
    four.setBounds(20, 75, 25, 25);
    four.addActionListener(this);

    five=new Button("5");
    add(five);
    five.setBounds(45, 75, 25, 25);
    five.addActionListener(this);

    six=new Button("6");
    add(six);
    six.setBounds(70, 75, 25, 25);
    six.addActionListener(this);

    seven=new Button("7");
    add(seven);
    seven.setBounds(20, 100, 25, 25);
    seven.addActionListener(this);

    eight=new Button("8");
    add(eight);
    eight.setBounds(45, 100, 25, 25);
    eight.addActionListener(this);

    nine=new Button("9");
    add(nine);
    nine.setBounds(70, 100, 25, 25);
    nine.addActionListener(this);

    zero=new Button("0");
    add(zero);
    zero.setBounds(20, 125, 25, 25);
    zero.addActionListener(this);

    plus=new Button("+");
    add(plus);
    plus.setBounds(95, 50, 25, 25);
    plus.addActionListener(this);

    min=new Button("-");
    add(min);
    min.setBounds(95, 75, 25, 25);
    min.addActionListener(this);

    mult=new Button("*");
    add(mult);
    mult.setBounds(95, 100, 25, 25);
    mult.addActionListener(this);

    div=new Button("/");
    add(div);
    div.setBounds(95, 125, 25, 25);
    div.addActionListener(this);

    dot=new Button(".");
    add(dot);
    dot.setBounds(45, 125, 25, 25);
    dot.addActionListener(this);

    eq=new Button("=");
    add(eq);
    eq.setBounds(70, 125, 25, 25);
    eq.addActionListener(this);

    add(cmd);
    cmd.setBounds(20, 150, 50, 20);
    cmd.addActionListener(this);
    }

    public void paint(Graphics g) {

    }
    public void actionPerformed(ActionEvent x)
    {
    if(x.getSource()==one)
    str=str+"1";
    lbl.setText(str);
    if(x.getSource()==two)
    str=str+"2";
    lbl.setText(str);
    if(x.getSource()==three)
    str=str+"3";
    lbl.setText(str);
    if(x.getSource()==zero)
    str=str+"0";
    lbl.setText(str);
    if(x.getSource()==four)
    str=str+"4";
    lbl.setText(str);
    if(x.getSource()==five)
    str=str+"5";
    lbl.setText(str);
    if(x.getSource()==six)
    str=str+"6";
    lbl.setText(str);
    if(x.getSource()==seven)
    str=str+"7";
    lbl.setText(str);
    if(x.getSource()==eight)
    str=str+"8";
    lbl.setText(str);
    if(x.getSource()==nine)
    str=str+"9";
    lbl.setText(str);
    if(x.getSource()==dot)
    str=str+".";
    lbl.setText(str);

    if(x.getSource()==plus)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim());
    sol='+';

    if(x.getSource()==min)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim());
    sol='-';

    if(x.getSource()==mult)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim());
    sol='*';

    if(x.getSource()==div)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim());
    sol='/';

    if(x.getSource()==eq)
    {

    s2=lbl.getText();
    d2=Double.parseDouble(s2.trim());
    String s3=" ";
    switch(sol)
    {
    case '+':
    d3=d1+d2;
    break;
    case '-':
    d3=d1-d2;
    break;
    case '*':
    d3=d1*d2;
    case '/':
    d3=d1/d2;
    break;
    }
    s3=Double.toString(d3);
    lbl.setText(s3);


    if(x.getSource()==cmd)
    {
    s1=" ";
    s2=" ";
    d1=0;
    d2=0;
    d3=0;
    lbl.setText(" ");
    }


    }//end of eq
    }//end of actionPerformed
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Applet Calculator

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Did you have any questions?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet Calculator

    What?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Applet Calculator

    What's on second base.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet Calculator

    Can I have a sample format?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Applet Calculator

    Format of what? There are tons of properly formatted code on the forum. Take a look.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet Calculator

    What do you mean by?
    Please edit your post and wrap your code with
    <YOUR CODE HERE>
    to get highlighting and preserve formatting.

    --- Update ---

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
     
    public class Compute extends java.applet.Applet implements ActionListener{
    char sol;
    String s1, s2;
    double d1,d2,d3;
    String str=" ";
     
    Label lbl=new Label();
     
    Button one, two, three, four, five, six, seven, eight, nine, zero, dot, plus, min, div, mult, eq;
    Button cmd= new Button("Clear");
     
    public void init() {
    setLayout(null);
    setBackground(Color.BLACK);
     
    lbl=new Label();
    lbl.setBackground(Color.GRAY);
    add(lbl);
    lbl.setBounds(20, 20, 100, 25);
     
    one=new Button("1");
    add(one);
    one.setBounds(20, 50, 25, 25);
    one.addActionListener(this);
     
    two=new Button("2");
    add(two);
    two.setBounds(45, 50, 25, 25);
    two.addActionListener(this);
     
    three=new Button("3");
    add(three);
    three.setBounds(70, 50, 25, 25);
    three.addActionListener(this);
     
    four=new Button("4");
    add(four);
    four.setBounds(20, 75, 25, 25);
    four.addActionListener(this);
     
    five=new Button("5");
    add(five);
    five.setBounds(45, 75, 25, 25);
    five.addActionListener(this);
     
    six=new Button("6");
    add(six);
    six.setBounds(70, 75, 25, 25);
    six.addActionListener(this);
     
    seven=new Button("7");
    add(seven);
    seven.setBounds(20, 100, 25, 25);
    seven.addActionListener(this);
     
    eight=new Button("8");
    add(eight);
    eight.setBounds(45, 100, 25, 25);
    eight.addActionListener(this);
     
    nine=new Button("9");
    add(nine);
    nine.setBounds(70, 100, 25, 25);
    nine.addActionListener(this);
     
    zero=new Button("0");
    add(zero);
    zero.setBounds(20, 125, 25, 25);
    zero.addActionListener(this);
     
    plus=new Button("+");
    add(plus);
    plus.setBounds(95, 50, 25, 25);
    plus.addActionListener(this);
     
    min=new Button("-");
    add(min);
    min.setBounds(95, 75, 25, 25);
    min.addActionListener(this);
     
    mult=new Button("*");
    add(mult);
    mult.setBounds(95, 100, 25, 25);
    mult.addActionListener(this);
     
    div=new Button("/");
    add(div);
    div.setBounds(95, 125, 25, 25);
    div.addActionListener(this);
     
    dot=new Button(".");
    add(dot);
    dot.setBounds(45, 125, 25, 25);
    dot.addActionListener(this);
     
    eq=new Button("=");
    add(eq);
    eq.setBounds(70, 125, 25, 25);
    eq.addActionListener(this);
     
    add(cmd);
    cmd.setBounds(20, 150, 50, 20);
    cmd.addActionListener(this);
    }
     
    public void paint(Graphics g) {
     
    }
    public void actionPerformed(ActionEvent x)
    {
    if(x.getSource()==one)
    str=str+"1";
    lbl.setText(str);
    if(x.getSource()==two)
    str=str+"2";
    lbl.setText(str);
    if(x.getSource()==three)
    str=str+"3";
    lbl.setText(str);
    if(x.getSource()==zero)
    str=str+"0";
    lbl.setText(str);
    if(x.getSource()==four)
    str=str+"4";
    lbl.setText(str);
    if(x.getSource()==five)
    str=str+"5";
    lbl.setText(str);
    if(x.getSource()==six)
    str=str+"6";
    lbl.setText(str);
    if(x.getSource()==seven)
    str=str+"7";
    lbl.setText(str);
    if(x.getSource()==eight)
    str=str+"8";
    lbl.setText(str);
    if(x.getSource()==nine)
    str=str+"9";
    lbl.setText(str);
    if(x.getSource()==dot)
    str=str+".";
    lbl.setText(str);
     
    if(x.getSource()==plus)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim());
    sol='+';
     
    if(x.getSource()==min)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim());
    sol='-';
     
    if(x.getSource()==mult)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim()); 
    sol='*';
     
    if(x.getSource()==div)
    s1=lbl.getText();
    str=" ";
    lbl.setText(str);
    d1=Double.parseDouble(s1.trim()); 
    sol='/';
     
    if(x.getSource()==eq)
    {
     
    s2=lbl.getText();
    d2=Double.parseDouble(s2.trim());
    String s3=" ";	
    switch(sol)
    {
    case '+':
    d3=d1+d2;
    break;
    case '-':
    d3=d1-d2;
    break;
    case '*':
    d3=d1*d2;
    case '/':
    d3=d1/d2;
    break;
    }
    s3=Double.toString(d3);
    lbl.setText(s3); 
     
     
    if(x.getSource()==cmd)
    {
    s1=" ";
    s2=" ";
    d1=0;
    d2=0;
    d3=0;
    lbl.setText(" ");
    }
     
     
    }//end of eq
    }//end of actionPerformed
    }


    --- Update ---

    When I click on the buttons nothing displays in the Label.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Applet Calculator

    The posted code is very hard to read because of its formatting.
    The posted code needs to be properly formatted. Statements nested inside {}s need to be indented 3-4 spaces.
    The statements should NOT all start in the left column.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. HELP - GUI Java Applet Calculator..
    By AtOmTen in forum AWT / Java Swing
    Replies: 2
    Last Post: March 24th, 2013, 06:13 AM
  2. Interest calculator applet
    By bruizer in forum Java Applets
    Replies: 4
    Last Post: September 22nd, 2012, 08:38 PM
  3. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM
  4. Replies: 0
    Last Post: October 13th, 2011, 07:42 PM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM

Tags for this Thread