import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;public class MyCalculator extends JFrame implements ActionListener{
JPanel jPanel1,jPanel2;
//JTextField ta;
JTextArea ta;
JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,b1,b2,b3,b4,b5,b6;
boolean finish,add,sub,mul,div;
String str;
double num1,num2;
int nequal=0;public MyCalculator(){
setTitle("计算器");
setSize(300,300);
Container c=getContentPane();
c.setLayout(new BorderLayout());
//jPanel1=new JPanel(new GridLayout(1,1));
//jPanel1.setSize(300,50);
jPanel2=new JPanel(new GridLayout(4,4));
// ta=new JTextField("0");
ta=new JTextArea("0",1,20);//前面的是控制行
ta.setFont(new Font("",Font.BOLD,28));
ta.setForeground(Color.gray);
//jPanel1.add(ta);
c.add(ta,BorderLayout.NORTH);
n1=new JButton(" 1 "); n1.addActionListener(this); n1.setFont(new Font("",Font.BOLD,20));
n2=new JButton(" 2 "); n2.addActionListener(this); n2.setFont(new Font("",Font.BOLD,20));
n3=new JButton(" 3 "); n3.addActionListener(this); n3.setFont(new Font("",Font.BOLD,20));
n4=new JButton(" 4 "); n4.addActionListener(this); n4.setFont(new Font("",Font.BOLD,20));
n5=new JButton(" 5 "); n5.addActionListener(this); n5.setFont(new Font("",Font.BOLD,20));
n6=new JButton(" 6 "); n6.addActionListener(this); n6.setFont(new Font("",Font.BOLD,20));
n7=new JButton(" 7 "); n7.addActionListener(this); n7.setFont(new Font("",Font.BOLD,20));
n8=new JButton(" 8 "); n8.addActionListener(this); n8.setFont(new Font("",Font.BOLD,20));
n9=new JButton(" 9 "); n9.addActionListener(this); n9.setFont(new Font("",Font.BOLD,20));
n0=new JButton(" 0 "); n0.addActionListener(this); n0.setFont(new Font("",Font.BOLD,20));
b1=new JButton(" + "); b1.addActionListener(this); b1.setFont(new Font("",Font.BOLD,20));
b2=new JButton(" - "); b2.addActionListener(this); b2.setFont(new Font("",Font.BOLD,20));
b3=new JButton(" × "); b3.addActionListener(this); b3.setFont(new Font("",Font.BOLD,20));
b4=new JButton(" ÷ "); b4.addActionListener(this); b4.setFont(new Font("",Font.BOLD,20));
b5=new JButton(" . "); b5.addActionListener(this); b5.setFont(new Font("",Font.BOLD,20));
b6=new JButton(" = "); b6.addActionListener(this); b6.setFont(new Font("",Font.BOLD,20));
jPanel2.add(n7);
jPanel2.add(n8);
jPanel2.add(n9);
jPanel2.add(b1);
jPanel2.add(n4);
jPanel2.add(n5);
jPanel2.add(n6);
jPanel2.add(b2);
jPanel2.add(n1);
jPanel2.add(n2);
jPanel2.add(n3);
jPanel2.add(b3);
jPanel2.add(n0);
jPanel2.add(b5);
jPanel2.add(b6);
jPanel2.add(b4);
c.add(jPanel2,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void num(int i){
//String s ;
String s=String.valueOf(i);//s的作用是接受用户输入的数字并且把它显示在计算器上
//finish==true表示刚才做完了一次计算 计算器上还有结果 现在要开始下一次计算了 所以先清空计算器
if(finish){
//
ta.setText("0");
finish=false;
}
//没有输入 计算器仍显示0
if((ta.getText()).equals("0")){
ta.setText(s);
}
else{
//输入了 就去掉原先就有的0
str = ta.getText() + s;
ta.setText(str);
}
}
//选择所要进行的计算
public void sign(int s){
//一旦选择计算方式 先保存num1 准备输入第二个数 计算器上显示刚才输入的数字
num1=Double.parseDouble(ta.getText());
ta.setText(String.valueOf(num1));
if(s==1){
//这四个必须要重新赋值 比如做乘法 add如果还是真 在下面就会调用add方法了
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
finish=true;
} public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==n1)
num(1);
else if(e.getSource()==n2)
num(2);
else if(e.getSource()==n3)
num(3);
else if(e.getSource()==n4)
num(4);
else if(e.getSource()==n5)
num(5);
else if(e.getSource()==n6)
num(6);
else if(e.getSource()==n7)
num(7);
else if(e.getSource()==n8)
num(8);
else if(e.getSource()==n9)
num(9);
else if(e.getSource()==n0)
num(0);
//符号事件
else if(e.getSource()==b1){
//ta.setText("0");
sign(1);
}
else if(e.getSource()==b2){
//ta.setText("0");
sign(2);
}
else if(e.getSource()==b3){
//ta.setText("0");
sign(3);
}
else if(e.getSource()==b4){
sign(4);
}
//小数点 只有在没有小数点的时候才能够在输入小数点
else if(e.getSource()==b5){
str=ta.getText();
if(str.indexOf(".")==-1){//没有小数点才返回-1
str+=".";
//ta.setText(str);
}
if(str.indexOf(".")==0){
str+="0.";
}
ta.setText(str);
}
//等号事件
else if(e.getSource()==b6){
if(nequal==0){
num2=Double.parseDouble(ta.getText());//num2就是点等号前计算器上的数字
nequal++;
}
if(add){//只有当调用add后add才是真 由于没有调用别的方法 add一直是真 就实现了连等 但是这个连等总是在翻倍 先去掉它
num1=num1 + num2;
//add=false;
}//将最后的结果作为下次计算起始数字 这样实现的了连续操作+-*/
else if(sub){
num1=num1 - num2;
///sub=false;
}
else if(mul){
num1=num1 * num2;
//mul=false;
}
else if(div){
num1=num1 / num2;
//div=false;
}
ta.setText(String.valueOf(num1));//num1是double 转换成String才显示
finish=true;
}
}public static void main(String[] args){
new MyCalculator();
}
}
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;public class MyCalculator extends JFrame implements ActionListener{
JPanel jPanel1,jPanel2;
//JTextField ta;
JTextArea ta;
JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,b1,b2,b3,b4,b5,b6;
boolean finish,add,sub,mul,div;
String str;
double num1,num2;
int nequal=0;public MyCalculator(){
setTitle("计算器");
setSize(300,300);
Container c=getContentPane();
c.setLayout(new BorderLayout());
//jPanel1=new JPanel(new GridLayout(1,1));
//jPanel1.setSize(300,50);
jPanel2=new JPanel(new GridLayout(4,4));
// ta=new JTextField("0");
ta=new JTextArea("0",1,20);//前面的是控制行
ta.setFont(new Font("",Font.BOLD,28));
ta.setForeground(Color.gray);
//jPanel1.add(ta);
c.add(ta,BorderLayout.NORTH);
n1=new JButton(" 1 "); n1.addActionListener(this); n1.setFont(new Font("",Font.BOLD,20));
n2=new JButton(" 2 "); n2.addActionListener(this); n2.setFont(new Font("",Font.BOLD,20));
n3=new JButton(" 3 "); n3.addActionListener(this); n3.setFont(new Font("",Font.BOLD,20));
n4=new JButton(" 4 "); n4.addActionListener(this); n4.setFont(new Font("",Font.BOLD,20));
n5=new JButton(" 5 "); n5.addActionListener(this); n5.setFont(new Font("",Font.BOLD,20));
n6=new JButton(" 6 "); n6.addActionListener(this); n6.setFont(new Font("",Font.BOLD,20));
n7=new JButton(" 7 "); n7.addActionListener(this); n7.setFont(new Font("",Font.BOLD,20));
n8=new JButton(" 8 "); n8.addActionListener(this); n8.setFont(new Font("",Font.BOLD,20));
n9=new JButton(" 9 "); n9.addActionListener(this); n9.setFont(new Font("",Font.BOLD,20));
n0=new JButton(" 0 "); n0.addActionListener(this); n0.setFont(new Font("",Font.BOLD,20));
b1=new JButton(" + "); b1.addActionListener(this); b1.setFont(new Font("",Font.BOLD,20));
b2=new JButton(" - "); b2.addActionListener(this); b2.setFont(new Font("",Font.BOLD,20));
b3=new JButton(" × "); b3.addActionListener(this); b3.setFont(new Font("",Font.BOLD,20));
b4=new JButton(" ÷ "); b4.addActionListener(this); b4.setFont(new Font("",Font.BOLD,20));
b5=new JButton(" . "); b5.addActionListener(this); b5.setFont(new Font("",Font.BOLD,20));
b6=new JButton(" = "); b6.addActionListener(this); b6.setFont(new Font("",Font.BOLD,20));
jPanel2.add(n7);
jPanel2.add(n8);
jPanel2.add(n9);
jPanel2.add(b1);
jPanel2.add(n4);
jPanel2.add(n5);
jPanel2.add(n6);
jPanel2.add(b2);
jPanel2.add(n1);
jPanel2.add(n2);
jPanel2.add(n3);
jPanel2.add(b3);
jPanel2.add(n0);
jPanel2.add(b5);
jPanel2.add(b6);
jPanel2.add(b4);
c.add(jPanel2,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void num(int i){
//String s ;
String s=String.valueOf(i);//s的作用是接受用户输入的数字并且把它显示在计算器上
//finish==true表示刚才做完了一次计算 计算器上还有结果 现在要开始下一次计算了 所以先清空计算器
if(finish){
//
ta.setText("0");
finish=false;
}
//没有输入 计算器仍显示0
if((ta.getText()).equals("0")){
ta.setText(s);
}
else{
//输入了 就去掉原先就有的0
str = ta.getText() + s;
ta.setText(str);
}
}
//选择所要进行的计算
public void sign(int s){
//一旦选择计算方式 先保存num1 准备输入第二个数 计算器上显示刚才输入的数字
num1=Double.parseDouble(ta.getText());
ta.setText(String.valueOf(num1));
if(s==1){
//这四个必须要重新赋值 比如做乘法 add如果还是真 在下面就会调用add方法了
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
finish=true;
} public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==n1)
num(1);
else if(e.getSource()==n2)
num(2);
else if(e.getSource()==n3)
num(3);
else if(e.getSource()==n4)
num(4);
else if(e.getSource()==n5)
num(5);
else if(e.getSource()==n6)
num(6);
else if(e.getSource()==n7)
num(7);
else if(e.getSource()==n8)
num(8);
else if(e.getSource()==n9)
num(9);
else if(e.getSource()==n0)
num(0);
//符号事件
else if(e.getSource()==b1){
//ta.setText("0");
sign(1);
}
else if(e.getSource()==b2){
//ta.setText("0");
sign(2);
}
else if(e.getSource()==b3){
//ta.setText("0");
sign(3);
}
else if(e.getSource()==b4){
sign(4);
}
//小数点 只有在没有小数点的时候才能够在输入小数点
else if(e.getSource()==b5){
str=ta.getText();
if(str.indexOf(".")==-1){//没有小数点才返回-1
str+=".";
//ta.setText(str);
}
if(str.indexOf(".")==0){
str+="0.";
}
ta.setText(str);
}
//等号事件
else if(e.getSource()==b6){
if(nequal==0){
num2=Double.parseDouble(ta.getText());//num2就是点等号前计算器上的数字
nequal++;
}
if(add){//只有当调用add后add才是真 由于没有调用别的方法 add一直是真 就实现了连等 但是这个连等总是在翻倍 先去掉它
num1=num1 + num2;
//add=false;
}//将最后的结果作为下次计算起始数字 这样实现的了连续操作+-*/
else if(sub){
num1=num1 - num2;
///sub=false;
}
else if(mul){
num1=num1 * num2;
//mul=false;
}
else if(div){
num1=num1 / num2;
//div=false;
}
ta.setText(String.valueOf(num1));//num1是double 转换成String才显示
finish=true;
}
}public static void main(String[] args){
new MyCalculator();
}
}
用BigDecimal
例如System.out.println(new BigDecimal("0.2").multiply(new BigDecimal("3.0")));
结果就是6.0
改一下你的就是
num1=new BigDecimal(num1+"").add(new BigDecimal(num2+"")).doubleValue() ;
//add=false;
}//将最后的结果作为下次计算起始数字 这样实现的了连续操作+-*/
else if(sub){
num1=new BigDecimal(num1+"").subtract(new BigDecimal(num2+"")).doubleValue() ;
///sub=false;
}
else if(mul){
num1=new BigDecimal(num1+"").multiply(new BigDecimal(num2+"")).doubleValue() ;
//mul=false;
}
else if(div){
num1=new BigDecimal(num1+"").divide(new BigDecimal(num2+"")).doubleValue() ;
//div=false;
}
不过你的程序还是有逻辑问题,运行的时候有好多数算的都不对