是用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);
}
}
----------------解决方案--------------------------------------------------------
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());//我只能发现这里错了哦。 这样返回的是个Double对象;
num1=Double.parseDouble(result.getText());//这2种都可以得到double类型的数
num1=Double.valueOf(result.getText()).doubleValue();//
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);
}
}
----------------解决方案--------------------------------------------------------
谢谢大家的帮忙,错误我自己改过来的,下面是计数器所有代码
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Calculatorapn
{
public static void main(String[] args)
{
JFrame cul=new cal();
cul.show();
}
}
class cal extends JFrame implements ActionListener,KeyListener
{
JPanel p=new JPanel(new GridLayout(5,4));
Vector b=new Vector();
JTextField result=new JTextField("0");
String s1,s2,c;
double d1,d2,d,num1,num2,num;
boolean isd,end,add,sub,mul,div;
cal()
{
super("Calculator");
b.addElement(new JButton("7"));b.addElement(new JButton("8"));b.addElement(new JButton("9"));b.addElement(new JButton("+"));
b.addElement(new JButton("4"));b.addElement(new JButton("5"));b.addElement(new JButton("6"));b.addElement(new JButton("-"));
b.addElement(new JButton("1"));b.addElement(new JButton("2"));b.addElement(new JButton("3"));b.addElement(new JButton("*"));
b.addElement(new JButton("0"));b.addElement(new JButton("."));b.addElement(new JButton("="));b.addElement(new JButton("/"));
b.addElement(new JButton());b.addElement(new JButton());b.addElement(new JButton("back"));b.addElement(new JButton("c"));
for(int k=-1,i=1;i<=20;i++)
{
p.add((JButton)b.elementAt(++k));
((JButton)b.elementAt(k)).addActionListener(this);
}
add(result,"North");
add(p,"Center");
addWindowListener(new WinApt());
pack();
setResizable(false);
this.requestFocus(true);
addKeyListener(this);
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void 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 43:
case 107:sign(1);break;
case 45:
case 109:sign(2);break;
case 42:
case 106:sign(3);break;
case 47:
case 111:sign(4);break;
case 10:getResult();break;
case 46:
case 110:nPoint();break;//小数点
case 8:back();break;//回车
case 27:clear();break;//ESC 清除
default:break;
}
}
public void actionPerformed(ActionEvent e)
{
String s=((JButton)e.getSource()).getLabel();
if(s.equals("+")){
sign(1);
}else if(s.equals("-")){
sign(2);
}else if(s.equals("*")){
sign(3);
}else if(s.equals("/")){
sign(4);
}else if(s.equals("c")){
clear();
}else if(s.equals("back")){
back();
}else if(s.equals("=")){
getResult();
}else if(s.equals(".")){
nPoint();
}else{
int lbnum=Integer.parseInt(s);
number(lbnum);
}
}
void number(int i) //数值
{
String s=Integer.toString(i);
if(result.getText().equals("0")){
result.setText(s);
}else{
result.setText(result.getText()+s);
}
this.requestFocus(true);
}
void sign(int i){ //运算符
num1=Double.valueOf(result.getText()).doubleValue();
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()).doubleValue();
if(add==true){
num=num1+num2;
}else if(sub==true){
num=num1-num2;
}else if(mul==true){
num=num1*num2;
}else{
num=num1/num2;
}
result.setText(""+num);
this.requestFocus(true);
}
void nPoint(){ //加小数点
result.setText(result.getText()+".");
}
void back(){
result.setText(result.getText().substring(0,result.getText().length()-1));
}
void clear(){
result.setText("0");
}
public boolean isFocusTraversable(){
return true;
}
}
class WinApt extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
----------------解决方案--------------------------------------------------------