怎么实现响应键盘事件啊??
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;
public class Calculate extends JFrame{
private JPanel p1;
private JButton Button[]=new JButton[16],signButton,clearButton,deleteButton;
private JTextField outputField;
private Container contentPane;
private boolean firstInput=true;
private Calculatecommon model=new Calculatecommon();
public Calculate(){
p1=new JPanel();
Button[0]=new JButton("1");
Button[1]=new JButton("2");
Button[2]=new JButton("3");
Button[3]=new JButton("/");
Button[4]=new JButton("4");
Button[5]=new JButton("5");
Button[6]=new JButton("6");
Button[7]=new JButton("*");
Button[8]=new JButton("7");
Button[9]=new JButton("8");
Button[10]=new JButton("9");
Button[11]=new JButton("-");
Button[12]=new JButton("0");
Button[13]=new JButton(".");
Button[14]=new JButton("=");
Button[15]=new JButton("+");
clearButton=new JButton("clear");
signButton=new JButton("+/-");
deleteButton=new JButton("退格");
contentPane=this.getContentPane();
outputField=new JTextField(3);
outputField.setText("0");
outputField.setEditable(true);
this.setSize(310,250);
p1.setLayout(new java.awt.GridLayout(4,5));
for(int i=0;i<Button.length;i++){
if(i==4)p1.add(deleteButton);
if(i==8)p1.add(clearButton);
if(i==12)p1.add(signButton);
Button[i].setFont(new java.awt.Font("Dialog",1,16));
p1.add(Button[i]);
}
contentPane.add(outputField,java.awt.BorderLayout.NORTH);
contentPane.add(p1,java.awt.BorderLayout.CENTER);
this.setVisible(true);
}
public void registEvent(){
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}
});
for(int i=0;i<Button.length;i++){
Button[i].addActionListener(new ButtonHandler());
}
signButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
outputField.setText(String.valueOf(model.toSign(Double.parseDouble(outputField.getText()))));
}
});
deleteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
outputField.setText(model.backSpace(outputField.getText()));
if(outputField.getText().equals("0"))firstInput=true;
}
});
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
firstInput=true;
outputField.setText("0");
model.reset();
}
});
}
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ae){
char actionCommand=ae.getActionCommand().charAt(0);
switch(actionCommand){
case '1':case '2':case '3':case '4':case '5': case '6':case '7':case '8':case '9':case '0':case '.':
if(firstInput){outputField.setText(String.valueOf(actionCommand));firstInput=false;}
else outputField.setText(outputField.getText()+actionCommand);
break;
case '+':case '-':case '*':case '/':
if(firstInput)model.setOperate(actionCommand);
else{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
model.setOperate(actionCommand);
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
break;
case '=':
if(firstInput){
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
}
else{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
} } }
public static void main(String args[]){
new Calculate().registEvent();
}
}
//Calculatecommon.java
class Calculatecommon{
private double result;
private double operateNumber;
private char operate;
public Calculatecommon(){
result=0;
operateNumber=0;
operate=' ';
}
public void setResult(double n){
result=n;
}
public void setOperate(char o){
operate=o;
}
public void setOperateNumber(double n){
operateNumber=n;
}
public double getResult(){
return result;
}
public void calculating(){
switch(operate){
case ' ':result=operateNumber;break;
case '+':result+=operateNumber;break;
case '-':result-=operateNumber;break;
case '*':result*=operateNumber;break;
case '/':result/=operateNumber;break;
}
}
public void reset(){
result=0;
operateNumber=0;
operate=' ';
}
public double toSign(double n){
return 0-(n-0);
}
public String backSpace(String n){
String reResult="0";
try{
reResult=n.substring(0,n.length()-1);
Double.parseDouble(reResult);
}
catch(NumberFormatException e){reResult="0";}
return reResult;
}
}
[此贴子已经被作者于2007-1-3 19:58:22编辑过]