使用java写的一个简易的计算器,可以检测输入是否合法.doc_第1页
使用java写的一个简易的计算器,可以检测输入是否合法.doc_第2页
使用java写的一个简易的计算器,可以检测输入是否合法.doc_第3页
使用java写的一个简易的计算器,可以检测输入是否合法.doc_第4页
使用java写的一个简易的计算器,可以检测输入是否合法.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、/* 使用java编写的简易计算器,可以实现+,,/四则运算。注意: 括号没有作用,就是一个装饰。待日后有时间定会更新.DEL是退格,CLC是清空。并带有日期,输入语法检测功能 */import java。awt.BorderLayout;import java.awt。Button;import java。awt.Color;import java.awt.Container;import java.awt.Font;import java。awt.GridLayout;import java.awt.event.ActionEvent;import java。awt。event.Actio

2、nListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing。JOptionPane;import javax.swing.JPanel;import javax。swing.JScrollPane;import javax。swing。JTextArea;import javax.swing.JTextField;import javax。swing.ScrollPaneConstants;import javax。swing。Timer;public class Calculator ex

3、tends JFrame implements ActionListenerprivate String operandString = "”;public static Calculator cal;private JTextArea textArea;private JPanel panel;private Container container;private double operand = new double100; private int operandCount;private String operator = new String100; private int

4、operatorCount;private JScrollPane jScrollPane;public Calculator(String title)super(title);public Calculator()operand = new double200;operator = new String100;cal = new Calculator("计算器");cal.setTitle(”Calculator");container = cal.getContentPane();container.setBackground(null);cal.setBa

5、ckground(new Color(100, 250, 150));cal。setSize(320, 400);cal.setLocation(100, 100);container。setLayout(new GridLayout(0, 1, 2, 1));new DateApplication().getNewInstance("");showComponent();cal。setDefaultCloseOperation(EXIT_ON_CLOSE);cal.setResizable(false);cal.setVisible(true);private void

6、showComponent()/ container。add(new JTextArea(40,20),BorderLayout.NORTH);String textString = ”789+456-1230。/=";textArea = new JTextArea(1, 2);jScrollPane = new JScrollPane(textArea);System.out.println("jScrollPane=" + jScrollPane);textArea.setBackground(Color。CYAN);textArea.setForegrou

7、nd(Color.BLACK);textArea.setEditable(false);textArea.setFont(new Font(”, Font。BOLD, 30));textArea.setWrapStyleWord(true);textArea.setLineWrap(true);jScrollPane。setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);/ jScrollPane.setViewportView(textArea);container.add(jScrollPa

8、ne);panel = new JPanel();panel。setLayout(new GridLayout(5, 4));for (int i = 0; i textString.length(); i+)addButton(textString。substring(i, i + 1), panel);String leftBracket = ”(”;String rightBracket = ”)";String back = "DEL"String setNull = "CLC"/ panel.add(new JButton(leftB

9、racket));/ panel。add(new JButton(rigthBracke);/ panel.add(new JButton(back);/ panel.add(new JButton(setNull));addButton(leftBracket, panel);addButton(rightBracket, panel);addButton(back, panel);addButton(setNull, panel);container。add(panel);private void addButton(String str, JPanel panel2)/ TODO Aut

10、ogenerated method stubButton button = new Button(str);button.setFont(new Font("", Font。BOLD, 14));if (button.getLabel()。equals("。”)| button.getLabel()。equals(”)button.setFont(new Font("", Font.BOLD, 30));if (button.getLabel().equals(”-")button.setFont(new Font(”, Font.B

11、OLD, 24));button。setBackground(Color.GREEN);button。addActionListener(this);panel2。add(button);public static void main(String args)new Calculator();Overridepublic void actionPerformed(ActionEvent e)textArea.setForeground(Color。BLACK);String str = e。getActionCommand();/ System.out.println(str);/ if(op

12、erandString=nulloperandString.contains(s)if (operandString.contains(”=”)operandString = "”;textArea.setText(operandString);operand = new double200;operator = new String200;operandCount = 0;operatorCount = 0;if (str。equals(”(”) | str。equals(”)")return;if (str。equals("DEL”)if (operandSt

13、ring.length() > 0)operandString = operandString.substring(0,operandString。length() - 1);textArea.setText(operandString);return;if (str。equals(”CLC"))operandString = ”";textArea.setText(operandString);return;operandString = operandString + str;System.out.println(operandString);operandStr

14、ing.trim();textArea.setText(operandString);if (str。equals("=”)handleString(operandString);public void handleString(String alString)int beginIndex = 0;String temp = null;for (int i = 0; i alString.length(); i+)if (containsOperator(alString。substring(i, i + 1)operatoroperatorCount = alString.subs

15、tring(i, i + 1);operatorCount+;if (operatorCount = 1)temp = alString。substring(0, i);beginIndex = i;elsetemp = alString.substring(beginIndex + 1, i);beginIndex = i;checkLegal(temp);/ for (int i = 0; i operatorCount; i+)/ System.out。println(operatori);countResult();public boolean containsOperator(Str

16、ing substring)/ TODO Autogenerated method stubString str = ”+/=";if (str。contains(substring))return true;return false;public void checkLegal(String str)double tempDouble = 0;trytempDouble = Double。parseDouble(str);operandoperandCount = tempDouble;operandCount+;catch (Exception e)JOptionPane。sho

17、wMessageDialog(null, ”输入有错");operandString = operandString + "="textArea.setText(operandString);public void countResult()double tempResult;for (int i = 0; i < operatorCount - 1; i+)if (operatori.equals("”)tempResult = operandi operandi + 1;backFunc(tempResult, i);i;else if (op

18、eratori。equals(”/"))trytempResult = operandi / operandi + 1;catch (Exception e)JOptionPane.showMessageDialog(null, "除数不能为0");operandString = operandString + "=”;return;backFunc(tempResult, i);i-;tempResult = operand0;for (int i = 0; i operandCount; i+)if (operatori。equals("+”)tempResult += operandi + 1;if (operatori。equals(”"))tempResult -= operandi + 1;if (operatori。equals(”=”))break;int similarInteger = Calculator。round(tempResult);textArea.setForeground(Color.RED);if (Math。abs(tempResult similarInteger) < M

温馨提示

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

评论

0/150

提交评论