这个是我自己做的计算器的源代码,还请大家多多指教
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Count extends JFrame implements ActionListener{
JButton b11;
JTextField tf;
double x,y=0;
int f=0;
boolean flag=false,start=false;
int a=0,b=0,c=0,d=0;
public Count(String title){
setTitle("计算器");
Container p=getContentPane();
p.setLayout(new BorderLayout());
tf=new JTextField();
tf.setEditable(false);tf.setHorizontalAlignment(JTextField.RIGHT);
JButton b0=new JButton(""+0);b0.addActionListener(this);
JButton b1=new JButton(""+1);b1.addActionListener(this);
JButton b2=new JButton(""+2);b2.addActionListener(this);
JButton b3=new JButton(""+3);b3.addActionListener(this);
JButton b4=new JButton(""+4);b4.addActionListener(this);
JButton b5=new JButton(""+5);b5.addActionListener(this);
JButton b6=new JButton(""+6);b6.addActionListener(this);
JButton b7=new JButton(""+7);b7.addActionListener(this);
JButton b8=new JButton(""+8);b8.addActionListener(this);
JButton b9=new JButton(""+9);b9.addActionListener(this);
JButton b10=new JButton("+/-");b10.addActionListener(this);
b11=new JButton("+");b11.addActionListener(this);
JButton b12=new JButton("-");b12.addActionListener(this);
JButton b13=new JButton("*");b13.addActionListener(this);
JButton b14=new JButton("/");b14.addActionListener(this);
JButton b15=new JButton("=");b15.addActionListener(this);
JButton b16=new JButton(".");b16.addActionListener(this);
JButton b17=new JButton("Backspace");b17.addActionListener(this);
JButton b18=new JButton("CE");b18.addActionListener(this);
JButton b19=new JButton("C");b19.addActionListener(this);
JPanel p1=new JPanel(new BorderLayout());
JPanel p2=new JPanel(new GridLayout(4,4));
JPanel p3=new JPanel(new FlowLayout());
p.add(tf,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p1.add(b15,BorderLayout.EAST);
p1.add(p3,BorderLayout.NORTH);
p1.add(p2,BorderLayout.CENTER);
p3.add(b17);
p3.add(b18);
p3.add(b19);
p2.add(b9);
p2.add(b8);
p2.add(b7);
p2.add(b11);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b12);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b13);
p2.add(b0);
p2.add(b16);
p2.add(b10);
p2.add(b14);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
}
public void actionPerformed(ActionEvent e){
String select=e.getActionCommand();
for(int i=0;i<10;i++){String s1=""+i;
if(select.equals(s1))
{if(flag){tf.setText(s1); flag=false;}
else{tf.setText(tf.getText()+select);}}
}
if(select.equals(".")){
tf.setText(tf.getText()+select);}
if(select.equals("C")){
tf.setText("");x=0;y=0;f=0; start=false; }
if(select.equals("CE")){
tf.setText("");}
if(select.equals("+")){
x=Double.parseDouble(tf.getText());flag=true;
if(!start){y=x;start=true;}
else{setResult();}f=1;
}
if(select.equals("-")){
x=Double.parseDouble(tf.getText());flag=true;
if(!start){y=x;start=true;}
else{
setResult();}
f=2;}
if(select.equals("*")){
x=Double.parseDouble(tf.getText());flag=true;
if(!start){y=x;start=true;}
else{setResult();}
f=3;}
if(select.equals("/")){
x=Double.parseDouble(tf.getText());flag=true;
if(!start){y=x;start=true;}
else{setResult();}
f=4;}
if(select.equals("=")){
x=Double.parseDouble(tf.getText());
setResult();start=false;}
if(select.equals("Backspace")){
if(tf.getText().equals(""))return;
else{
StringBuffer sb=new StringBuffer(tf.getText());
sb.deleteCharAt(sb.length()-1);
String s9=new String(sb);
tf.setText(s9);}
}
}
public void setResult(){
if(f==1){y=y+x;tf.setText(y+"");}
if(f==2){y=y-x;tf.setText(y+"");}
if(f==3){y=y*x;tf.setText(y+"");}
if(f==4){y=y/x;tf.setText(y+"");}
}
public static void main(String[] args){
Count f =new Count("计算器");
f.setSize(300,250);
f.setVisible(true);
}
}