简易的计算器代码.docx
- 文档编号:24112672
- 上传时间:2023-05-24
- 格式:DOCX
- 页数:19
- 大小:17.10KB
简易的计算器代码.docx
《简易的计算器代码.docx》由会员分享,可在线阅读,更多相关《简易的计算器代码.docx(19页珍藏版)》请在冰豆网上搜索。
简易的计算器代码
//版权所有唐嘉宁
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
classMycomputeextendsJFrameimplementsActionListener{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
Containercon;
charch='#';
booleancan=false;
floatnum1=0;
JTextAreatj;
JButtonbutton1,button2,button3,button4,button5,button6,button7,
button8,button9,button10,button11,button12,button13,button14,
button15,button16,button17,button18,button19,button20,button21,
button22,button23,button24,button25,button26,button27,button28;
publicMycompute(){
super("计算器");
GridBagLayoutgridbag=newGridBagLayout();
GridBagConstraintsc=newGridBagConstraints();
con=this.getContentPane();
c.fill=GridBagConstraints.BOTH;
con.setFont(newFont("Helvetica",Font.PLAIN,14));
con.setLayout(gridbag);
c.insets=newInsets(2,2,2,2);
c.gridx=0;
c.gridy=0;
c.weightx=1.0;
c.gridwidth=5;
tj=newJTextArea("0.0",5,0);
gridbag.setConstraints(tj,c);
tj.setEditable(false);
con.add(tj);
button1=newJButton("MC");
c.gridx=0;
c.gridy=1;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button1,c);
con.add(button1);
button2=newJButton("MR");
c.gridx=1;
c.gridy=1;
c.gridwidth=1;
gridbag.setConstraints(button2,c);
con.add(button2);
button3=newJButton("MS");
c.gridx=2;
c.gridy=1;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button3,c);
con.add(button3);
button4=newJButton("M+");
c.gridx=3;
c.gridy=1;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button4,c);
con.add(button4);
button5=newJButton("M-");
c.gridx=4;
c.gridy=1;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button5,c);
con.add(button5);
button6=newJButton("←");
c.gridx=0;
c.gridy=2;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button6,c);
con.add(button6);
button7=newJButton("CE");
c.gridx=1;
c.gridy=2;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button7,c);
con.add(button7);
button8=newJButton("C");
c.gridx=2;
c.gridy=2;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button8,c);
con.add(button8);
button9=newJButton("±");
c.gridx=3;
c.gridy=2;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button9,c);
con.add(button9);
button10=newJButton("√");
c.gridx=4;
c.gridy=2;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button10,c);
con.add(button10);
button11=newJButton("7");
c.gridx=0;
c.gridy=3;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button11,c);
con.add(button11);
button12=newJButton("8");
c.gridx=1;
c.gridy=3;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button12,c);
con.add(button12);
button13=newJButton("9");
c.gridx=2;
c.gridy=3;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button13,c);
con.add(button13);
button14=newJButton("/");
c.gridx=3;
c.gridy=3;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button14,c);
con.add(button14);
button15=newJButton("%");
c.gridx=4;
c.gridy=3;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button15,c);
con.add(button15);
button16=newJButton("4");
c.gridx=0;
c.gridy=4;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button16,c);
con.add(button16);
button17=newJButton("5");
c.gridx=1;
c.gridy=4;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button17,c);
con.add(button17);
button18=newJButton("6");
c.gridx=2;
c.gridy=4;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button18,c);
con.add(button18);
button19=newJButton("*");
c.gridx=3;
c.gridy=4;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button19,c);
con.add(button19);
button20=newJButton("1/χ");
c.gridx=4;
c.gridy=4;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button20,c);
con.add(button20);
button21=newJButton("1");
c.gridx=0;
c.gridy=5;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button21,c);
con.add(button21);
button22=newJButton("2");
c.gridx=1;
c.gridy=5;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button22,c);
con.add(button22);
button23=newJButton("3");
c.gridx=2;
c.gridy=5;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button23,c);
con.add(button23);
button24=newJButton("-");
c.gridx=3;
c.gridy=5;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button24,c);
con.add(button24);
button25=newJButton("=");
c.gridx=4;
c.gridy=5;
c.weightx=1.0;
c.gridwidth=1;
c.gridheight=0;
gridbag.setConstraints(button25,c);
con.add(button25);
button26=newJButton("0");
c.gridx=0;
c.gridy=6;
c.weightx=1.0;
c.gridwidth=2;
gridbag.setConstraints(button26,c);
con.add(button26);
button27=newJButton(".");
c.gridx=2;
c.gridy=6;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button27,c);
con.add(button27);
button28=newJButton("+");
c.gridx=3;
c.gridy=6;
c.weightx=1.0;
c.gridwidth=1;
gridbag.setConstraints(button28,c);
con.add(button28);
button6.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
button13.addActionListener(this);
button14.addActionListener(this);
button15.addActionListener(this);
button16.addActionListener(this);
button17.addActionListener(this);
button18.addActionListener(this);
button19.addActionListener(this);
button20.addActionListener(this);
button21.addActionListener(this);
button22.addActionListener(this);
button23.addActionListener(this);
button24.addActionListener(this);
button25.addActionListener(this);
button26.addActionListener(this);
button27.addActionListener(this);
button28.addActionListener(this);
setSize(300,350);
Toolkittl=this.getToolkit();
setLocation((tl.getScreenSize().width-this.getWidth())/2,(tl.getScreenSize().height-this.getHeight())/2);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
Stringact=e.getActionCommand();
if(e.getSource()==button6){
if(tj.getText().length()>1)
tj.setText(tj.getText().substring(0,tj.getText().length()-1));
else
tj.setText("0");
return;
}
if(e.getSource()==button8){
tj.setText("0");
ch='#';
return;
}
if(act.equals("±")){
if(tj.getText().charAt(0)!
='-')
tj.setText("-"+tj.getText());
else
tj.setText(tj.getText().substring
(1));
return;
}
if(act.equals(".")){
tj.setText(tj.getText()+act);
return;
}
if(act!
="1/χ"&&act.charAt(0)>='0'&&act.charAt(0)<='9'){
if(can){
tj.setText(act);
can=false;
}
else
{
try{
if(Float.parseFloat(tj.getText())==0)
{
if(tj.getText().equals("0."))
tj.setText(tj.getText()+act);
else
tj.setText(act);
return;
}
else
{
tj.setText(tj.getText()+act);
return;
}
}
catch(NumberFormatExceptione1)
{
JOptionPane.showMessageDialog(null,"输入格式错误!
","警告!
",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
if(act.equals("+")||act.equals("-")||act.equals("*")||act.equals("/")||act.equals("%")){
if(ch!
='#'){
try{
num1=cacu(num1,ch,Float.parseFloat(tj.getText()));
tj.setText(String.valueOf(num1));
ch=act.charAt(0);
can=true;
return;
}
catch(NumberFormatExceptione1)
{
JOptionPane.showMessageDialog(null,"输入格式错误!
","警告!
",JOptionPane.ERROR_MESSAGE);
return;
}
}
else
{
try{
num1=Float.parseFloat(tj.getText());
ch=act.charAt(0);
can=true;
return;
}
catch(NumberFormatExceptione1)
{
JOptionPane.showMessageDialog(null,"输入格式错误!
","警告!
",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
if(act.equals("√")){
try{
floatnum=(float)Math.sqrt(Float.parseFloat(tj.getText()));
tj.setText(String.valueOf(num));
can=true;
return;
}
catch(NumberFormatExceptione1)
{
JOptionPane.showMessageDialog(null,"输入格式错误!
","警告!
",JOptionPane.ERROR_MESSAGE);
return;
}
}
if(act.equals("1/χ")){
try{
floatnum=1/Float.parseFloat(tj.getText());
tj.setText(String.valueOf(num));
can=true;
return;
}
catch(NumberFormatExceptione1)
{
JOptionPane.showMessageDialog(null,"输入格式错误!
","警告!
",JOptionPane.ERROR_MESSAGE);
return;
}
catch(ArithmeticExceptione1)
{
JOptionPane.showMessageDialog(null,"除0错误!
","警告!
",JOptionPane.ERROR_MESSAGE);return;}
}
if(act.equals("=")){
can=true;
try{if(ch=='#')
return;
floatnum=Float.parseFloat(tj.getText());
num1=cacu(num1,ch,num);
tj.setText(String.valueOf(num1));
ch='#';
return;
}
catch(NumberFormatExceptione1)
{
JOptionPane.showMessageDialog(null,"输入格式错误!
","警告!
",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
publicfloatcacu(floata,charc,floatb){
floatsum;
switch(c)
{
case'+':
sum=a+b;
break;
case'-':
sum=a-b;
break;
case'*':
sum=a*b;
break;
case'/':
if(b==0){
JOptionPane.showMessageDialog(null,"除0错误!
","警告!
",JOptionPane.ERROR_MESSAGE);
return0;
}
sum=a/b;
break;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 简易 计算器 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)