| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1103 人关注过本帖
标题:怎么实现响应键盘事件啊??
只看楼主 加入收藏
guxianming
Rank: 1
等 级:新手上路
帖 子:140
专家分:0
注 册:2006-7-4
收藏
 问题点数:0 回复次数:3 
怎么实现响应键盘事件啊??

怎么实现响应键盘事件啊??
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编辑过]

搜索更多相关主题的帖子: 键盘 import awt java private 
2007-01-03 19:54
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

addKeyListener

和你实现按钮addActionListener差不多


可惜不是你,陪我到最后
2007-01-03 20:01
guxianming
Rank: 1
等 级:新手上路
帖 子:140
专家分:0
注 册:2006-7-4
收藏
得分:0 

那我试试,不行的话,版主你要帮我改改啊.


java --you are my second girdfriend.
2007-01-03 20:18
117ai
Rank: 1
等 级:新手上路
帖 子:200
专家分:0
注 册:2006-12-9
收藏
得分:0 

KeyListener 记得要实现里面的所有方法!


人真的很奇怪,以前看书时老喜欢看表,希望时间快点过;现在看书总是不敢看表,但时间却很快过!
2007-01-03 21:53
快速回复:怎么实现响应键盘事件啊??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022805 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved