| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 556 人关注过本帖
标题:我终于做出了一个计算器! 但有欠缺,希望得到帮助!!
只看楼主 加入收藏
250697812
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2005-10-5
收藏
 问题点数:0 回复次数:4 
我终于做出了一个计算器! 但有欠缺,希望得到帮助!!

怎么能让它一直输入0的时候 只显示一个0 比如:“00000”=“0”

还有小数点"." 比如:“..........”=“.”


不要见笑啊!!!!
import java.awt.*;
import java.awt.event.*;

class LL extends Frame implements ActionListener{
private Button b0=new Button("0");
private Button b1=new Button("1");
private Button b2=new Button("2");
private Button b3=new Button("3");
private Button b4=new Button("4");
private Button b5=new Button("5");
private Button b6=new Button("6");
private Button b7=new Button("7");
private Button b8=new Button("8");
private Button b9=new Button("9");
private Button b=new Button(".");
private Button c1=new Button("+");
private Button c2=new Button("-");
private Button c3=new Button("*");
private Button c4=new Button("/");
private Button c5=new Button("=");
private Button bb=new Button("清除");
private TextField t1=new TextField(20);
float a=0,a4=0,a1=0,a3=0;
int a2=0,a9=0;



public LL() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});

Panel p1=new Panel();
p1.setLayout(new GridLayout(4,4));
p1.add(b7);p1.add(b8);p1.add(b9);p1.add(c1);
p1.add(b4);p1.add(b5);p1.add(b6);p1.add(c2);
p1.add(b1);p1.add(b2);p1.add(b3);p1.add(c3);
p1.add(b0);p1.add(c4);p1.add(c5);p1.add(b);

b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
c5.addActionListener(this);
b.addActionListener(this);
bb.addActionListener(this);


Panel p2=new Panel();
p2.add(t1);


Panel p3=new Panel();
p3.add(bb);

setLayout(new BorderLayout());
add("North",p2);
add("Center",p1);
add("South",bb);



t1.addKeyListener(new ff());

pack();show();
}

public static void main(String args[])
{
LL l=new LL();
l.setSize(300,300);
l.setVisible(true);
}

class ff implements KeyListener
{

public void keyPressed(KeyEvent ke)
{
/*if(ke.getKeyChar()=='0')
{
if(t1.getText().equals("0"))
{}
else
{
t1.setText(t1.getText()+("0"));
}


}
if(ke.getKeyChar()=='1')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"1");
}
else
{
t1.setText(t1.getText()+"1");
}
}
if(ke.getKeyChar()=='2')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"2");
}
else
{
t1.setText(t1.getText()+"2");
}
}
if(ke.getKeyChar()=='3')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"3");
}
else
{
t1.setText(t1.getText()+"3");
}
}
if(ke.getKeyChar()=='4')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"4");
}
else
{
t1.setText(t1.getText()+"4");
}
}
if(ke.getKeyChar()=='5')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"5");
}
else
{
t1.setText(t1.getText()+"5");
}
}
if(ke.getKeyChar()=='6')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"6");
}
else
{
t1.setText(t1.getText()+"6");
}
}
if(ke.getKeyChar()=='7')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"7");
}
else
{
t1.setText(t1.getText()+"7");
}
}
if(ke.getKeyChar()=='8')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"8");
}
else
{
t1.setText(t1.getText()+"8");
}
}
if(ke.getKeyChar()=='9')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"9");
}
else
{
t1.setText(t1.getText()+"9");
}
}*/
if(ke.getKeyChar()=='+')
{

a1=Float.parseFloat(t1.getText());
a9=1;
t1.setText("");
}
if(ke.getKeyChar()=='-')
{
a1=Float.parseFloat(t1.getText());
a9=2;
t1.setText("");
}
if(ke.getKeyChar()=='*')
{
a1=Float.parseFloat(t1.getText());
a9=3;
t1.setText("");
}
if(ke.getKeyChar()=='/')
{
a1=Float.parseFloat(t1.getText());
a9=4;
t1.setText("");
}

/*if(ke.getKeyChar()=='.')
{
if(t1.getText()==".")
{}
else
t1.setText(t1.getText()+".");
}*/

if(ke.getKeyChar()=='=')
{
switch (a9)
{
case 1: a3=a1+Float.parseFloat(t1.getText()); t1.setText(""+a3);break;
case 2: a3=a1-Float.parseFloat(t1.getText()); t1.setText(""+a3);break;
case 3: a3=a1*Float.parseFloat(t1.getText()); t1.setText(""+a3);break;
case 4: a3=a1/Float.parseFloat(t1.getText()); t1.setText(""+a3);break;
}
}

}
public void keyReleased(KeyEvent ke)
{
if(ke.getKeyChar()=='+')
{
t1.setText("");
}
if(ke.getKeyChar()=='-')
{
t1.setText("");
}
if(ke.getKeyChar()=='*')
{
t1.setText("");
}
if(ke.getKeyChar()=='/')
{
t1.setText("");
}

if(ke.getKeyChar()=='=')
{
switch (a9)
{
case 1: t1.setText(""+a3);break;
case 2: t1.setText(""+a3);break;
case 3: t1.setText(""+a3);break;
case 4: t1.setText(""+a3);break;
}
}
}
public void keyTyped(KeyEvent ke)
{

}
}






