java简单界面计算器.docx_第1页
java简单界面计算器.docx_第2页
java简单界面计算器.docx_第3页
java简单界面计算器.docx_第4页
java简单界面计算器.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

package calculator;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;/* * * author 某某某 * */public class Calculator extends JFrame implements ActionListener /设计按钮数组1包含数字键和删除键DEL和清屏键,小数点。 JButton but1 = new JButton(1),new JButton(2),new JButton(3),new JButton(4),new JButton(5),new JButton(6),new JButton(7),new JButton(8),new JButton(9),new JButton(.),new JButton(0),new JButton(del);/设计按钮数组2包含加减乘除,开根号,取余,等于键,平方,三次方。JButton but2 = new JButton(+),new JButton(-),new JButton(*),new JButton(/),new JButton(2),new JButton(3),new JButton(%),new JButton(sqrt),new JButton(exit),new JButton(=),new JButton(AC) ;/创建一个单行文本JTextField text = new JTextField(25);/创建五个面板JPanel jp1 = new JPanel();JPanel jp2 = new JPanel();JPanel jp3 = new JPanel();JPanel jp4 = new JPanel();JPanel jp5 = new JPanel();/创建有关状态变量boolean clicked = true;/是否有小数点boolean clear = true;/是否应该清屏double num1,num2;/两次输入的数字String sign;/用户点击的运算符号/主要界面设计public Calculator()this.setVisible(true);this.setTitle(my calculator);this.setSize(300,250);this.setLocation(400, 400);text.setHorizontalAlignment(JTextField.RIGHT);/设计各面板的格式jp1.setLayout(new GridLayout(1,1);jp2.setLayout(new GridLayout(4,3);jp3.setLayout(new GridLayout(4,1);jp4.setLayout(new GridLayout(4,1);jp5.setLayout(new GridLayout(1,1);/设置整体布局/面板一种只有一个单行文本 放在NORTH。this.add(jp1,BorderLayout.NORTH);jp1.add(text); text.setEditable(false); /面板二是数字面板还包括小数点和Del键,放在CENTERthis.add(jp2,BorderLayout.CENTER);/循环向面板二中添加按钮,并添加监听者for (int i = 0; i but1.length; i+) jp2.add(but1i);but1i.addActionListener(this);/面板三是加减乘除,放在EASTthis.add(jp3,BorderLayout.EAST);/循环向面板中添加按钮,并添加监听者for (int i = 0; i 4; i+) jp3.add(but2i);but2i.addActionListener(this);/面板四是平方,三次方,取余,开方,放在WEST。this.add(jp4,BorderLayout.WEST);/循环向里面添加按钮,并添加监听者for (int i = 4; i 8; i+) jp4.add(but2i);but2i.addActionListener(this);/面板五是exit键,等于键,和清屏键,放在SOUTH。this.add(jp5,BorderLayout.SOUTH);/循环向其中添加按钮,并添加监听者for (int i = 8; i but2.length; i+) jp5.add(but2i);but2i.addActionListener(this);/监听设置public void actionPerformed(ActionEvent e) /del键if(e.getActionCommand() = del)String s = text.getText();text.setText(null);for (int i = 0; i s.length()-1; i+) char a = s.charAt(i);text.setText(text.getText() + a);/AC键(清零)if(e.getActionCommand() = AC)text.setText(null);clear = true;if(e.getActionCommand() = 0)if(clear = false)text.setText(null);text.setText(text.getText()+0);clear = true;if(e.getActionCommand() = 1)if(clear = false)text.setText(null);text.setText(text.getText()+1);clear = true;if(e.getActionCommand() = 2)if(clear = false)text.setText(null);text.setText(text.getText()+2);clear = true;if(e.getActionCommand() = 3)if(clear = false)text.setText(null);text.setText(text.getText()+3);clear = true;if(e.getActionCommand() = 4)if(clear = false)text.setText(null);text.setText(text.getText()+4);clear = true;if(e.getActionCommand() = 5)if(clear = false)text.setText(null);text.setText(text.getText()+5);clear = true;if(e.getActionCommand() = 6)if(clear = false)text.setText(null);text.setText(text.getText()+6);clear = true;if(e.getActionCommand() = 7)if(clear = false)text.setText(null);text.setText(text.getText()+7);clear = true;if(e.getActionCommand() = 8)if(clear = false)text.setText(null);text.setText(text.getText()+8);clear = true;if(e.getActionCommand() = 9)if(clear = false)text.setText(null);text.setText(text.getText()+9);clear = true;/小数点键if(e.getActionCommand() = .)clicked = true;for (int i = 0; i text.getText().length(); i+) if(. = text.getText().charAt(i)clicked = false;break;if(clicked = true)text.setText(text.getText()+.);/加减乘除键if(e.getActionCommand() = +)if(clear = true)num1 = Double.parseDouble(text.getText();text.setText(null);text.setText(text.getText()+);sign = +;clear = false;if(e.getActionCommand() = -)num1 = Double.parseDouble(text.getText();sign = -;clear = false;if(e.getActionCommand() = *)num1 = Double.parseDouble(text.getText();sign = *;clear = false;if(e.getActionCommand() = /)num1 = Double.parseDouble(text.getText();sign = /;clear = false;/等于键if(e.getActionCommand() = =) num2 = Double.parseDouble(text.getText();text.setText(null);if(sign = +)text.setText(num1 + num2+);if(sign = -)text.setText(num1 - num2+);if(sign = *)text.setText(num1 * num2+);if(sign = /)if(num2 = 0)text.setText(除数不能为零);elsetext.setText(num1 / num2+);if(sign = %)if(num2 = 0)text.setText(error!);elsetext.setText(num1 % num2+);clear = false;if(e.getActionCommand() = sqrt)String s = text.getText();if(s.charAt(0)=-)text.setText(根号下的数不能小于零);elsetext.setText(Double.toString(Math.sqrt(Double.parseDouble(text.getText();clear = false;if(e.getActionCommand() = %)num1 = Double.parseDouble(text.getText();sign = %;clear = false;if(e.getActionCommand() = 2)num1 = Double.parseDouble(text.get

温馨提示

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

评论

0/150

提交评论