如何在计算器中写小数点?

时间:2017-06-05 14:07:47

标签: java

这是我的计算器。如何正确写入小数点。

    if(e.getSource()==decPoint ){ 
    tf.setText(tf.getText()+"." ); 
        } 

我想我需要在代码中编写if ... else块,这表明我们将point指向文本字段中的数字。如何正确地写这个块? 我很感激你的帮助。

这是我的代码:

 import java.awt.BorderLayout;
 import java.awt.Button;
 import java.awt.Color;
 import java.awt.Frame;
 import java.awt.GridLayout;
 import java.awt.Panel;
 import java.awt.TextField;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class CalcListener implements  WindowListener, ActionListener  {
private Frame f;
private Panel p;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button division;
private Button plus;
private Button minus;
private Button multiply;
private Button decPoint;
private Button equal;
private TextField tf;


double c,n;
String s1,s2,s3,s4,s5;

public CalcListener () {
    f = new Frame("GuiCalculator");
    p = new Panel();
    tf = new TextField ();
    b0 = new Button("0"); b9 = new Button("9");
    b1 = new Button("1"); b2 = new Button("2");
    b3 = new Button("3"); b4 = new Button("4");
    b5 = new Button("5"); b6 = new Button("6");
    b7 = new Button("7"); b8 = new Button("8");
    division = new Button("/"); plus = new Button("+");
    minus = new Button("-"); multiply = new Button("*");
    decPoint = new Button("."); equal = new Button("=");
}

public void launchFrame() {
    f.addWindowListener(this);

    f.add( tf,BorderLayout.NORTH);

    //f.setBackground(Color.blue);

    //p.setBackground(Color.yellow);
    f.add(p,BorderLayout.CENTER);
    p.setLayout (new GridLayout(4,4));
    p.add(b7);  p.add(b8); p.add(b9); p.add(division);
    p.add(b4);  p.add(b5); p.add(b6); p.add(multiply); 
    p.add(b1);  p.add(b2); p.add(b3); p.add(minus);
    p.add(b0);  p.add(decPoint ); p.add(equal); p.add(plus);

    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);
    division.addActionListener(this);
    plus.addActionListener(this);
    minus.addActionListener(this);
    multiply.addActionListener(this);
    decPoint .addActionListener(this);
    equal.addActionListener(this);


    f.setSize(300, 300); 
    f.setVisible(true);
}

public static void main(String[] args) {
    CalcListener calcu = new CalcListener();
    calcu.launchFrame();
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b0)
    {
        s3 = tf.getText();
        s4 = "0";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b1)
    {
        s3 = tf.getText();
        s4 = "1";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b2)
    {
        s3 = tf.getText();
        s4 = "2";
        s5 = s3+s4;
        tf.setText(s5);
    }if(e.getSource()==b3)
    {
        s3 = tf.getText();
        s4 = "3";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b4)
    {
        s3 = tf.getText();
        s4 = "4";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b5)
    {
        s3 = tf.getText();
        s4 = "5";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b6)
    {
        s3 = tf.getText();
        s4 = "6";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b7)
    {
        s3 = tf.getText();
        s4 = "7";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b8)
    {
        s3 = tf.getText();
        s4 = "8";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b9)
    {
        s3 = tf.getText();
        s4 = "9";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==plus)
    {
        s1 = tf.getText();
        tf.setText("");
        c=1;

    }
    if(e.getSource()==minus)
    {
        s1 = tf.getText();
        tf.setText("");
        c=2;

    }
    if(e.getSource()==multiply)
    {
        s1 = tf.getText();
        tf.setText("");
        c=3;

    }
    if(e.getSource()==division)
    {
        s1 = tf.getText();
        tf.setText("");
        c=4;

    }

    if(e.getSource()==equal)
    {
        s2 = tf.getText();
        if(c==1)
        {
            n = Integer.parseInt(s1)+Integer.parseInt(s2);
            tf.setText(String.valueOf(n));
        }
        else
        if(c==2)
        {
            n = Integer.parseInt(s1)-Integer.parseInt(s2);
            tf.setText(String.valueOf(n));
        }
        else
        if(c==3)
        {
            n = Integer.parseInt(s1)*Integer.parseInt(s2);
            tf.setText(String.valueOf(n));
        }
        if(c==4)
        {
            try
            {
                int p=Integer.parseInt(s2);
                if(p!=0)
                {
                                    n = Integer.parseInt(s1)/Integer.parseInt(s2);
                tf.setText(String.valueOf(n));
                 }
                 else
                    tf.setText("infinite");

            }
            catch(Exception i){}
        }

        if(e.getSource()==decPoint ){
            tf.setText(tf.getText()+decPoint );

        }



        /*
        if(e.getSource()==dot){

            s1 = tf.getText();
            if (s1.indexOf('.')==-1){
                tf.setText(".");
                c=5;
            } else{

            }
            tf.setText("");
            c=5;

        }*/
    }

}




 ///////////////////////////////////
@Override
public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub

}
 /////////////////////////////////////
@Override
public void windowClosing(WindowEvent e) {
    System.exit(0);
 /////////////////////////////////////      
}

@Override
public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.DecimalFormat;

