importxa0java.awt.event.*;xa0
importxa0javax.swing.*;xa0
publicxa0classxa0Calculatorxa0extendsxa0JFramexa0{xa0
privatexa0Containxa0
privatexa0GridBagLayoutxa0layout;xa0
privatexa0GridBagConstraints;xa0
privatexa0JTextFieldxa0displayField;
pingxa0lastCommand;
privatexa0doublexa0result;
privatexa0boole;
publicxa0Calculator()xa0{xa0
super(“Calcu
container=getContentPane();xa0
layout=newxa0GridBagLayout();xa0
container.setLayout(layout);xa0
constraints=newxa0GridBagConstraints();xa0
start=true;xa0
result=0;xa0
lastCommandxa0=xa0″=”;xa0
displayField=newxa0JTextField(20);xa0
displayField.talAlignment(JTextField.RI
constraints.gridx=0;xa0
c.gridy=0;xa0
constraints.gridwidth=4;xa0
constraints.gridheight=1;xa0
constraintsonstraints.B
constraints.weightx=100;xa0
cts.weighty=100;xa0
layout.setConstraints(displanstraints);xa0
container.add(displayField);xa0
ActionListenerxa0insertxa0=xa0newxa0InsertAction();xa0
ActionListenerxa0commandxa0=xa0newxa0CommandAction();xa0
addButton(“Backspace”,0,1t);xa0
addButton(“CE”,2,1,1,1,insert);xa0
addButton(“C”,3,1,1,1,insert);xa0
addButto,1,1,insert);xa0
addButton(“8”,1,2,1,1,insert);xa0
addButton(“9”,2,2,1,1,insert);xa0
addButton(“/”,3,2,1,1,command);xa0
addButtinsert);xa0
addButton(“5”,1,3,1,1,insert);xa0
addButton(“6”,2,3,1,1,insert);xa0
addButt1,1,command);xa0
addButton(“1”,0,4,1,1,insert);xa0
addButton(“2”,1,4,1,1,insert);xa0
addButton(“3”,2,4,1,1,insert);xa0
addButton(“-“,3,4,1,1,command);xa0
addButton(“0”,0,5,1,1,insert);xa0
addButton(“+/-“,1,5,1,1
addButton(“.”,2,5,1,1,insert);xa0
addButton(“+”,3,5,1,1,command);xa0
addButton(“=”,0,6,4,1,command);xa0
setSize(300,300);xa0
setVisible(true);xa0
}xa0
privatexa0voidxa0addButton(Stringxa0label,intxa0row,intxa0column,intxa0with,intxa0height,ActionListenerxa0listener)xa0{xa0
JButtonxa0button=newxa0JButton(label);xa0
constraints.gridx=row;xa0
constraints.gridy=column;xa0
constraints.gridwidth=with;xa0
constraints.gridheight=height;xa0
constraints.fill=GridBagConstraints.BOTH;xa0
button.addActionListener(listener);xa0
layout.setConstraints(button,constraints);xa0
container.add(button);xa0
}xa0
privatexa0classxa0InsertActionxa0implementsxa0ActionListenerxa0{xa0
publicxa0voidxa0actionPerformed(ActionEventxa0event)xa0{xa0
Stringxa0input=event.getActionCommand();xa0
ifxa0(start)xa0{xa0
displayField.setText(“”);xa0
start=false;xa0
if(input.equals(“+/-“))xa0
displayField.setText(displayField.getText()+”-“);xa0
}xa0
if(!e68a84e8a2ade799bee5baa631333337383963input.equals(“+/-“))xa0{xa0
if(input.equals(“Backspace”))xa0{xa0
Stringxa0str=displayField.getText();xa0
if(str.length()>0)xa0
displayField.setText(str.substring(0,str.length()-1));xa0
}elsexa0if(input.equals(“CE”)||input.equals(“C”))xa0{xa0
displayField.setText(“0”);xa0
start=true;xa0
}elsexa0
displayField.setText(displayField.getText()+input);xa0
}xa0
}xa0
}xa0
privatexa0classxa0CommandActionxa0implementsxa0ActionListenerxa0{xa0
publicxa0voidxa0actionPerformed(ActionEventxa0evt)xa0{xa0
Stringxa0command=evt.getActionCommand();xa0
if(start)xa0{xa0
lastCommand=command;xa0
}elsexa0{xa0
calculate(Double.parseDouble(displayField.getText()));xa0
lastCommand=command;xa0
start=true;xa0
}xa0
}xa0
}xa0
publicxa0voidxa0calculate(doublexa0x)xa0{xa0
ifxa0(lastCommand.equals(“+”))xa0result+=xa0x;xa0
elsexa0ifxa0(lastCommand.equals(“-“))xa0result-=x;xa0
elsexa0ifxa0(lastCommand.equals(“*”))xa0result*=x;xa0
elsexa0ifxa0(lastCommand.equals(“/”))xa0result/=x;xa0
elsexa0ifxa0(lastCommand.equals(“=”))xa0result=x;xa0
displayField.setText(“”+xa0result);xa0
}xa0
publicxa0staticxa0voidxa0main(Stringxa0args)xa0{xa0
Calculatorxa0calculator=newxa0Calculator();xa0
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);xa0
}xa0
}
版权声明:以上文章转载自互联网用户,该文观点仅代表作者本人。文章内容仅供参考,不构成建议,不代表快递查询网立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如有侵权请联系:1048208551@qq.com,将立刻删除。