java计算器代码.doc

上传人:11****ws 文档编号:2973965 上传时间:2019-05-13 格式:DOC 页数:6 大小:42.50KB
下载 相关 举报
java计算器代码.doc_第1页
第1页 / 共6页
java计算器代码.doc_第2页
第2页 / 共6页
java计算器代码.doc_第3页
第3页 / 共6页
java计算器代码.doc_第4页
第4页 / 共6页
java计算器代码.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

1、 /计算器 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends WindowAdapter implements ActionListener JFrame frame; JTextField show; JButton bc,ce,c,ab,jia,jian,cheng,chu,equ,point,sqrt,zf,bfh,ds; /退格,复位 ,清空,关于,加,减,乘,除,等,点, 2,加减,百分之,倒数 JButton b=new JButton1

2、0;/保存数字 JDialog about; final int length=30; int i=0,j=0,action=0,p=0; /记录数字键连续时间次数 double num=0,getValue;/记录计算结果的值 ,保存值 public Calculator() frame=new JFrame(“计算器 “); frame.addWindowListener(this); frame.setSize(360, 230); frame.setLocation(380,260); frame.setLayout(new FlowLayout(FlowLayout.CENTER);

3、 frame.setResizable(false); show=new JTextField(31); show.setText(“0“); show.setHorizontalAlignment(SwingConstants.RIGHT); show.setEnabled(false); frame.add(show); Panel dispTop=new Panel(); frame.add(dispTop); dispTop.setLayout(new GridLayout(1,4,3,3); bc=new JButton(“BACK“); bc.addActionListener(t

4、his); dispTop.add(bc); ce=new JButton(“CE“); ce.addActionListener(this); dispTop.add(ce); c=new JButton(“C“); c.addActionListener(this); dispTop.add(c); ab=new JButton(“ABOUT“); ab.addActionListener(this); dispTop.add(ab); /about弹出框 about=new JDialog(frame,“关于计算器 “,true); about.addWindowListener(thi

5、s); JLabel label=new JLabel(“http:/“,JLabel.CENTER); about.add(label); about.setSize(200,100); about.setLocation(500,300); /数据显示区 Panel dispMain=new Panel(); frame.add(dispMain); dispMain.setLayout(new GridLayout(1,2,10,10); /按钮左面板 Panel dispLeft=new Panel(); frame.add(dispLeft); dispLeft.setLayout(

6、new GridLayout(4,3,3,3);/四行三列 /按钮右面板 Panel dispRight=new Panel(); frame.add(dispRight); dispRight.setLayout(new GridLayout(4,2,3,3);/四行两列 for(int l=9;l=0;l-) bl=new JButton(String.valueOf(l); dispLeft.add(bl); bl.addActionListener(this); zf=new JButton(“+/-“); zf.addActionListener(this); dispLeft.ad

7、d(zf); point=new JButton(“.“); point.addActionListener(this); dispLeft.add(point); chu=new JButton(“/“); chu.addActionListener(this); dispRight.add(chu); sqrt=new JButton(“sqrt“); sqrt.addActionListener(this); dispRight.add(sqrt); cheng=new JButton(“*“); cheng.addActionListener(this); dispRight.add(

8、cheng); bfh=new JButton(“%“); bfh.addActionListener(this); dispRight.add(bfh); jian=new JButton(“-“); jian.addActionListener(this); dispRight.add(jian); ds=new JButton(“1/x“); ds.addActionListener(this); dispRight.add(ds); jia=new JButton(“+“); jia.addActionListener(this); dispRight.add(jia); equ=ne

9、w JButton(“=“); equ.addActionListener(this); dispRight.add(equ); frame.setVisible(true); /计算器抽象会报错 public void actionPerformed(ActionEvent e) getValue=Double.valueOf(show.getText().doubleValue(); for(int k=0;k1) show.setText(“); for(int t=0;tstr.length()-1;t+) char c=str.charAt(t); show.setText(show

10、.getText()+c); else show.setText(“0“); else if(e.getSource()=ab) about.setVisible(true); else if(e.getSource()=sqrt) num=Math.sqrt(getValue); show.setText(String.valueOf(num); j+;i=0;/ else if(e.getSource()=ds) num=1/getValue; show.setText(String.valueOf(num); j+;i=0;/ else if(e.getSource()=bfh) num

11、=getValue/100; show.setText(String.valueOf(num); j+;i=0; else if(e.getSource()=zf) String str=show.getText(); char c=str.charAt(0); if(c=-) show.setText(“); for(int t=1;tstr.length();t+) show.setText(show.getText()+str.charAt(t); else if(getValue!=0) show.setText(“-“+show.getText(); i=0; /windows关闭时调用此方法 public void windowClosing(WindowEvent e) if(e.getSource()=about) about.setVisible(false); else if(e.getSource()=frame) System.exit(0); public static void main(String args) Calculator c1=new Calculator();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。