public class CalcListener implements  WindowListener, ActionListener  {
private Frame f;
private Panel p;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button division;
private Button plus;
private Button minus;
private Button multiply;
private Button decPoint;
private Button equal;
private TextField tf;


double c,n;
String s1,s2,s3,s4,s5;

public CalcListener () {
    f = new Frame("GuiCalculator");
    p = new Panel();
    tf = new TextField ();
    b0 = new Button("0"); b9 = new Button("9");
    b1 = new Button("1"); b2 = new Button("2");
    b3 = new Button("3"); b4 = new Button("4");
    b5 = new Button("5"); b6 = new Button("6");
    b7 = new Button("7"); b8 = new Button("8");
    division = new Button("/"); plus = new Button("+");
    minus = new Button("-"); multiply = new Button("*");
    decPoint = new Button("."); equal = new Button("=");
}

public void launchFrame() {
    f.addWindowListener(this);

    f.add( tf,BorderLayout.NORTH);

    //f.setBackground(Color.blue);

    //p.setBackground(Color.yellow);
    f.add(p,BorderLayout.CENTER);
    p.setLayout (new GridLayout(4,4));
    p.add(b7);  p.add(b8); p.add(b9); p.add(division);
    p.add(b4);  p.add(b5); p.add(b6); p.add(multiply); 
    p.add(b1);  p.add(b2); p.add(b3); p.add(minus);
    p.add(b0);  p.add(decPoint ); p.add(equal); p.add(plus);

    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);
    division.addActionListener(this);
    plus.addActionListener(this);
    minus.addActionListener(this);
    multiply.addActionListener(this);
    decPoint.addActionListener(this);
    equal.addActionListener(this);


    f.setSize(300, 300); 
    f.setVisible(true);
}

public static void main(String[] args) {
    CalcListener calcu = new CalcListener();
    calcu.launchFrame();
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b0)
    {
        s3 = tf.getText();
        s4 = "0";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b1)
    {
        s3 = tf.getText();
        s4 = "1";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b2)
    {
        s3 = tf.getText();
        s4 = "2";
        s5 = s3+s4;
        tf.setText(s5);
    }if(e.getSource()==b3)
    {
        s3 = tf.getText();
        s4 = "3";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b4)
    {
        s3 = tf.getText();
        s4 = "4";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b5)
    {
        s3 = tf.getText();
        s4 = "5";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b6)
    {
        s3 = tf.getText();
        s4 = "6";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b7)
    {
        s3 = tf.getText();
        s4 = "7";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b8)
    {
        s3 = tf.getText();
        s4 = "8";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==b9)
    {
        s3 = tf.getText();
        s4 = "9";
        s5 = s3+s4;
        tf.setText(s5);
    }
    if(e.getSource()==plus)
    {
        s1 = tf.getText();
        tf.setText("");
        c=1;

    }
    if(e.getSource()==minus)
    {
        s1 = tf.getText();
        tf.setText("");
        c=2;

    }
    if(e.getSource()==multiply)
    {
        s1 = tf.getText();
        tf.setText("");
        c=3;

    }
    if(e.getSource()==division)
    {
        s1 = tf.getText();
        tf.setText("");
        c=4;

    }

    if(e.getSource()==equal)
    {
        s2 = tf.getText();
        if(c==1)
        {
            n = Double.parseDouble(s1)+Double.parseDouble(s2);
            tf.setText(getPreciseString(s1, s2, n));
        }
        else
        if(c==2)
        {
            n = Double.parseDouble(s1)-Double.parseDouble(s2);
            tf.setText(getPreciseString(s1, s2, n));
        }
        else
        if(c==3)
        {
            n = Double.parseDouble(s1)*Double.parseDouble(s2);
            tf.setText(getPreciseString(s1, s2, n));
        }
        if(c==4)
        {
            try
            {
                double p=Double.parseDouble(s2);
                if(p!=0)
                {
                                    n = Double.parseDouble(s1)/Double.parseDouble(s2);
                tf.setText(getPreciseString(s1, s2, n));
                 }
                 else
                    tf.setText("infinite");

            }
            catch(Exception i){}
        }



        /*
        if(e.getSource()==dot){

            s1 = tf.getText();
            if (s1.indexOf('.')==-1){
                tf.setText(".");
                c=5;
            } else{

            }
            tf.setText("");
            c=5;

        }*/
    }
    if(e.getSource()==decPoint && !tf.getText().contains(".")){
        tf.setText(tf.getText()+"." );  
    }

}

private String getPreciseString(String s1, String s2, double n){
    int p1 = s1.indexOf('.') > 0 ? s1.length() - s1.indexOf('.') - 1 : 0;
    int p2 = s2.indexOf('.') > 0 ? s2.length() - s2.indexOf('.') - 1 : 0;
    int precision = p1>p2 ? p1 : p2;
    StringBuilder sb = new StringBuilder("0");
    int i = 1;
    while(i++ < precision){
        sb.append("0");
    }
    return new DecimalFormat("#0."+sb.toString()).format(n);
}





 ///////////////////////////////////
@Override
public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub

}
 /////////////////////////////////////
@Override
public void windowClosing(WindowEvent e) {
    System.exit(0);
 /////////////////////////////////////      
}

@Override
public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

}