永发信息网

答案:1  悬赏:60  手机版
解决时间 2021-05-06 03:05
  • 提问者网友:留有余香
  • 2021-05-05 12:32

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)
{}
}

最佳答案
  • 五星知识达人网友:妄饮晩冬酒
  • 2021-05-05 13:42
Label是不能通过键盘直接往里写的,应该换成TextField 或TextArea,将Label tf=new Label();改成TextField tf=new TextField();就可以了
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