当前位置: 代码迷 >> Java相关 >> [原创]自己做的一个建议计算器,太粗糙了,贴出来大家赐教
  详细解决方案

[原创]自己做的一个建议计算器,太粗糙了,贴出来大家赐教

热度:177   发布时间:2006-04-22 13:18:00.0
我最近刚做好了一个计算器 等会把代码发上来你看看
----------------解决方案--------------------------------------------------------
用jar打包类文件
----------------解决方案--------------------------------------------------------

这个是我自己做的计算器的源代码,还请大家多多指教
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);
}
}


----------------解决方案--------------------------------------------------------
楼上的做的很不错
其实我那个很粗糙 思路和你差不多

----------------解决方案--------------------------------------------------------
编的真不错
----------------解决方案--------------------------------------------------------

都不错 都挺棒的


----------------解决方案--------------------------------------------------------

不看结果,单看程序的话还是不错的
如果以windows自带的计算器为目标的话,最起码的就是让显示的数字以右边开始显示
以后多多改进吧~


----------------解决方案--------------------------------------------------------
飘飘叶子 终于在java版露面了
表示热烈欢迎
感谢提出宝贵意见

----------------解决方案--------------------------------------------------------
快毕业了,赶工程比较忙……都没时间上来了~
而且现在的版主都很厉害,所以我着重去J2EE版了~
----------------解决方案--------------------------------------------------------
你可是元老啊
----------------解决方案--------------------------------------------------------
  相关解决方案