import java.awt.*;
import java.awt.event.*;
public class Com extends Frame implements ActionListener,KeyListener{
String[] str=new String[]{"7","8","9","-","4","5","6","+","1","2","3","*","0",".","=","/"};
Panel panel=new Panel();
BorderLayout bl=new BorderLayout();
GridLayout gl=new GridLayout(4,4);
Button[] button=new Button[16];
Label tf=new Label();
Button bb=new Button("C");
String opr="";
String temp="";
StringBuffer sb=new StringBuffer();
int count=-1;
Double res=0.0;
public Com(String Name){
super(Name);
addWindowListener(new win());
setLayout(bl);
panel.setLayout(gl);
for(int i=0;i<str.length;i++){
button[i]=new Button(str[i]);
panel.add(button[i]);
button[i].addActionListener(this);
}
add(tf,BorderLayout.NORTH);
add(bb,BorderLayout.SOUTH);
add(panel);
bb.addActionListener(this);//点击事件的添加
tf.addKeyListener(this);//键盘事件的添加
setSize(200,170);
setVisible(true);
}
//键盘事件,问题在这里
public void keyPressed(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
public void keyTyped(KeyEvent e){
if(String.valueOf(e.getKeyChar()).equals("+")||String.valueOf(e.getKeyChar()).equals("-")||String.valueOf(e.getKeyChar()).equals("*")||String.valueOf(e.getKeyChar()).equals("/")||String.valueOf(e.getKeyChar()).equals("=")){
count++;
sb.append(String.valueOf(e.getKeyChar()));
temp=opr;
opr="";
if(count>0){
if(sb.charAt(count-1)=='+')
res=res+Double.parseDouble(temp);
else if(sb.charAt(count-1)=='*')
res=res*Double.parseDouble(temp);
else if(sb.charAt(count-1)=='-')
res=res-Double.parseDouble(temp);
else if(sb.charAt(count-1)=='/')
res=res/Double.parseDouble(temp);
else if(sb.charAt(count)=='=')
System.out.print(res);
tf.setText(res.toString());
}
if(count==0){
res=Double.parseDouble(temp);
tf.setText(res.toString());
}
}
else{
opr=opr+String.valueOf(e.getKeyChar());
tf.setText(opr);
}
}
//点击事件,可以显示
public void actionPerformed(ActionEvent e){
Button bt=(Button)(e.getSource());
if(bt==bb){
res=0.0;
sb=new StringBuffer("");
opr="";
temp="";
count=-1;
tf.setText(res.toString());
}
else{
if(bt.getLabel().equals("+")||bt.getLabel().equals("*")||bt.getLabel().equals("-")||bt.getLabel().equals("/")||bt.getLabel().equals("=")){
count++;
sb.append(bt.getLabel());
temp=opr;
opr="";
if(count>0){
if(sb.charAt(count-1)=='+')
res=res+Double.parseDouble(temp);
else if(sb.charAt(count-1)=='*')
res=res*Double.parseDouble(temp);
else if(sb.charAt(count-1)=='-')
res=res-Double.parseDouble(temp);
else if(sb.charAt(count-1)=='/')
res=res/Double.parseDouble(temp);
else if(sb.charAt(count)=='=')
System.out.print(res);
tf.setText(res.toString());
}
if(count==0){
res=Double.parseDouble(temp);
tf.setText(res.toString());
}
}
else{
opr=opr+bt.getLabel();
tf.setText(opr);
}
}
}
public static void main(String args[]){
Com com=new Com("我的计算器");
}
}
class win implements WindowListener{
public void windowActivated(WindowEvent e)
{}
public void windowClosed(WindowEvent e)
{}
public void windowClosing(WindowEvent e)
{System.exit(0);}
public void windowDeactivated(WindowEvent e)
{}
public void windowDeiconified(WindowEvent e)
{}
public void windowIconified(WindowEvent e)
{}
public void windowOpened(WindowEvent e)
{}
}