标签:
package xjj; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.math.BigDecimal; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class Calculator extends JFrame implements ActionListener{ private JTextField text; private BigDecimal bigDecimal_a,bigDecimal_b,bigDecimal_result; private boolean isFirst=true,isPoint=true,isEqual=false; private String str; private int operator; private JButton[] jbtns=new JButton[21]; private String[] buttonName={"", "±","C","←","÷", "7","8","9","×", "4","5","6","+", "1","2","3","-" , "0",".","=",""}; public Calculator(){ super("计算器"); Dimension dim=this.getToolkit().getScreenSize(); this.getContentPane().setLayout(null); //this.getContentPane().setBackground(Color.CYAN); this.setBounds(dim.width*2/5, dim.height/4, dim.width/4, dim.height/2); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); text=new JTextField(20); text.setBounds(new Rectangle(5, 3, 325, 40)); text.setHorizontalAlignment(JTextField.RIGHT); text.setBackground(Color.CYAN); this.getContentPane().add(text); int count=1; for(int i=1;i<20;i++){ jbtns[i]=new JButton(); jbtns[i] = new JButton(buttonName[i]); jbtns[i].addActionListener(this); this.getContentPane().add(jbtns[i]); int k=i%4; if(i%4==0){ k=4; jbtns[i].setBounds(5+(k-1)*85, 60*count, 70, 42); count++; }else{ jbtns[i].setBounds(5+(k-1)*85, 60*count, 70, 42); } } jbtns[19].setBounds(5+2*85, 300, 155, 42); text.setFont(new Font("xjj",Font.BOLD,20)); this.setVisible(true); } public static void main(String[] args) { new Calculator(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==jbtns[19]){ str=text.getText(); if(!haveSign(str) && !isEqual){ return ; } int i=calculator(); if(i==0){ return ; } isEqual=true; isFirst=true; text.setText(""+bigDecimal_result); } if(e.getSource()==jbtns[18] && isPoint ){ isAddPoint(); String s=str.substring(0,str.length()-1); if(havePoint(s)){ return ; } isEqual=false; isPoint=false; text.setText(str); } if(e.getSource()==jbtns[1]){ str=text.getText(); if(str.length()==0){ str="-"; text.setText(str); return ; } if(str.charAt(str.length()-1)=='-'||str.charAt(str.length()-1)=='+'||str.charAt(str.length()-1)=='×'||str.charAt(str.length()-1)=='÷'){ str=str+"-"; text.setText(str); return ; } } if(e.getSource()==jbtns[3]&& !isEqual){ str=text.getText(); if(str.length()==0){ return ; } if(str.length()==1){ str=""; } if(str.length()>=2){ if(str.charAt(str.length()-1)=='÷' || str.charAt(str.length()-1)=='×' ||str.charAt(str.length()-1)=='+' ||str.charAt(str.length()-1)=='-'){ isFirst=true; } str=str.substring(0, str.length()-1); if(havePoint(str)){ isPoint=false; }else{ isPoint=true; } } text.setText(str); return ; } if(e.getSource()==jbtns[2]){ str=""; isFirst=true;isPoint=true;isEqual=false; text.setText(str); return ; } JButton jbtn=(JButton)e.getSource(); if(jbtn.getText().compareTo("0")>=0 && jbtn.getText().compareTo("9") <=0){ str=text.getText()+jbtn.getText(); isEqual=false; text.setText(str); } for(int i=4;i<=16;i+=4){ if(e.getSource()==jbtns[i]&&isFirst){ str=text.getText(); if(str.length()==0){ return ; } isEqual=false; operator=i; str=text.getText()+jbtns[i].getText(); isFirst=false; isPoint=true; text.setText(str); } } } private int calculator() { str=text.getText(); if(operator==4){ String[] string=str.split("÷"); if(string.length==1){ bigDecimal_result=bigDecimal_result.divide(bigDecimal_b,5,BigDecimal.ROUND_HALF_UP); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); return 1; } bigDecimal_a=new BigDecimal(string[0]); bigDecimal_b=new BigDecimal(string[1]); if(bigDecimal_b.compareTo(BigDecimal.ZERO)==0){ return 0; } bigDecimal_result=bigDecimal_a.divide(bigDecimal_b,20,BigDecimal.ROUND_HALF_UP); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); } if(operator==8){ String[] string=str.split("×"); if(string.length==1){ bigDecimal_result=bigDecimal_result.multiply(bigDecimal_b); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); return 1; } bigDecimal_a=new BigDecimal(string[0]); bigDecimal_b=new BigDecimal(string[1]); bigDecimal_result=bigDecimal_a.multiply(bigDecimal_b); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); } if(operator==12){ String[] string=str.split("+"); if(string.length==1){ bigDecimal_result=bigDecimal_result.add(bigDecimal_b); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); return 1; } bigDecimal_a=new BigDecimal(string[0]); bigDecimal_b=new BigDecimal(string[1]); bigDecimal_result=bigDecimal_a.add(bigDecimal_b); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); } if(operator==16){ String[] string=str.split("-"); if(string.length==1){ bigDecimal_result=bigDecimal_result.subtract(bigDecimal_b); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); return 1; } bigDecimal_a=new BigDecimal(string[0]); bigDecimal_b=new BigDecimal(string[1]); bigDecimal_result=bigDecimal_a.subtract(bigDecimal_b); bigDecimal_result=bigDecimal_result.stripTrailingZeros(); } return 1; } public void isAddPoint(){ str=text.getText(); if(str.length()==0){ str="0."; return ; } if(str.charAt(str.length()-1)>'9'||str.charAt(str.length()-1)<'0'){ str=str+"0."; return ; } str=text.getText()+"."; } public boolean haveSign(String s){ for(int i=0;i<s.length();i++){ if(s.charAt(i)=='+' || s.charAt(i)=='-' ||s.charAt(i)=='÷' ||s.charAt(i)=='×'){ return true; } } return false; } public boolean havePoint(String s){ int count=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='.' ){ count++; } } if(count==1 && haveSign(s)){ return false; } if(count==0 ){ return false; } return true; } }
标签:
原文地址:http://blog.csdn.net/u011479875/article/details/45627137