Ask Experts Questions for FREE Help !
Ask
    laith-fawwaz's Avatar
    laith-fawwaz Posts: 1, Reputation: 1
    New Member
     
    #1

    Apr 10, 2014, 05:49 AM
    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();
    }

    }

Check out some similar questions!

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

Is there any Gui Based Program in Linux as Task Manager in Windows [ 5 Answers ]

Hello I'm new to Linux I'm getting problem in process handling Is there any GUI based program in Linux as Task Manager in Windows so that I can handle processs easily Please assist me Regards

How to write The GUI program that will find weighted average of four testscores [ 1 Answers ]

To write The GUI program that will find weighted average of four testscores.The four testscores and their respective weights

Java program error [ 1 Answers ]

Sir, program is for managing account details using jdbc but ,there error like some variable not accessible in specefic block . Please solve this problem. If you can.. //mypanel1-LogIn class mypanel1 extends JPanel { JTextField t1,t2; JButton b1,b2; mypanel1() { JLabel l1,l2; ...

Error: Java Virtual Machine Launcher "Could not find the main class program will exit [ 3 Answers ]

Dear Sir / Madam, When I am trying to open .exj extension file, I am getting error as mentioned above. Please help me, whether I have to install any java module or something other has to be done. Thanks


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.