import java.awt.Container;
import javax.swing.*;
public class CalculatorFrame extends JFrame{
JTextField text1=new JTextField(25);
JTextField text2=new JTextField(2);
JTextField text3=new JTextField();
JButton backSP=new JButton("Backspace");
JButton ce=new JButton("CE");
JButton c=new JButton("C");
JButton mclear=new JButton("MC");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton divide=new JButton("/");
JButton sqrt=new JButton("sqrt");
JButton mread=new JButton("MR");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton multiple=new JButton("*");
JButton percent=new JButton("%");
JButton msave=new JButton("MS");
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton minus=new JButton("-");
JButton invert=new JButton("1/x");
JButton mplus=new JButton("M+");
JButton b0=new JButton("0");
JButton reverse=new JButton("+/-");
JButton dot=new JButton(".");
JButton plus=new JButton("+");
JButton equal=new JButton("=");
JLabel name=new JLabel("05070409制");
Container contentpane;
double num;
String op="",s="";
double result,temp;
boolean flag=true,containdot=false,enterednum=false;//flag代表是否应该覆盖原有的text内容
int integer;
double mem;
public CalculatorFrame(){
super("计算器");
contentpane=getContentPane();
contentpane.setLayout(null);
contentpane.add(text1);
text1.setBounds(10, 20, 355, 30);
text1.setHorizontalAlignment(JTextField.RIGHT);
text1.setFocusable(false);
text1.setText("0.");
contentpane.add(text2);
text2.setBounds(10, 65, 20, 20);
text2.setFocusable(false);
text2.setEditable(false);
contentpane.add(backSP);
backSP.setBounds(105, 55, 100, 30);
backSP.addActionListener(new commandProcessor(this));
backSP.addKeyListener(new commandProcessor(this));
contentpane.add(ce);
ce.setBounds(210, 55, 80, 30);
ce.addActionListener(new commandProcessor(this));
ce.addKeyListener(new commandProcessor(this));
contentpane.add(c);
c.setBounds(295, 55, 70, 30);
c.addActionListener(new commandProcessor(this));
c.addActionListener(new commandProcessor(this));
contentpane.add(mclear);
mclear.setBounds(10, 90, 60, 30);
mclear.addActionListener(new commandProcessor(this));
mclear.addActionListener(new commandProcessor(this));
contentpane.add(b7);
b7.setBounds(75, 90, 50, 30);
b7.addActionListener(new commandProcessor(this));
b7.addActionListener(new commandProcessor(this));
contentpane.add(b8);
b8.setBounds(130, 90, 50, 30);
b8.addActionListener(new commandProcessor(this));
b8.addActionListener(new commandProcessor(this));
contentpane.add(b9);
b9.setBounds(185, 90, 50, 30);
b9.addActionListener(new commandProcessor(this));
contentpane.add(divide);
divide.setBounds(240, 90, 50, 30);
divide.addActionListener(new commandProcessor(this));
divide.addActionListener(new commandProcessor(this));
contentpane.add(sqrt);
sqrt.setBounds(295, 90, 70, 30);
sqrt.addActionListener(new commandProcessor(this));
sqrt.addActionListener(new commandProcessor(this));
contentpane.add(mread);
mread.setBounds(10, 125, 60, 30);
mread.addActionListener(new commandProcessor(this));
mread.addActionListener(new commandProcessor(this));
contentpane.add(b4);
b4.setBounds(75, 125, 50, 30);
b4.addActionListener(new commandProcessor(this));
b4.addActionListener(new commandProcessor(this));
contentpane.add(b5);
b5.setBounds(130, 125, 50, 30);
b5.addActionListener(new commandProcessor(this));
b5.addActionListener(new commandProcessor(this));
contentpane.add(b6);
b6.setBounds(185, 125, 50, 30);
b6.addActionListener(new commandProcessor(this));
b6.addActionListener(new commandProcessor(this));
contentpane.add(multiple);
multiple.setBounds(240, 125, 50, 30);
multiple.addActionListener(new commandProcessor(this));
multiple.addActionListener(new commandProcessor(this));
contentpane.add(percent);
percent.setBounds(295, 125, 70, 30);
percent.addActionListener(new commandProcessor(this));
percent.addActionListener(new commandProcessor(this));
contentpane.add(msave);
msave.setBounds(10, 160, 60, 30);
msave.addActionListener(new commandProcessor(this));
msave.addActionListener(new commandProcessor(this));
contentpane.add(b1);
b1.setBounds(75, 160, 50, 30);
b1.addActionListener(new commandProcessor(this));
b1.addActionListener(new commandProcessor(this));
contentpane.add(b2);
b2.setBounds(130,160,50,30);
b2.addActionListener(new commandProcessor(this));
b2.addActionListener(new commandProcessor(this));
contentpane.add(b3);
b3.setBounds(185, 160, 50, 30);
b3.addActionListener(new commandProcessor(this));
b3.addActionListener(new commandProcessor(this));
contentpane.add(minus);
minus.setBounds(240, 160, 50, 30);
minus.addActionListener(new commandProcessor(this));
minus.addActionListener(new commandProcessor(this));
contentpane.add(invert);
invert.setBounds(295, 160, 70, 30);
invert.addActionListener(new commandProcessor(this));
invert.addActionListener(new commandProcessor(this));
contentpane.add(mplus);
mplus.setBounds(10, 195, 60, 30);
mplus.addActionListener(new commandProcessor(this));
mplus.addActionListener(new commandProcessor(this));
contentpane.add(b0);
b0.setBounds(75, 195, 50, 30);
b0.addActionListener(new commandProcessor(this));
b0.addActionListener(new commandProcessor(this));
contentpane.add(reverse);
reverse.setBounds(130, 195, 50, 30);
reverse.addActionListener(new commandProcessor(this));
reverse.addActionListener(new commandProcessor(this));
contentpane.add(dot);
dot.setBounds(185, 195, 50, 30);
dot.addActionListener(new commandProcessor(this));
dot.addActionListener(new commandProcessor(this));
contentpane.add(plus);
plus.setBounds(240, 195, 50, 30);
plus.addActionListener(new commandProcessor(this));
plus.addActionListener(new commandProcessor(this));
contentpane.add(equal);
equal.setBounds(295,195,70,30);
equal.addActionListener(new commandProcessor(this));
equal.addActionListener(new commandProcessor(this));
contentpane.add(name);
name.setBounds(240, 230, 100, 30);
setSize(385,300);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.event.*;
public class commandProcessor extends KeyAdapter implements ActionListener{
String cmd; //获取命令字符串
CalculatorFrame ui;
public commandProcessor(CalculatorFrame ui){
this.ui=ui;
}
public void actionPerformed(ActionEvent e){
cmd=e.getActionCommand();
Compute(cmd);
}
public void keyPressed(KeyEvent e){
int keycode=0;
char c;
keycode=e.getKeyCode();
cmd=e.getKeyText(keycode);
//System.out.print(cmd); //这里是第一个问题
Compute(cmd);
}
public void Compute(String cmd){
if(cmd=="1"|cmd=="2"|cmd=="3"|cmd=="4"|cmd=="5"|cmd=="6"|cmd=="7"|cmd=="8"|cmd=="9"|cmd=="0"){
if(ui.flag) {
ui.s+=cmd;
if(ui.containdot==false)
ui.text1.setText(ui.s+".");
else ui.text1.setText(ui.s);
ui.enterednum=true;
}
else {
ui.s="";
ui.flag=true;
ui.s=cmd;
ui.text1.setText(ui.s+".");
}
}
else if(cmd=="+"|cmd=="-"|cmd=="*"|cmd=="/"){
if(ui.op!=""){ //判断是不是连加,如果是那么立刻将上一步结果显示出来
ui.num=Double.parseDouble(ui.text1.getText());
if(ui.op=="+") ui.result+=ui.num;
if(ui.op=="-") ui.result-=ui.num;
if(ui.op=="*") ui.result*=ui.num;
if(ui.op=="/") ui.result/=ui.num;
if(ui.result==(int)(ui.result)){
ui.integer=(int)(ui.result);
ui.text1.setText(Integer.toString(ui.integer)+".");
}
else ui.text1.setText(Double.toString(ui.result));
}
else {
ui.result=Double.parseDouble(ui.text1.getText());
}
ui.op=cmd;
ui.num=0;
ui.flag=false;
ui.containdot=false;
ui.num=0;
}
else if(cmd=="="){
ui.num=Double.parseDouble(ui.text1.getText());
if(ui.op=="+") ui.result+=ui.num;
if(ui.op=="-") ui.result-=ui.num;
if(ui.op=="*") ui.result*=ui.num;
if(ui.op=="/")
ui.result/=ui.num;
if(ui.result==(int)(ui.result)){ //判断是否可以去掉小数点后的数
ui.integer=(int)(ui.result);
ui.text1.setText(Integer.toString(ui.integer)+".");
}
else ui.text1.setText(Double.toString(ui.result));
ui.num=0;
ui.result=0;
ui.s="";
ui.containdot=false;
ui.op="";
}
else if(cmd=="+/-"){
ui.result=Double.parseDouble(ui.text1.getText());
ui.result=-1*ui.result;
if(ui.result==(int)(ui.result)){
ui.integer=(int)(ui.result);
ui.text1.setText(Integer.toString(ui.integer)+".");
}
else ui.text1.setText(Double.toString(ui.result));
}
else if(cmd=="sqrt"){
ui.result=Double.parseDouble(ui.text1.getText());
ui.result=Math.sqrt(ui.result);
if(ui.result==(int)(ui.result)){
ui.integer=(int)(ui.result);
ui.text1.setText(Integer.toString(ui.integer)+".");
}
else ui.text1.setText(Double.toString(ui.result));
}
else if(cmd=="%"){
ui.temp=Double.parseDouble(ui.text1.getText());
if(ui.op=="") ui.text1.setText("0"); //判断是否输入了第二个数,如果没输入只返回0
//如果输入了就将第二个数除100后返回屏幕上
else
ui.temp=Double.parseDouble(ui.text1.getText())/100.0;
if(ui.temp==(int)(ui.temp)){
ui.integer=(int)(ui.num);
ui.text1.setText(Integer.toString(ui.integer)+".");
}
else ui.text1.setText(Double.toString(ui.temp));
}
else if(cmd=="1/x"){
ui.result=Double.parseDouble(ui.text1.getText());
ui.result=1/ui.result;
if(ui.result==(int)(ui.result)){
ui.integer=(int)(ui.result);
ui.text1.setText(Integer.toString(ui.integer)+".");
}
else ui.text1.setText(Double.toString(ui.result));
}
else if(cmd=="C"){
ui.text1.setText("0.");
ui.num=0;
ui.op="";
ui.s="";
ui.result=0;
ui.temp=0;
ui.flag=true;
ui.containdot=false;
}
else if(cmd=="Backspace"){
StringBuffer buff=new StringBuffer(ui.text1.getText());
StringBuffer cmdbuff=new StringBuffer(ui.s);
if(ui.s==""){}
else{
if(buff.charAt(buff.length()-1)=='.'){
if(buff.length()==2){
buff.delete(0,buff.length());
buff.append("0.");
ui.s="";
ui.text1.setText(buff.toString());
}
else{
if(ui.containdot){
ui.containdot=false;
cmdbuff.deleteCharAt(cmdbuff.length()-1);
ui.s=cmdbuff.toString();
}
else {
cmdbuff.deleteCharAt(cmdbuff.length()-1);
ui.s=cmdbuff.toString();
ui.containdot=false;
ui.text1.setText(cmdbuff.toString()+".");}
}
}
else{
buff.deleteCharAt(buff.length()-1);
ui.text1.setText(buff.toString());
ui.s=buff.toString();
}
}
}
else if(cmd=="CE"){
ui.text1.setText("0.");
ui.s="";
ui.containdot=false;
}
else if(cmd=="."){
if(ui.containdot)
ui.text1.setText(ui.s);
else {
if(ui.s=="") ui.s="0";
ui.text1.setText(ui.s+=".");
ui.containdot=true;
}
}
else if(cmd=="MC"){
ui.mem=0;
ui.text2.setText("");
}
else if(cmd=="MR"){
if(ui.mem==(int)(ui.mem)){
ui.integer=(int)(ui.mem);
ui.text1.setText(Integer.toString(ui.integer)+".");
}
else ui.text1.setText(Double.toString(ui.mem));
}
else if(cmd=="M+"){
ui.mem+=Double.parseDouble(ui.text1.getText());
ui.text2.setText("M");
ui.s="";
}
else if(cmd=="MS"){
ui.mem=Double.parseDouble(ui.text1.getText());
ui.text2.setText("M");
ui.s="";
}
}
}
import javax.swing.*;
public class Testport {
public static void main(String[] args) {
CalculatorFrame frm=new CalculatorFrame();
}
}
现在遇到2个问题,第一,从getkeytext()得来的字符串不知道为什么无法与Compute()中的配对,另一个就是如果先从键盘输入那么键盘监听器可以用,但一旦点了按钮,键盘监听器无效,困扰了我好久,高手帮忙看一下,谢谢
----------------解决方案--------------------------------------------------------