是用JB编的,下只附JFrame1代码
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.Component;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1
extends JFrame {
JPanel contentPane;
JTextField result = new JTextField("0");
boolean add,sub,mul,div;
double num1,num2,num;
JButton jButton1 = new JButton();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.requestFocus(true);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("计数器");
result.setBounds(new Rectangle(102, 44, 152, 21));
this.addKeyListener(new Frame1_this_keyAdapter(this));
contentPane.add(result);
contentPane.add(jButton1);
}
void number(int i)
{
String s=Integer.toString(i);
if(s.equals(result.getText())){
result.setText(s);
}else{
result.setText(result.getText()+s);
}
this.requestFocus(true);
}
void sign(int i){
num1=Double.valueOf(result.getText());
result.setText("0");
if(i==1){
add=true;
sub=false;
mul=false;
div=false;
}else if(i==2){
sub=true;
add=false;
mul=false;
div=false;
}else if(i==3){
mul=true;
add=false;
sub=false;
div=false;
}else{
div=true;
add=false;
sub=false;
mul=false;
}
this.requestFocus(true);
}
void getResult(){
num2=Double.valueOf(result.getText());
if(add){
num=num1+num2;
}else if(sub){
num=num1-num2;
}else if(mul){
num=num1*num2;
}else{
num=num1/num2;
}
result.setText(""+num);
this.requestFocus(true);
}
void nPoint(){
result.setText(result.getText()+".");
}
/*public boolean isFocusTraversable(){
return true;
}*/
public void this_keyPressed(KeyEvent e) {
int kc=e.getKeyCode();
switch(kc){
case 48:
case 96:number(0);break;
case 49:
case 97:number(1);break;
case 50:
case 98:number(2);break;
case 51:
case 99:number(3);break;
case 52:
case 100:number(4);break;
case 53:
case 101:number(5);break;
case 54:
case 102:number(6);break;
case 55:
case 103:number(7);break;
case 56:
case 104:number(8);break;
case 57:
case 105:number(9);break;
case 107:sign(1);break;
case 109:sign(2);break;
case 106:sign(3);break;
case 111:sign(4);break;
case 10:
case 61:getResult();break;
case 46:
case 110:nPoint();break;
default:break;
}
}
}
class Frame1_this_keyAdapter
extends KeyAdapter {
private Frame1 adaptee;
Frame1_this_keyAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.this_keyPressed(e);
}
}