java计算器小程序.doc
Myeclipse环境下Java语言编写的计算器小程序 源代码 /程序的主要部分package pkg_1;import java.awt.*;import java.awt.Button;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 cacalator extends Frame implements ActionListener, WindowListener String button_name = "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", "=", "清除", "/" ;/按钮上的字符串Button but;/按钮数组TextField text_1;/一个文本框 用于记录数入的字符串Panel pan_1, pan_2;/两个面板double n_1 = 0, n_2 = 0;/记录输入的数值char c = '1'/记录输入的符号 static int n=0;/记录数入的符号的数目static String s_1="",s_3="" static String s_2 =""/记录输入符号前输入的字符串 cacalator()super("计算器");/调用父类的构造函数setSize(300,300);/设置大小setLayout(new BorderLayout(10,10);/布局but=new Buttonbutton_name.length;/实例化 创建按钮数组text_1=new TextField("0");/实例化并出示话为0pan_1=new Panel();/实例化pan_2=new Panel();/实例化pan_1.setLayout(new GridLayout(1,1);/设置面板1为2行1列pan_1.add(text_1);/将文本框添加到面板1中add(pan_1,BorderLayout.NORTH);/将面板1放到窗体的上部pan_2.setLayout(new GridLayout(4,4);/设置面板2为4行4列for(int i=0;i<button_name.length;i+)buti=new Button(button_namei);/写入按钮上的字pan_2.add(buti);/按钮添加到面板2上buti.addActionListener(this);/加上监听add(pan_2,BorderLayout.CENTER);/将面板2放到窗体的中间addWindowListener(this);/窗体加上监听setVisible(true);/设置可见public void actionPerformed(ActionEvent arg0) String s_0, s_1;/定义字符创用以存储读出的东西s_0 = arg0.getActionCommand();/读到单击按钮时 按钮上的字符串 相当于记录命令s_1 = text_1.getText();/读出文本框1的内容if(s_0.equals("1") | s_0.equals("2") | s_0.equals("3") |s_0.equals("4") |s_0.equals("5") |s_0.equals("6") |s_0.equals("7") |s_0.equals("8") |s_0.equals("9") |s_0.equals("0")if(n%2=0)s_2=s_2+s_0;text_1.setText(s_2);/如果这个时候没有输入 运算符 则直接将单击的东西连接在s_2上 并显示在s_2上n_1=(Float.valueOf(s_2).floatValue();/在没有输入运算符的时候将数值记录在n_1上if(n%2=1)s_3=s_3+s_0;text_1.setText(s_3);/如果这个时候输入了运算符 则直接将单击的东西连接在s_2上 并显示在s_2上n_2=(Float.valueOf(s_3).floatValue();/在输入了运算符的时候将数值记录在n_2上if(s_0.equals("+")n=n+1;c='+'/text_1.setText(""); if(s_0.equals("-")n=n+1;c='-'/text_1.setText("");if(s_0.equals("*")n=n+1;c='*'/text_1.setText("");if(s_0.equals("/")n=n+1;c='/'/text_1.setText("");if(s_0.equals("=")n=n+1;s_3=""if(c='+')n_1=n_1+n_2;s_1=Double.toString(n_1);if(c='-')n_1=n_1-n_2;s_1=Double.toString(n_1);if(c='*')n_1=n_1*n_2;s_1=Double.toString(n_1);if(c='/')n_1=n_1/n_2;s_1=Double.toString(n_1);text_1.setText(s_1); if(s_0.equals("清除")c='1'n_1=0;n_2=0;s_1=""s_2=""text_1.setText("");public void windowActivated(WindowEvent arg0) / TODO Auto-generated method stubpublic void windowClosed(WindowEvent arg0) public void windowClosing(WindowEvent arg0) Window w=(Window)arg0.getSource();setVisible(false);w.dispose();public void windowDeactivated(WindowEvent arg0) / TODO Auto-generated method stubpublic void windowDeiconified(WindowEvent arg0) / TODO Auto-generated method stubpublic void windowIconified(WindowEvent arg0) / TODO Auto-generated method stubpublic void windowOpened(WindowEvent arg0) / TODO Auto-generated method stub/这是主函数部分package pkg_1;import javax.swing.JOptionPane;public class cacalator_1 public static void main(String args) cacalator cac_1=new cacalator();