我编写了一个计算器,但是发现键盘监听有问题。刚运行时,不能直接用键盘输入,要先用鼠标输入才能跟着用键盘输入,这是为什么???
就是这段代码了,请帮帮我。谢谢~~!!!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class sj4_1 extends Applet implements MouseListener{
private Button btnKeyBoard[] = new Button[20];
private TextField tf1=new TextField(15);
private String input="";
private int oper=0,oldoper=0,newoper=0;
private double answer,num1=0.0,num2=0.0,num3=0.0;
private final int ADD=1,SUB=2,MUL=3,DIV=4,SQRT=5,PERC=6,AIB=7;
private boolean firstpress=true,morenums=false,equals=false,clearscreen=false,dec=false,doubleclick=false;
Panel p1,p2;
public void init(){
resize(200,200);
setLayout(new BorderLayout());
p1 = new Panel();
add(p1,BorderLayout.NORTH);
p1.add(tf1);
p2 = new Panel();
add(p2,BorderLayout.CENTER);
int AddOrder[] = { 8, 9, 10, 0, 5, 6, 7, 11,2, 3, 4, 12, 1, 15, 14, 13 ,16,17,18,19};
p2.setLayout(new GridLayout(5, 4, 5, 5));
btnKeyBoard[0] = new Button();
btnKeyBoard[0].setLabel("C");
int i;
for (i = 1;i < 11;i ++)
btnKeyBoard[i] = new Button(String.valueOf(i-1)); btnKeyBoard[11] = new Button("+");
btnKeyBoard[12] = new Button("-");
btnKeyBoard[13] = new Button("*");
btnKeyBoard[14] = new Button("/");
btnKeyBoard[15] = new Button("=");
btnKeyBoard[16] = new Button(".");
btnKeyBoard[17] = new Button("sqrt");
btnKeyBoard[18] = new Button("%");
btnKeyBoard[19] = new Button("1/X");
for (i = 0;i < 20;i ++){
p2.add(btnKeyBoard[AddOrder[i]]);
btnKeyBoard[i].addKeyListener(new KK());
btnKeyBoard[i].addMouseListener(this);
}
}
public void mouseReleased(MouseEvent e){
if(e.getSource()!=btnKeyBoard[0]&&e.getSource()!=btnKeyBoard[11]&&e.getSource()!=btnKeyBoard[12]&&e.getSource()!=btnKeyBoard[13]&&e.getSource()!=btnKeyBoard[14]&&e.getSource()!=btnKeyBoard[15]&&e.getSource()!=btnKeyBoard[17]&&e.getSource()!=btnKeyBoard[18]&&e.getSource()!=btnKeyBoard[19]){
if(clearscreen){
clearScreen();
clearscreen=false;
}
if(e.getSource()==btnKeyBoard[2]){
input+="1";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[3]){
input+="2";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[4]){
input+="3";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[5]){
input+="4";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[6]){
input+="5";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[7]){
input+="6";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[8]){
input+="7";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[9]){
input+="8";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[10]){
input+="9";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[1]){
input+="0";
tf1.setText(input);
showAnswer(input);
}
else if(e.getSource()==btnKeyBoard[16]){
if(dec==false){
dec=true;
input+=".";
showAnswer(input);
}
}
}
if(e.getSource()==btnKeyBoard[11]){
clearscreen=false;
dec=false;
oper=ADD;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[12]){
clearscreen=false;
dec=false;
oper=SUB;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[13]){
clearscreen=false;
dec=false;
oper=MUL;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[14]){
clearscreen=false;
dec=false;
oper=DIV;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[17]){
clearscreen=false;
dec=false;
oper=SQRT;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[18]){
clearscreen=false;
dec=false;
oper=PERC;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[19]){
clearscreen=false;
dec=false;
oper=AIB;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[0]){
clearScreen();
}
if(e.getSource()==btnKeyBoard[15]){
equals=true;
clearscreen=false;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input=Double.toString(answer);
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
就是这段代码了,请帮帮我。谢谢~~!!!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class sj4_1 extends Applet implements MouseListener{
private Button btnKeyBoard[] = new Button[20];
private TextField tf1=new TextField(15);
private String input="";
private int oper=0,oldoper=0,newoper=0;
private double answer,num1=0.0,num2=0.0,num3=0.0;
private final int ADD=1,SUB=2,MUL=3,DIV=4,SQRT=5,PERC=6,AIB=7;
private boolean firstpress=true,morenums=false,equals=false,clearscreen=false,dec=false,doubleclick=false;
Panel p1,p2;
public void init(){
resize(200,200);
setLayout(new BorderLayout());
p1 = new Panel();
add(p1,BorderLayout.NORTH);
p1.add(tf1);
p2 = new Panel();
add(p2,BorderLayout.CENTER);
int AddOrder[] = { 8, 9, 10, 0, 5, 6, 7, 11,2, 3, 4, 12, 1, 15, 14, 13 ,16,17,18,19};
p2.setLayout(new GridLayout(5, 4, 5, 5));
btnKeyBoard[0] = new Button();
btnKeyBoard[0].setLabel("C");
int i;
for (i = 1;i < 11;i ++)
btnKeyBoard[i] = new Button(String.valueOf(i-1)); btnKeyBoard[11] = new Button("+");
btnKeyBoard[12] = new Button("-");
btnKeyBoard[13] = new Button("*");
btnKeyBoard[14] = new Button("/");
btnKeyBoard[15] = new Button("=");
btnKeyBoard[16] = new Button(".");
btnKeyBoard[17] = new Button("sqrt");
btnKeyBoard[18] = new Button("%");
btnKeyBoard[19] = new Button("1/X");
for (i = 0;i < 20;i ++){
p2.add(btnKeyBoard[AddOrder[i]]);
btnKeyBoard[i].addKeyListener(new KK());
btnKeyBoard[i].addMouseListener(this);
}
}
public void mouseReleased(MouseEvent e){
if(e.getSource()!=btnKeyBoard[0]&&e.getSource()!=btnKeyBoard[11]&&e.getSource()!=btnKeyBoard[12]&&e.getSource()!=btnKeyBoard[13]&&e.getSource()!=btnKeyBoard[14]&&e.getSource()!=btnKeyBoard[15]&&e.getSource()!=btnKeyBoard[17]&&e.getSource()!=btnKeyBoard[18]&&e.getSource()!=btnKeyBoard[19]){
if(clearscreen){
clearScreen();
clearscreen=false;
}
if(e.getSource()==btnKeyBoard[2]){
input+="1";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[3]){
input+="2";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[4]){
input+="3";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[5]){
input+="4";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[6]){
input+="5";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[7]){
input+="6";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[8]){
input+="7";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[9]){
input+="8";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[10]){
input+="9";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==btnKeyBoard[1]){
input+="0";
tf1.setText(input);
showAnswer(input);
}
else if(e.getSource()==btnKeyBoard[16]){
if(dec==false){
dec=true;
input+=".";
showAnswer(input);
}
}
}
if(e.getSource()==btnKeyBoard[11]){
clearscreen=false;
dec=false;
oper=ADD;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[12]){
clearscreen=false;
dec=false;
oper=SUB;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[13]){
clearscreen=false;
dec=false;
oper=MUL;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[14]){
clearscreen=false;
dec=false;
oper=DIV;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[17]){
clearscreen=false;
dec=false;
oper=SQRT;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[18]){
clearscreen=false;
dec=false;
oper=PERC;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[19]){
clearscreen=false;
dec=false;
oper=AIB;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==btnKeyBoard[0]){
clearScreen();
}
if(e.getSource()==btnKeyBoard[15]){
equals=true;
clearscreen=false;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input=Double.toString(answer);
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
class KK implements KeyListener{
public void keyTyped(KeyEvent e){
if(e.getKeyChar()!='c'&&e.getKeyChar()!='+'&&e.getKeyChar()!='-'&&e.getKeyChar()!='*'&&e.getKeyChar()!='/'&&e.getKeyChar()!='='&&e.getKeyChar()!='%'){
if(clearscreen){
clearScreen();
clearscreen=false;
}
if(e.getKeyChar()=='1'){
input+="1";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='2'){
input+="2";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='3'){
input+="3";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='4'){
input+="4";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='5'){
input+="5";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='6'){
input+="6";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='7'){
input+="7";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='8'){
input+="8";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='9'){
input+="9";
tf1.setText(input);
showAnswer(input);
}
if(e.getKeyChar()=='0'){
input+="0";
tf1.setText(input);
showAnswer(input);
}
else if(e.getKeyChar()=='.'){
if(dec==false){
dec=true;
input+=".";
showAnswer(input);
}
}
}
else{
if(e.getKeyChar()=='+'){
clearscreen=false;
dec=false;
oper=ADD;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getKeyChar()=='-'){
clearscreen=false;
dec=false;
oper=SUB;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getKeyChar()=='*'){
clearscreen=false;
dec=false;
oper=MUL;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getKeyChar()=='/'){
clearscreen=false;
dec=false;
oper=DIV;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getKeyChar()=='c'){
clearScreen();
}
if(e.getKeyChar()=='='){
equals=true;
clearscreen=false;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input=Double.toString(answer);
}
}
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
public void processNumbers(){
if(firstpress){
if(equals){
num1=answer;
equals=false;
}
else
num1=Double.valueOf(input).doubleValue();
oldoper=oper;
if (oper==SQRT){
answer=claculate(oldoper,num1,0.0);
showAnswer(Double.toString(answer));
morenums=true;
}
if (oper==PERC){
answer=claculate(oldoper,num1,0.0);
showAnswer(Double.toString(answer));
morenums=true;
}
if (oper==AIB){
answer=claculate(oldoper,num1,0.0);
showAnswer(Double.toString(answer));
morenums=true;
}
firstpress=false;
}
else if(!morenums){
num2=Double.valueOf(input).doubleValue();
answer=claculate(oldoper,num1,num2);
showAnswer(Double.toString(answer));
newoper=oper;
if(!equals)
morenums=true;
else{
morenums=false;
firstpress=true;
}
}
else {
if(equals){
newoper=oper;
morenums=false;
firstpress=true;
}
num3=Double.valueOf(input).doubleValue();
answer=claculate(newoper,answer,num3);
showAnswer(Double.toString(answer));
newoper=oper;
}
}
public double claculate(int oper,double number1,double number2){
double answer=0.0;
switch(oper){
case ADD:
answer=number1+number2;
break;
case SUB:
answer=number1-number2;
break;
case MUL:
answer=number1*number2;
break;
case DIV:
answer=number1/number2;
break;
case SQRT:
answer=Math.sqrt(number1);
break;
case PERC:
answer=number1*100;
break;
case AIB:
answer=1/number1;
break;
}
return answer;
}
public void showAnswer(String s){
double answer;
answer =Double.valueOf(s).doubleValue();
if(dec)
tf1.setText(Double.toString(answer));
else
tf1.setText(s);
}
public boolean clickCheck(String s){
if(s=="")
doubleclick=true;
else
doubleclick=false;
return doubleclick;
}
public void clearScreen(){
oper=0;
input="";
answer=0;
dec=false;
morenums=false;
firstpress=true;
equals=false;
showAnswer(Integer.toString((int)answer));
}
}
KeyListenert和MouseListener是同一级的监听器,有不同的吗??
我试过用ActionListener都是一样不行..
用鼠标输入没问题,用键盘输入数字没问题,但是,对于操作符却视而不见。
整个程序并没有报错。
是不是漏写了写代码啊?