计算器的java代码_第1页
计算器的java代码_第2页
计算器的java代码_第3页
计算器的java代码_第4页
计算器的java代码_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.F ont;import java.awt.GridLayout;import java.awt.eve nt.Act ion Eve nt;import java.awt.eve nt.Act ion Liste ner;import javax.swi ng.JButt on;import javax.swi ng.JFrame;import javax.swi ng.JPa nel;import jav

2、ax.swi ng.JTextField;public class CalculatorB impleme nts Acti on Liste ner private JFrame frame;private JTextField field;private JButton allButtons;private JButton clearButton;/构造初始化成员变量public CalculatorB() frame = new JFrame(Calculator v1.1);field = new JTextField(25);allButto ns = new JButto n16;

3、Stri ng str = 123+456-789*0.=/;for (int i = 0; i 16; i+) allButt on si = new JButt on( str.substri ng(i, i + 1);clearButton = new JButto n( CLEAR);/调用界面的初始化方法ini t();/调用设置文本框的字体和颜色方法setFo ntAn dColor();addEve ntHa ndler();public void addEve ntHa ndler() for (int i = 0; i allButt ons.len gth; i+) all

4、Butt on si.addAct ion Liste ner(this);clearButt on .addAct ion Liste ner(this);private String op = null;private String m = null;private String n = nuII;private boolea n cls = false;private void setResult() if (field.getText().equals() cis = true;m = n ull;n = n ull; else if (m != n ull) n = field.ge

5、tText();if (op.equals(+) field.setText(Double.parseDouble(m) + Double.parseDouble( n)+ ); else if (op.equals(-) field.setText(Double.parseDouble(m) - Double.parseDouble( n)+ ); else if (op.equals(*) field.setText(Double.parseDouble(m) * Double.parseDouble( n)+ ); else if (op.equals(/) field.setText(

6、Double.parseDouble(m) / Double.parseDouble( n)+ );cls = true;m = n ull;n = n ull;public void actio nPerformed(Actio nEvent e) Stri ng str = e.getActio nComma nd();得到被点的按钮名称if (0123456789.indexOf(str) != -1) if (cis) field.setText(”);cis = false;field.setText(field.getText() + str); else if (+-*/.ind

7、exOf(str) != -1) if (field.getText().equals() op = str; else if (m = n ull) m = field.getText(); op = str;field.setText(”); else if (m != n ull) setResult();m = field.getText(); op = str; else if (str.equals(=) setResult(); else if (str.equals(CLEAR) field.setText(”);m = n ull;n = n ull;public void

8、in it() frame.setLayout (new BorderLayout();JPa nel n orthPa nnel = new JPa nel();JPa nel cen terPa nnel = new JPa nel();JPa nel southPa nnel = new JPa nel();no rthPa nn el.setLayout (new FlowLayout();cen terPa nn el.setLayout (new GridLayout(4, 4);southPa nn el.setLayout (new FlowLayout();n orthPa

9、nn el.add(field);southPa nn el.add(clearButt on);for (int i = 0; i allButt ons.len gth; i+) cen terPa nn el.add(allButt on si);frame.add( no rthPa nn el, BorderLayout.NORTH);frame.add(southPa nn el, BorderLayout.SOUTH); frame.add(ce nterPa nn el, BorderLayout.CENTER);public void setFo ntAn dColor()

10、field.setFont(new Font( 宋体,Font.BOLD, 20);field.setForegrou nd(Color.PINK);field.setBackgrou nd( new Color(0x2a, 0x3b, 0x4f);public void showMe() frame.pack(); /系统调整大小-很重要/ frame.setSize(600, 400);/自定义窗体大小frame.setLocatio n(500, 400);/设置窗体的启动位置frame.setVisible(true);frame.setDefaultCloseOperati on(

11、3);public static void main( Stri ng args) new CalculatorB().showMe();When you are old and grey and full of sleep,And no ddi ng by the fire, take down this book,And slowly read, and dream of the soft lookYour eyes had once, and of their shadows deep;How many loved your mome nts of glad grace,And love

12、d your beauty with love false or true,But one man loved the pilgrim soul in you,And loved the sorrows of your cha nging face;And bending dow n beside the glow ing bars,Murmur, a little sadly, how love fledAnd paced upon the mountains overheadAnd hid his face amid a crowd of stars.The furthest dista

13、nee in the worldIs not betwee n life and deathBut whe n I sta nd in front of youYet you dont know thatI love you.The furthest dista nee in the worldIs not whe n I sta nd in front of youYet you cant see my loveBut whe n un doubtedly knowing the love from bothYet cannot be together.The furthest dista

14、nee in the worldIs not being apart while being in loveBut whe n I pla inly cannot resist the year ningYet prete nding you have n ever bee n in my heart.The furthest dista nee in the worldIs not struggli ng aga inst the tidesBut using on es in differe nt heartTo dig an un crossable riverFor the one who loves you.倚窗远眺,目光目光尽处必有一座山,那影影绰绰的黛绿色的影,是春天的 颜色。周遭流岚升腾,没露出那真实的面孔。面对那流转的薄雾,我会幻想,那 里有一个世外桃源。在天阶夜色凉如水的夏夜,我会静静地,静静地,等待一场 流星

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论