怎么能让它一直输入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;
}
}
}
}
----------------解决方案--------------------------------------------------------
我这多亏了斑竹的支持和帮助啊 非常感谢斑竹!!
----------------解决方案--------------------------------------------------------
太复杂了 好多地方可以改进 在琢磨...
----------------解决方案--------------------------------------------------------
恭喜
不过我这还有一个更好的
呵呵@-@!
----------------解决方案--------------------------------------------------------
是吧!!!
那你发过来啊!!!!
----------------解决方案--------------------------------------------------------