public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
if(t1.getText().equals("0"))
{}
else
{
t1.setText(t1.getText()+("0"));
}


}
if(e.getSource()==b1)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"1");
}
else
{
t1.setText(t1.getText()+"1");
}
}
if(e.getSource()==b2)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"2");
}
else
{
t1.setText(t1.getText()+"2");
}
}
if(e.getSource()==b3)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"3");
}
else
{
t1.setText(t1.getText()+"3");
}
}
if(e.getSource()==b4)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"4");
}
else
{
t1.setText(t1.getText()+"4");
}
}
if(e.getSource()==b5)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"5");
}
else
{
t1.setText(t1.getText()+"5");
}
}
if(e.getSource()==b6)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"6");
}
else
{
t1.setText(t1.getText()+"6");
}
}
if(e.getSource()==b7)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"7");
}
else
{
t1.setText(t1.getText()+"7");
}
}
if(e.getSource()==b8)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"8");
}
else
{
t1.setText(t1.getText()+"8");
}
}
if(e.getSource()==b9)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"9");
}
else
{
t1.setText(t1.getText()+"9");
}
}
if(e.getSource()==b)
{
if(t1.getText().length()==0)
{}
else
t1.setText(t1.getText()+".");
}
if(e.getSource()==bb)
{
t1.setText("");
a=0;a1=0;
a2=0;a3=0;
}
if(e.getSource()==c1)
{
a1=Float.parseFloat(t1.getText());
a2=1;
t1.setText("");
}
if(e.getSource()==c2)
{
a1=Float.parseFloat(t1.getText());
a2=2;
t1.setText("");
}
if(e.getSource()==c3)
{
a1=Float.parseFloat(t1.getText());
a2=3;
t1.setText("");
}
if(e.getSource()==c4)
{
a1=Float.parseFloat(t1.getText());
a2=4;
t1.setText("");
}
if(e.getSource()==c5)
{
switch (a2)
{
case 1: a3=a1+Float.parseFloat(t1.getText()); t1.setText(""+a3);break;
case 2: a3=a1-Float.parseFloat(t1.getText());; t1.setText(""+a3);break;
case 3: a3=a1*Float.parseFloat(t1.getText());; t1.setText(""+a3);break;
case 4: a3=a1/Float.parseFloat(t1.getText());; t1.setText(""+a3);break;
}
}

}


}

搜索更多相关主题的帖子: 计算器 
2006-05-20 14:43
250697812
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2005-10-5
收藏
得分:0 
我这多亏了斑竹的支持和帮助啊    非常感谢斑竹!!

2006-05-20 14:47
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
收藏
得分:0 
太复杂了 好多地方可以改进 在琢磨...

------Java 爱好者,论坛小混混,学习中------
2006-05-20 20:02
vigourpan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-11-30
收藏
得分:0 
恭喜
不过我这还有一个更好的
呵呵@-@!
2006-05-21 00:58
250697812
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2005-10-5
收藏
得分:0 

是吧!!!
那你发过来啊!!!!


2006-05-21 16:30
快速回复:我终于做出了一个计算器! 但有欠缺,希望得到帮助!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026062 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved