Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Simple Calculator has error (my first program in java with GUI) (https://www.askmehelpdesk.com/showthread.php?t=789244)

  • Apr 10, 2014, 05:49 AM
    laith-fawwaz
    Simple Calculator has error (my first program in java with GUI)
    I have a problem with my code... it's a simple code and the error I think in ' = ' button or sum button
    import javax.swing.*;


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


    public class Frame extends JFrame implements ActionListener{
    JButton b0,b1,b2,b3,before,b5,b6,b7,b8,b9,C,X,O,sum;
    JLabel Ans;
    JTextField Prob;
    public Frame(){
    this.setTitle("Calcolater");
    this.setBounds(250, 200, 500, 450);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setLayout(null);
    this.setVisible(true);

    b0=new JButton("0");
    b1=new JButton("1");
    b2=new JButton("2");
    b3=new JButton("3");
    b4=new JButton("4");
    b5=new JButton("5");
    b6=new JButton("6");
    b7=new JButton("7");
    b8=new JButton("8");
    b9=new JButton("9");
    C=new JButton("C");
    X=new JButton("X");
    O=new JButton("/");
    sum=new JButton("=");
    Ans=new JLabel();
    Prob=new JTextField();

    this.add(b0).setBounds(10, 10, 45, 45);
    this.add(b1).setBounds(10+50, 10, 45, 45);
    this.add(b2).setBounds(10+50+50, 10, 45, 45);
    this.add(b3).setBounds(10+50+50+50, 10, 45, 45);
    this.add(b4).setBounds(10, 10+50, 45, 45);
    this.add(b5).setBounds(10+50, 10+50, 45, 45);
    this.add(b6).setBounds(10+50+50, 10+50, 45, 45);
    this.add(b7).setBounds(10+50+50+50, 10+50, 45, 45);
    this.add(b8).setBounds(10, 10+50+50, 45, 45);
    this.add(b9).setBounds(10+50, 10+50+50, 45, 45);
    this.add(C).setBounds(10+50+50+25, 10+50+50, 45, 45);
    this.add(X).setBounds(10+25, 10+50+50+50, 45, 45);
    this.add(O).setBounds(10+50+25, 10+50+50+50, 45, 45);
    this.add(sum).setBounds(10+50, 10+50+50+50+50, 45, 45);
    this.add(Ans).setBounds(300, 10, 100, 10);
    this.add(Prob).setBounds(300, 10+50, 100, 25);

    b0.addActionListener(this);
    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);
    C.addActionListener(this);
    X.addActionListener(this);
    O.addActionListener(this);
    sum.addActionListener(this);

    }
    @Override
    public void actionPerformed(ActionEvent a) {

    if(a.getSource()==b0){
    Prob.setText(Prob.getText()+"0");
    }
    else if(a.getSource()==b1){
    Prob.setText(Prob.getText()+"1");
    }
    else if(a.getSource()==b2){
    Prob.setText(Prob.getText()+"2");
    }
    else if(a.getSource()==b3){
    Prob.setText(Prob.getText()+"3");
    }
    else if(a.getSource()==b4){
    Prob.setText(Prob.getText()+"4");
    }
    else if(a.getSource()==b5){
    Prob.setText(Prob.getText()+"5");
    }
    else if(a.getSource()==b6){
    Prob.setText(Prob.getText()+"6");
    }
    else if(a.getSource()==b7){
    Prob.setText(Prob.getText()+"7");
    }
    else if(a.getSource()==b8){
    Prob.setText(Prob.getText()+"8");
    }
    else if(a.getSource()==b9){
    Prob.setText(Prob.getText()+"9");
    }
    else if(a.getSource()==C){
    Prob.setText("");
    Ans.setText("");
    }
    else if(a.getSource()==X){
    Prob.setText(Prob.getText()+" X ");
    }
    else if(a.getSource()==O){
    Prob.setText(Prob.getText()+" / ");
    }
    else if(a.getSource()==sum){
    String s=Prob.getText();int Sum=1,v=0;
    for(int m=0;m<s.length();m++){
    if(s.substring(m, m+2)==" X "){
    Sum*=Integer.valueOf(s.substring(v, m-1));
    v+=m+2;
    }
    else if(s.substring(m, m+2)==" / "){
    Sum/=Integer.valueOf(s.substring(v, m-1));
    v+=m+2;
    }
    }
    Ans.setText(String.valueOf(Sum));
    }
    }

    public static void main(String [] args) {
    new Frame();
    }

    }

  • All times are GMT -7. The time now is 06:18 AM.