



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class MyCalculater extends JFrame implements ActionListener private static final long serialVersionUID = 1L;/不管他 JPanel mainpanel = new JPanel(); /主面板 JPanel centerpanel = new JPanel(); /中心面板 JTextField txt = new JTextField(“); /主显示区 StringBuffer sb1 = new StringBuffer(); /参数一 StringBuffer sb2 = new StringBuffer(); /参数二 char sign; boolean flag = false; JButton buttons = new JButton16; buttons0 = new JButton(“1“); buttons1 = new JButton(“2“); buttons2 = new JButton(“3“); buttons3 = new JButton(“+“); buttons4 = new JButton(“4“); buttons5 = new JButton(“5“); buttons6 = new JButton(“6“); buttons7 = new JButton(“-“); buttons8 = new JButton(“7“); buttons9 = new JButton(“8“); buttons10 = new JButton(“9“); buttons11 = new JButton(“*“); buttons12 = new JButton(“0“); buttons13 = new JButton(“c“); buttons14 = new JButton(“=“); buttons15 = new JButton(“/“); centerpanel.setLayout(new GridLayout(4,4,8,8); for (int i = 0; i buttons.length; i+) centerpanel.add(buttonsi); buttonsi.addActionListener(this); public MyCalculater(int x,int y) this.setTitle(“我的计算器 “); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setContentPane(mainpanel); this.setResizable(false); BorderLayout bl = new BorderLayout(); mainpanel.setLayout(bl); mainpanel.add(txt,BorderLayout.NORTH); mainpanel.add(centerpanel,BorderLayout.CENTER); this.setBounds(x,y, 300, 240); this.setVisible(true); public static void main(String args) new MyCalculater(750, 200); Override public void actionPerformed(ActionEvent e) JButton jb = (JButton)e.getSource(); char c = jb.getText().charAt(0); switch(c) case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: addNum(c); break; case +: case -: case *: case /: signEvent(c); break; case c: MyCalculater.this.dispose(); new MyCalculater(MyCalculater.this.getLocation().x,MyCalculater.this.getLocation().y); break; case =: if(!flag)this.txt.setText(sb1.toString(); sb1 = new StringBuffer(getAnswer(); sb2 = new StringBuffer(); this.txt.setText(sb1.toString(); sign = =; flag = false; break; default: break; private void signEvent(char c) if(flag this.txt.setText(sb1.toString(); else flag = !flag; this.txt.setText(“); this.sign = c; private String getAnswer() double a = 0; if(sb1.length()!=0) a = Double.parseDouble(sb1.toString(); double b = 0; if(sb2.length()!=0) b = Double.parseDouble(sb2.toString(); double answer = 0; switch(sign) case +: answer = a+b; this.txt.setText(“); break; case -: answer = a-b; this.txt.setText(“); break; case *: answer = a*b; this.txt.setText(“); break; case /: answer = a/b; this.txt.setText(“); break; default : return null; this.sb1 = new StringBuffer(“); this.sb2 = new StringBuffer(“); ret
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 押题宝典教师招聘之《幼儿教师招聘》通关考试题库附参考答案详解(巩固)
- 2025年教师招聘之《小学教师招聘》考试题库附答案详解【b卷】
- 2026届南京市南师附中江宁分校化学九上期末考试试题含解析
- 2025年教师招聘之《幼儿教师招聘》考前冲刺模拟题库含答案详解(能力提升)
- 中式烹饪试题及答案
- 民航专业试题及答案
- 押题宝典教师招聘之《幼儿教师招聘》题库附答案详解【考试直接用】
- 江西省九江市修水县2026届九年级化学第一学期期中质量跟踪监视试题含解析
- 2026年高考试题汇编政治专题26树立科学思维观念
- 推拿治疗学复习试题及完整答案详解一套
- 新高考人教版高中化学必修一全套课件
- 《大学美育》第1章
- 城里来了大恐龙
- 体育原理完整版
- 门诊发药交待注意事项
- 中小学心理健康教育指导纲要考试试题及答案(整理)
- GA/T 115-2020道路交通拥堵度评价方法
- 食品试验设计与统计分析
- 小学二年级上册语文全册课件
- 公安民警心理压力应对Baidu课件
- 会议电视系统工程设计规范附条文说明
评论
0/150
提交评论