java calculator με jframe

Συζητήσεις για την Java

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
NTINAaa
Δημοσιεύσεις: 21
Εγγραφή: 16 Μαρ 2009 00:25

java calculator με jframe

Δημοσίευση από NTINAaa » 26 Μάιος 2010 00:07

καλησπέρα

έχω να φτιάξω ένα calculator σε κωδικα java και αντιμετωπίζω ενα πρόβλημα ,όποιος μπορεί ας βοηθήσει!Το πρόβλημα στον κώδικα που έχω φτίαξει είναι πως αντί να γίνεται η πράξη ανάμεσα στους δύο αριθμούς που δίνω ,η πράξη γίνεται ανάμεσα στο πρώτο και στον ευατό του
και δεν ξέρω πως ακριβώς διορθώνεται αυτό........

Ο κωδικας είναι ο παρακάτω....

Κώδικας: Επιλογή όλων

package javaapplication15;

/**
 *
 * @author ntina
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

 class Calc extends JFrame {

     Container cp;
     GridLayout gl;
     JPanel pl1,pl2,pl3;
     JTextField number = new JTextField("",3);
     JButton one =new JButton("1");
     JButton two =new JButton("2");
     JButton three =new JButton("3");
     JButton four =new JButton("4");
     JButton five =new JButton("5");
     JButton six =new JButton("6");
     JButton seven =new JButton("7");
     JButton eight =new JButton("8");
     JButton nine =new JButton("9");
     JButton zero =new JButton("0");
     JButton add =new JButton("+");
     JButton sub =new JButton("-");
     JButton mult =new JButton("*");
     JButton div =new JButton("/"); 
     JButton equals =new JButton("=");
     JButton clear =new JButton("clear");
     JButton komma=new JButton(".");
     String result;
     ButtonGroup radio;
     String s;
     
    
     /**
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Calc app=new Calc();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setSize(200,300);
        app.setVisible(true);

        // TODO code application logic here
    }


    public Calc(){
    super ("Calculator");
    ButtonHandler1 handler1=new ButtonHandler1();
    ButtonHandler2 handler2=new ButtonHandler2();
     ButtonHandler3 handler3=new ButtonHandler3();
 JPanel numbers=new JPanel();

    numbers.add(number);
    numbers.add(one);
    numbers.add(two);
    numbers.add(three);
    numbers.add(four);
    numbers.add(five);
    numbers.add(six);
    numbers.add(seven);
    numbers.add(eight);
    numbers.add(nine);
    numbers.add(zero);
    one.addActionListener(handler2);
    two.addActionListener(handler2);
    three.addActionListener(handler2);
    four.addActionListener(handler2);
    five.addActionListener(handler2);
    six.addActionListener(handler2);
    seven.addActionListener(handler2);
    eight.addActionListener(handler2);
    nine.addActionListener(handler2);
    zero.addActionListener(handler2);

    JPanel prakseis=new JPanel();
    prakseis.add(add);
    prakseis.add(sub);
    prakseis.add(div);
    prakseis.add(mult);

    add.addActionListener(handler3);
    sub.addActionListener(handler3);
    div.addActionListener(handler3);
    mult.addActionListener(handler3);
    JPanel others=new JPanel();
    others.add(equals);
    others.add(komma);
    others.add(clear);
    equals.addActionListener(handler3);
    clear. addActionListener(handler1);
     cp=getContentPane();
    gl=new GridLayout(3,2);
    cp.setLayout(gl);
   
    cp.add(numbers);
    cp.add(prakseis);
    cp.add(others);
   


    }

    private class ButtonHandler1  implements ActionListener {
   public void actionPerformed(ActionEvent e) {





            //result.setText("Press to copy");
            number.setText( " ");


   }
 }

    private class ButtonHandler2 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if (e.getSource()==one)
                number.setText("1");
            else if (e.getSource()==two)
                 number.setText("2");
            else if (e.getSource()==three)
                 number.setText("3");
             else if (e.getSource()==four)
                 number.setText("4");
              else if (e.getSource()==five)
                 number.setText("5");
              else if (e.getSource()==six)
                 number.setText("6");
               else if (e.getSource()==seven)
                 number.setText("7");
             else if (e.getSource()==eight)
                 number.setText("8");
             else if (e.getSource()==nine)
                  number.setText("9");
             else if (e.getSource()==zero)
                   number.setText("0");
            
        }


    }


     private class ButtonHandler3 implements ActionListener{
        public void actionPerformed(ActionEvent e){
           

             int value1=Integer.parseInt(number.getText());
           
             int value=Integer.parseInt(number.getText());
             if(e.getSource()==add)
             {

                

                   result=("" + (value1 + value));
             }
             if(e.getSource()==sub)
            {

                 

                 result=("" + (value1 - value));
             }
             if(e.getSource()==mult)
            {

               
              result=("" + (value1 * value));
             }
             if(e.getSource()==div)
            {

                

               result=("" + (value1 / value));
             }
             if(e.getSource()==equals)
            {


                number.setText(result);
             }
          
        }
    }
 }


Κάθε βοήθεια ευπρόσδεκτη...σας ευχαριστώ!!!

goten002
Δημοσιεύσεις: 5
Εγγραφή: 24 Ιαν 2009 18:11
Επικοινωνία:

java calculator με jframe

Δημοσίευση από goten002 » 28 Μάιος 2010 13:53

koita i logiki sou den einai k toso sosti. to vasiko lathos pou ekanes itan epernes to idio noumeno to evazes stin metavlites value1 k value ekanes tin praksi k meta otan patouses to = emfanize to apotelesma sto label. egw alaksa ligo ton kodika sou k prosthesa mia boolean metavliti pou elenxi an einai to 1o noumero auto pou edoses opote me auto ton tropo tha valei ton enan arithmo sto value1 k ton allo sto value. episeis etsi opws to exeis ftiaksei, ekteli mia praksi kathe fora pou patas ena button + - * / dld gia na kaneis mia prosthesi prepei na patiseis
10 + 5 + kai to = gia na sou emfanisi to apotelesma.
o kwdikas einai:

Κώδικας: Επιλογή όλων

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication3;

/**
 *
 * @author ntina
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

 class Calc extends JFrame {

     Container cp;
     GridLayout gl;
     JPanel pl1,pl2,pl3;
     JTextField number = new JTextField("",3);
     JButton one =new JButton("1");
     JButton two =new JButton("2");
     JButton three =new JButton("3");
     JButton four =new JButton("4");
     JButton five =new JButton("5");
     JButton six =new JButton("6");
     JButton seven =new JButton("7");
     JButton eight =new JButton("8");
     JButton nine =new JButton("9");
     JButton zero =new JButton("0");
     JButton add =new JButton("+");
     JButton sub =new JButton("-");
     JButton mult =new JButton("*");
     JButton div =new JButton("/");
     JButton equals =new JButton("=");
     JButton clear =new JButton("clear");
     JButton komma=new JButton(".");
     String result;
     ButtonGroup radio;
     String s;

     boolean isFirstNum=true;
     int value1,value=0;
     /**
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Calc app=new Calc();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setSize(200,300);
        app.setVisible(true);

        // TODO code application logic here
    }


    public Calc(){
    super ("Calculator");
    ButtonHandler1 handler1=new ButtonHandler1();
    ButtonHandler2 handler2=new ButtonHandler2();
     ButtonHandler3 handler3=new ButtonHandler3();
 JPanel numbers=new JPanel();

    numbers.add(number);
    numbers.add(one);
    numbers.add(two);
    numbers.add(three);
    numbers.add(four);
    numbers.add(five);
    numbers.add(six);
    numbers.add(seven);
    numbers.add(eight);
    numbers.add(nine);
    numbers.add(zero);
    one.addActionListener(handler2);
    two.addActionListener(handler2);
    three.addActionListener(handler2);
    four.addActionListener(handler2);
    five.addActionListener(handler2);
    six.addActionListener(handler2);
    seven.addActionListener(handler2);
    eight.addActionListener(handler2);
    nine.addActionListener(handler2);
    zero.addActionListener(handler2);

    JPanel prakseis=new JPanel();
    prakseis.add(add);
    prakseis.add(sub);i
    prakseis.add(div);
    prakseis.add(mult);

    add.addActionListener(handler3);
    sub.addActionListener(handler3);
    div.addActionListener(handler3);
    mult.addActionListener(handler3);
    JPanel others=new JPanel();
    others.add(equals);
    others.add(komma);
    others.add(clear);
    equals.addActionListener(handler3);
    clear. addActionListener(handler1);
     cp=getContentPane();
    gl=new GridLayout(3,2);
    cp.setLayout(gl);

    cp.add(numbers);
    cp.add(prakseis);
    cp.add(others);



    }

    private class ButtonHandler1  implements ActionListener {
   public void actionPerformed(ActionEvent e) {





            //result.setText("Press to copy");
            number.setText( " ");


   }
 }

    private class ButtonHandler2 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if (e.getSource()==one)
                number.setText("1");
            else if (e.getSource()==two)
                 number.setText("2");
            else if (e.getSource()==three)
                 number.setText("3");
             else if (e.getSource()==four)
                 number.setText("4");
              else if (e.getSource()==five)
                 number.setText("5");
              else if (e.getSource()==six)
                 number.setText("6");
               else if (e.getSource()==seven)
                 number.setText("7");
             else if (e.getSource()==eight)
                 number.setText("8");
             else if (e.getSource()==nine)
                  number.setText("9");
             else if (e.getSource()==zero)
                   number.setText("0");

        }


    }

i
     private class ButtonHandler3 implements ActionListener{
        public void actionPerformed(ActionEvent e){
//an einai o 1os arithmos valto sto value1
            if (isFirstNum==true){
             value1=Integer.parseInt(number.getText());
             isFirstNum=false;
//an einai o 2os valsto sto value
            }else{
             value=Integer.parseInt(number.getText());
              isFirstNum=true;
            }
            
             if(e.getSource()==add)
             {

                   result=("" + (value1 + value));
             }
             if(e.getSource()==sub)
            {



                 result=("" + (value1 - value));
             }
             if(e.getSource()==mult)
            {


              result=("" + (value1 * value));
             }
             if(e.getSource()==div)
            {



               result=("" + (value1 / value));

             }

             if(e.getSource()==equals)
            {


                number.setText(result);
             }
            
        }
    }
 } 

o poio swstos tropos gia na ftiakseis ena compouteraki k einai standared tropos einai i xreisimopoihsh stivas dld oso patas arithmous i praksis tha ta prostheteis se mia stiva k molis patithi to = tha diavazeis apo tin stiva k tha kaneis tis prakseis k tha emfanizeis to
px.
[10]
[ + ]
[10]
[ - ]
[20]
molis patithi to = tha diavazeis apo tis stiva k tha ekteleis tis prakseis k tha emfanizeis to apotelesma

eplizw na voithisa :)!
goten002

Απάντηση

Επιστροφή στο “Java”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες