/*2013年5月4日 14:11:24*/ /*Author:蒋宁宁*/
/*Email:linuxjnn@gmail.com*/
/*实现计算器的简单的计算功能*/ /*java语言*/ import java.awt.*;
import java.awt.event.*;
import static java.awt.BorderLayout.*; import javax.swing.*;
import javax.swing.border.*;
public class calculator extends JFrame implements ActionListener { JPanel pane = new JPanel(new GridLayout(4,5,5,5)); JPanel pane2 = new JPanel(new FlowLayout()); JTextField input = new JTextField(30); String [] str = {\"\ Double result; int j=0; JButton [] btn= new JButton[20]; String [] btnItem
{\"1\ public static void main(String args[]){ new calculator(); } calculator(){ init(); setTitle(\"简易计算器\"); setBounds(100,100,400,250); setVisible(true); setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
void init(){ int n=0; for(String i : btnItem){ btn[n] = new JButton(i); btn[n].addActionListener(this); pane.add(btn[n]); n++; } input.setText(\"0.\"); input.setHorizontalAlignment(SwingConstants.RIGHT); input.setPreferredSize(new Dimension(20,20)); pane2.add(input);
add(pane2,\"North\");
=
add(pane,\"Center\"); }
public void actionPerformed(ActionEvent e){ if(e.getSource()==btn[0]){ if(str[1].equals(\"\")){ str[j]=str[j]+1; input.setText(str[j]); }else{ str[j]=str[j]+1; input.setText(str[1]+str[j]); } } else if(e.getSource()==btn[1]){ if(str[1].equals(\"\")){ str[j]=str[j]+2; input.setText(str[j]); }else{ str[j]=str[j]+2; input.setText(str[1]+str[j]); } } else if(e.getSource()==btn[2]){ if(str[1].equals(\"\")){ str[j]=str[j]+3; input.setText(str[j]); }else{ str[j]=str[j]+3; input.setText(str[1]+str[j]); } } else if(e.getSource()==btn[3]){ if(Detection()){ str[5]=\"+\"; str[j+1]=str[j]+\"+\"; input.setText(str[j+1]); j=j+2; } } else if(e.getSource()==btn[4]){ if(Detection()){ str[5]=\"-\"; str[j+1]=str[j]+\"-\"; input.setText(str[j+1]); j=j+2;
} }
else if(e.getSource()==btn[5]){ if(str[1].equals(\"\")){ str[j]=str[j]+4; input.setText(str[j]); }else{ str[j]=str[j]+4; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[6]){ if(str[1].equals(\"\")){ str[j]=str[j]+5; input.setText(str[j]); }else{ str[j]=str[j]+5; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[7]){ if(str[1].equals(\"\")){ str[j]=str[j]+6; input.setText(str[j]); }else{ str[j]=str[j]+6; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[8]){ if(Detection()){ str[5]=\"*\"; str[j+1]=str[j]+\"*\"; input.setText(str[j+1]); j=j+2; } }
else if(e.getSource()==btn[9]){ if(Detection()){ str[5]=\"/\"; str[j+1]=str[j]+\"/\"; input.setText(str[j+1]); j=j+2; }
}
else if(e.getSource()==btn[10]){ if(str[1].equals(\"\")){ str[j]=str[j]+7; input.setText(str[j]); }else{ str[j]=str[j]+7; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[11]){ if(str[1].equals(\"\")){ str[j]=str[j]+8; input.setText(str[j]); }else{ str[j]=str[j]+8; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[12]){ if(str[1].equals(\"\")){ str[j]=str[j]+9; input.setText(str[j]); }else{ str[j]=str[j]+9; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[13]){ if(Detection()){ str[5]=\"sqrt\"; str[j+1]=str[j]+\"^(1/2)\"; input.setText(str[j+1]); j=j+2; } }
else if(e.getSource()==btn[14]){ if(Detection()){ str[5]=\"cbrt\"; str[j+1]=str[j]+\"^(1/3)\"; input.setText(str[j+1]); j=j+2; } }
else if(e.getSource()==btn[15]){ if(str[1].equals(\"\")){ str[j]=str[j]+0; input.setText(str[j]); }else{ str[j]=str[j]+0; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[16]){ if(str[1].equals(\"\")){ str[j]=str[j]+\".\"; input.setText(str[j]); }else{ str[j]=str[j]+\".\"; input.setText(str[1]+str[j]); } }
else if(e.getSource()==btn[17]){ if(Detection()){ str[5]=\"x^2\"; str[j+1]=str[j]+\"^2\"; input.setText(str[j+1]); j=j+2; } }
else if(e.getSource()==btn[18]){ clear(); input.setText(\"0.\"); }
else if(e.getSource()==btn[19]){ if(str[0].equals(\"\")&&str[2].equals(\"\")){
JOptionPane.showMessageDialog(this,\"无可用参数!\ }else{ result(); str[j+1]=str[j-1]+str[j]+\"= \"+result(); input.setText( str[j+1]); clear(); } } } Double result(){ if(str[5].equals(\"+\")){
\警告对话框
result = Double.parseDouble(str[0]) + Double.parseDouble(str[2]); } else if(str[5].equals(\"-\")){ result = Double.parseDouble(str[0]) - Double.parseDouble(str[2]); } else if(str[5].equals(\"*\")){ result = Double.parseDouble(str[0]) * Double.parseDouble(str[2]); } else if(str[5].equals(\"/\")){ result = Double.parseDouble(str[0]) / Double.parseDouble(str[2]); } else if(str[5].equals(\"sqrt\")){ result = Math.sqrt(Double.parseDouble(str[0])); } else if(str[5].equals(\"cbrt\")){ result = Math.cbrt(Double.parseDouble(str[0])); } else if(str[5].equals(\"x^2\")){ result = Double.parseDouble(str[0]) * Double.parseDouble(str[0]); } return result; }
void clear(){ for(int i=0;i<6;i++){ str[i]=\"\"; } j=0; }
boolean Detection(){ if(str[0].equals(\"\")){
JOptionPane.showMessageDialog(this,\"参数X不能为空!\警告对话框\ return false; }else{ return true; } } }