您好,欢迎来到华佗养生网。
搜索
您的当前位置:首页Java计算器实验报告

Java计算器实验报告

来源:华佗养生网
大连民族大学

计算机科学与工程学院 《Java语言》课程实验报告

实验题目: 计算器的设计与实现 课程名称: Java语言 实验类型:□演示性 □验证性 □操作性 ■设计性 □综合性

专业:网络工程 班级:**** 学生姓名*** 学号:***** 实验日期:2018年 4月23日

地点:计算机学院综合试验中心(I-303) 实验学时:4

实验成绩:

指导教师签字: 2018年 5月21日

一、 问题需求描述:Problem description

利用Java Swing设计计算器图形化界面,并实现事件的监听,以及计算器基本的加、减、乘、除、清空、输出运算结果的操作,学习使用各种图形化界面的控件,容器,布局等,学习对按钮事件的处理等。以及对非法输入的处理。

二、 设计说明:Design notes

本计算器,界面方面总体采用BorderLayout布局,显示输入输入的文本框和清除按钮采用FlowLayout布局,容器放在BorderLayout的North;0-9和各种运算符号还有小数点采用GridLayout布局,容器放在BorderLayou的Center;事件监听和处理通过实现addActioListener监视器接口和ItemEvent事件接口,从而对每一个按钮实现事件的监听和处理;计算方面,调用ScriptEngineManager和ScriptEngine两个类,直接通过eval函数直接把输入文本当作JS代码执行,输出执行结果,这样报错也会直接实现,另把文本框设置不允许直接从键盘输入以防止出现安全事件。 最后通过转化成JS代码运算得到启发,使用类似方法,我还实现了调用VB代码实现计算器实时读出键盘输入,发出声音。

三、 系统实现:system implementation

1.程序界面展示

四、 总结体会: summary and experience

实验达到了预期的目标。通过此次的编程,我学会了用Java语言编写简

单的软件,增强了我对学习Java的兴趣。编程过程中,总体设计主要是窗口与组件的应用,具体执行则是利用Java语言设计算法,而难点也在算法的设计上。虽然程序大体上符合要求,但由于第一次编写这么复杂的程序,程序仍然存在一-些漏洞,譬如由于未进行异常处理,当连续输入运算符是程序会出错,还有若一直按小数点的话会得到一连串的小数点。由于最近忙着考试没有时间修改,考试结束后一定认真修改这些问题。我觉得在编程时应该注意排版对齐,这样不论是自己检查程序还是别人阅读程序都会更加方便。如果能添加备注的话以后阅读程序会更容易些。在以后的编程中我会吸取经验,养成认真、严谨的编程习惯。

附:程序清单

1.

import javax.swing.JFrame;

public class Calculator {

public static void main(String[] paramArrayOfString) {

CalculatorFrame localCalculatorFrame = new CalculatorFrame(); localCalculatorFrame.setSize(800, 800); localCalculatorFrame.setVisible(true); } } 2

import java.awt.Container;

import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame;

class CalculatorFrame extends JFrame {

public CalculatorFrame() {

setTitle(\"Calculator\"); setSize(200, 200);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent paramAnonymousWindowEvent) {

System.exit(0); } });

Container localContainer = getContentPane(); localContainer.add(new CalculatorPanel()); } }

3. import java.awt.BorderLayout; 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.ActionListener;

import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextField;

class CalculatorPanel extends JPanel

implements ActionListener {

private JTextField display;

public CalculatorPanel() {

setLayout(new BorderLayout()); this.display = new JTextField(); this.display.setEditable(false); this.display.setFont(this.font);

this.display.setBackground(Color.orange);

add(this.display, \"North\");

JPanel localJPanel = new JPanel();

localJPanel.setLayout(new GridLayout(4, 4, 5, 5));

String[] arrayOfString = { \"7\\"1\

for (int i = 0; i < arrayOfString.length; i++) { addButton(localJPanel, arrayOfString[i]); }

add(localJPanel, \"Center\"); }

private void addButton(Container paramContainer, String paramString) {

JButton localJButton = new JButton(paramString); localJButton.setFont(this.font); paramContainer.add(localJButton);

localJButton.addActionListener(this); }

public void actionPerformed(ActionEvent paramActionEvent) {

String str = paramActionEvent.getActionCommand();

if ((('0' <= str.charAt(0)) && (str.charAt(0) <= '9')) || (str.equals(\".\")))

{

if (this.start) {

this.display.setText(str); } else {

this.display.setText(this.display.getText() + str); }

this.start = false; }

else if (this.start) {

if (str.equals(\"-\")) {

this.display.setText(str); this.start = false; } else {

this.op = str; } } else {

double d = Double.parseDouble(this.display.getText()); calculate(d); this.op = str; this.start = true; } }

public void calculate(double paramDouble) {

if (this.op.equals(\"+\")) { this.arg += paramDouble;

} else if (this.op.equals(\"-\")) { this.arg -= paramDouble;

} else if (this.op.equals(\"*\")) { this.arg *= paramDouble;

} else if (this.op.equals(\"/\")) { this.arg /= paramDouble;

} else if (this.op.equals(\"=\")) { this.arg = paramDouble; }

this.display.setText(\"\" + this.arg); }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo7.cn 版权所有 湘ICP备2022005869号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务