资源描述:
《java简单计算器源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、简单计算器代码packagecalcultorthree;importjava.awt.BorderLayout;//导入边界布局管理器类importjava.awt.GridLayout;//导入网格布局管理器类importjava.awt.TextField;//导入文本区域类importjava.awt.event.ActionEvent;//导入事件类importjava.awt.event.ActionListener;//导入事件监听者类importjavax.swing.JButton;//导入按钮类importjavax.swing.JFrame;
2、//导入窗体importjavax.swing.JPanel;//导入面板/***本例实现了简单计算器代码,具备加减乘除和正弦功能,旨在抱砖引玉。熟悉java的同学,可以在此基础上实现更复杂的功能。*@authorFjsh*/publicclassCalcultorThree{//新建对象,在构造函数中进行初始化JFrameframe;//新建窗体对象JButtonbuttonzero,buttondot,buttonequal;//新建按钮“0”“.”“=”JButtonbuttonplus,buttonminus,buttonmultiple,buttonde
3、vision,buttonsin,buttontozero;//新建按钮“+”“-”“*”“/”“sin”和归零按钮JButtonbuttonone,buttontwo,buttonthree,buttonfour,buttonfive,buttonsix,buttonseven,buttoneight,buttonnine;//新建数字按钮“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”JPanelpanelwest,panelcenter,paneleast;//新建三个面板TextFieldtf;//新建文本区域对象publicCalcultor
4、Three(){//初始化对象tf=newTextField(30);//构造空文本字段,字符宽度为30frame=newJFrame("CalculatorThree");//构造窗体对象,名称为“CalculatorThree”panelcenter=newJPanel();//构造面板,放到窗体中央panelwest=newJPanel();//构造面板,放到窗体西边paneleast=newJPanel();//构造面板,放到窗体东边Handleh=newHandle();//新建Handle类对象,Handle类为事件监听类//创建数字按钮对象,1、2、
5、3、4、5、6、7、8、9buttonone=newJButton("1");buttontwo=newJButton("2");buttonthree=newJButton("3");buttonfour=newJButton("4");buttonfive=newJButton("5");buttonsix=newJButton("6");buttonseven=newJButton("7");buttoneight=newJButton("8");buttonnine=newJButton("9");panelcenter.setLayout(newGrid
6、Layout(3,3));//设置面板布局为网格布局,3行3列//将数字按钮添加到中间面板panelcenter.add(buttonone);panelcenter.add(buttontwo);panelcenter.add(buttonthree);panelcenter.add(buttonfour);panelcenter.add(buttonfive);panelcenter.add(buttonsix);panelcenter.add(buttonseven);panelcenter.add(buttoneight);panelcenter.add(
7、buttonnine);//为10个按钮注册事件监听器buttonone.addActionListener(h);buttontwo.addActionListener(h);buttonthree.addActionListener(h);buttonfour.addActionListener(h);buttonfive.addActionListener(h);buttonsix.addActionListener(h);buttonseven.addActionListener(h);buttoneight.addActionListener(h);bu
8、ttonn