ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:17.10KB ,
资源ID:24112672      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/24112672.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(简易的计算器代码.docx)为本站会员(b****2)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

简易的计算器代码.docx

1、简易的计算器代码/版权所有 唐嘉宁import java.awt.*;import javax.swing.*;import java.awt.event.*;class Mycompute extends JFrame implements ActionListener /* * */ private static final long serialVersionUID = 1L; Container con; char ch=#; boolean can=false; float num1=0; JTextArea tj; JButton button1,button2,button3,b

2、utton4,button5,button6,button7, button8,button9,button10,button11,button12,button13,button14, button15,button16,button17,button18,button19,button20,button21, button22,button23,button24,button25,button26,button27,button28; public Mycompute() super(计算器); GridBagLayout gridbag = new GridBagLayout(); Gr

3、idBagConstraints c = new GridBagConstraints(); con=this.getContentPane(); c.fill = GridBagConstraints.BOTH; con.setFont(new Font(Helvetica, Font.PLAIN, 14); con.setLayout(gridbag); c.insets=new Insets(2,2,2,2); c.gridx=0; c.gridy=0; c.weightx = 1.0; c.gridwidth=5; tj=new JTextArea(0.0,5,0); gridbag.

4、setConstraints(tj,c); tj.setEditable(false); con.add(tj); button1=new JButton(MC); c.gridx=0; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button1,c); con.add(button1); button2=new JButton(MR); c.gridx=1; c.gridy=1; c.gridwidth=1; gridbag.setConstraints(button2,c); con.add(butto

5、n2); button3=new JButton(MS); c.gridx=2; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button3,c); con.add(button3); button4=new JButton(M+); c.gridx=3; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button4,c); con.add(button4); button5=new JButton(M-); c.grid

6、x=4; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button5,c); con.add(button5); button6=new JButton(); c.gridx=0; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button6,c); con.add(button6); button7=new JButton(CE); c.gridx=1; c.gridy=2; c.weightx = 1.0; c.gri

7、dwidth=1; gridbag.setConstraints(button7,c); con.add(button7); button8=new JButton(C); c.gridx=2; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button8,c); con.add(button8); button9=new JButton(); c.gridx=3; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button

8、9,c); con.add(button9); button10=new JButton(); c.gridx=4; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button10,c); con.add(button10); button11=new JButton(7); c.gridx=0; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button11,c); con.add(button11); button12=

9、new JButton(8); c.gridx=1; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button12,c); con.add(button12); button13=new JButton(9); c.gridx=2; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button13,c); con.add(button13); button14=new JButton(/); c.gridx=3; c.gri

10、dy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button14,c); con.add(button14); button15=new JButton(%); c.gridx=4; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button15,c); con.add(button15); button16=new JButton(4); c.gridx=0; c.gridy=4; c.weightx = 1.0; c.gridwid

11、th=1; gridbag.setConstraints(button16,c); con.add(button16); button17=new JButton(5); c.gridx=1; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button17,c); con.add(button17); button18=new JButton(6); c.gridx=2; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(but

12、ton18,c); con.add(button18); button19=new JButton(*); c.gridx=3; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button19,c); con.add(button19); button20=new JButton(1/); c.gridx=4; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button20,c); con.add(button20); bu

13、tton21=new JButton(1); c.gridx=0; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button21,c); con.add(button21); button22=new JButton(2); c.gridx=1; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button22,c); con.add(button22); button23=new JButton(3); c.gridx=2

14、; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button23,c); con.add(button23); button24=new JButton(-); c.gridx=3; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button24,c); con.add(button24); button25=new JButton(=); c.gridx=4; c.gridy=5; c.weightx = 1.0; c.

15、gridwidth=1; c.gridheight=0; gridbag.setConstraints(button25,c); con.add(button25); button26=new JButton(0); c.gridx=0; c.gridy=6; c.weightx = 1.0; c.gridwidth=2; gridbag.setConstraints(button26,c); con.add(button26); button27=new JButton(.); c.gridx=2; c.gridy=6; c.weightx = 1.0; c.gridwidth=1; gri

16、dbag.setConstraints(button27,c); con.add(button27); button28=new JButton(+); 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.addAct

17、ionListener(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.

18、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); but

19、ton28.addActionListener(this); setSize(300,350); Toolkit tl=this.getToolkit(); setLocation(tl.getScreenSize().width-this.getWidth()/2,(tl.getScreenSize().height-this.getHeight()/2); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Override public void actionPerformed(ActionEvent e)

20、/ TODO Auto-generated method stub String act=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().ch

21、arAt(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.se

22、tText(tj.getText()+act); else tj.setText(act); return; else tj.setText(tj.getText()+act); return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; if(act.equals(+)|act.equals(-)|act.equals(*)|act.equals(/)|act.equals(%)if(ch!=#) try

23、 num1=cacu(num1,ch,Float.parseFloat(tj.getText(); tj.setText(String.valueOf(num1); ch=act.charAt(0); can=true; return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; else try num1=Float.parseFloat(tj.getText(); ch=act.charAt(0); c

24、an=true; return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; if(act.equals() try float num=(float)Math.sqrt(Float.parseFloat(tj.getText(); tj.setText(String.valueOf(num); can=true; return; catch(NumberFormatException e1) JOptio

25、nPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; if(act.equals(1/) try float num=1/Float.parseFloat(tj.getText(); tj.setText(String.valueOf(num); can=true; return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE

26、); return; catch(ArithmeticException e1) JOptionPane.showMessageDialog(null, 除0错误!, 警告!,JOptionPane.ERROR_MESSAGE);return;if(act.equals(=) can=true; tryif(ch=#) return; float num=Float.parseFloat(tj.getText(); num1=cacu(num1,ch,num); tj.setText(String.valueOf(num1); ch=#; return; catch(NumberFormatE

27、xception e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; public float cacu(float a,char c,float b) float sum; 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); return 0; sum=a/b; break;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1