java课程设计报告_简单图形界面计算器的设计
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java 课程设计
简单图形界面计算器的设计
课程名称 Java程序设计
选题名称简单图形界面计算器的设计
专业
班级
姓名
学号
指导教师
简单图形界面计算器的设计
一、设计任务与目标
本次java程序设计我的设计任务是设计一个图形界面(GUI)的计算器应用程序并且能够完成简单的算术运算。
本次任务的基本要求是这个计算器应用程序可以完成十进制的加、减、乘、除、求倒、取余、开方运算,且有小数点、正负号、退格和清零功能。
而我要在此基础上添加一项千位符分隔符的功能,即以三位为一级,在输入的一串数字中每三位加入一个逗号,这项功能国际通用,并已经成为惯例,会计记账都用这种方法便于账目核算与管理。
GUI计算器设计的具体目标:
1.完成十进制的加、减、乘、除、求倒、取余和开方运算;
2.有小数点和正负号加入运算;
3.有退格、复位和清零的功能;
4.有千位符分隔符的功能,即在输入的一串数字中每三位加入一个逗号。
二、方案设计与论证
1.设计目标的总体分析
(1)设计目标的需求分析:计算器是现在一个普遍应用的工具,能够解决许多人工所无法计算的数据,节省大量宝贵的时间。
(2)设计目标的功能分析:实现计算器系统的功能,主要有两个功能模块:输入和输出。
(3)设计原则:基于计算器系统要具有适用性广、操作简便等特点,本系统预计要达到以下几个目标:①满足以上的基本功能要求;②能够在常见的计算机及其操作系统上运行。
2.设计的基本思路
利用GUI的界面设计,将整个大设计分为三块,分别是数据的输入,运算符
功能符的控制和数据的输入输出显示。
利用Swing控件,数据的输入由0~9这
10个按钮来表示,用“+”、“-”、“*”、“/”、“1/x”、“%”、“sqrt”这7个按钮
来表示加、减、乘、除、求倒、取余、开方运算,用“.”和“±”这2个按钮
来表示小数点和正负号,用“Back”、“CE”和“C”这3个按钮来表示退格、复
位和清零的功能,数据的输入输出显示由文本字段来表示。
将计算器的总体界面
设计好后,再将代码分别写入不同的按钮的源程序中。
我要完成的一项改进,即添加一个拥有千位符分隔符功能的按钮,按下这个按钮能够在输入的一串数字中每三位加入一个逗号并且显示出来。
我要在之前的
界面设计的基础上多添加一个按钮“$”来表示千位符分隔符,并且将功能代码
写入这个按钮的源程序中。
三、程序流程图,程序清单与调用关系
1. 程序流程图:
2. 程序清单
3. 程序的调用关系图
四、全部源程序清单
/* Calculator.java
GUI简单计算器
*/
package javacalculator;
/**
*
* @author */
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
flag = false;
d1 = 0;
d2 = 0;
op = "";
jTextField1.setText("0");
clipboard = getToolkit().getSystemClipboard();
}
private Clipboard clipboard;
private boolean flag;
private double d1;
private double d2;
private String op;
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
jButton23 = new javax.swing.JButton();
jButton24 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton4.setText("7");
jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("8");
jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("9");
jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("4");
jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("5");
jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("6");
jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt);
}
});
jButton10.setText("1");
jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt);
}
});
jButton11.setText("2");
jButton11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton11ActionPerformed(evt);
}
});
jButton12.setText("3");
jButton12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton12ActionPerformed(evt);
}
});
jButton13.setText("0");
jButton13.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton13ActionPerformed(evt);
}
});
jButton14.setText(".");
jButton14.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton14ActionPerformed(evt);
}
});
jButton15.setText("±");
jButton15.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});
jButton16.setText("/");
jButton16.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton16ActionPerformed(evt);
}
});
jButton17.setText("*");
jButton17.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton17ActionPerformed(evt);
}
});
jButton18.setText("-");
jButton18.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton18ActionPerformed(evt);
}
});
jButton19.setText("+");
jButton19.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton19ActionPerformed(evt);
}
});
jButton20.setText("$");
jButton20.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton20ActionPerformed(evt);
}
});
jButton21.setText("sqrt");
jButton21.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton21ActionPerformed(evt);
}
});
jButton22.setText("1/x");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});
jButton23.setText("=");
jButton23.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton23ActionPerformed(evt);
}
});
jButton24.setText("%");
jButton24.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton24ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(53, 53, 53)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.LEADING, false)
.addComponent(jButton20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_V ALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout .Alignment.TRAILING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton13,
javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ponentPlacem ent.RELATED)
.addComponent(jButton15,
javax.swing.GroupLayout.PREFERRED_SIZE, 49,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ponentPlacem ent.RELATED)
.addComponent(jButton14,
javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ponentPlacem ent.UNRELATED)
.addComponent(jButton19)
.addPreferredGap(ponentPlacem ent.RELATED)
.addComponent(jButton23,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, jPanel2Layout.createSequentialGroup()
.addComponent(jButton4,
javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(pone ntPlacement.RELATED)
.addComponent(jButton5,
javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(pone ntPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_V ALUE)
.addComponent(jButton6,
javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton10,
javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_V ALUE)
.addComponent(jButton7,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(pone ntPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment .LEADING, jPanel2Layout.createSequentialGroup()
.addComponent(jButton8,
javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(youtStyle .ComponentPlacement.RELATED)
.addComponent(jButton9,
javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment .LEADING, jPanel2Layout.createSequentialGroup()
.addComponent(jButton11,
javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(youtStyle .ComponentPlacement.RELATED)
.addComponent(jButton12,
javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addPreferredGap(ponentPlacem ent.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton17)
.addPreferredGap(pone ntPlacement.RELATED)
.addComponent(jButton21,
javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton18)
.addPreferredGap(pone ntPlacement.RELATED)
.addComponent(jButton22,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton16)
.addPreferredGap(pone ntPlacement.RELATED)
.addComponent(jButton24,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
.addGap(0, 85, Short.MAX_V ALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.BASELINE)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6)
.addComponent(jButton16)
.addComponent(jButton24))
.addPreferredGap(ponentPlacement.UNRELAT ED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton17)
.addComponent(jButton21))
.addPreferredGap(ponentPlacement.UNRELAT ED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.BASELINE)
.addComponent(jButton10)
.addComponent(jButton11)
.addComponent(jButton12)
.addComponent(jButton18)
.addComponent(jButton22))
.addPreferredGap(ponentPlacement.UNRELAT ED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.BASELINE)
.addComponent(jButton13)
.addComponent(jButton14)
.addComponent(jButton15)
.addComponent(jButton19)
.addComponent(jButton23))
.addPreferredGap(ponentPlacement.UNRELAT ED)
.addComponent(jButton20)
.addContainerGap(42, Short.MAX_V ALUE))
);
jButton1.setText("Back");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("CE");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("C");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupL ayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ponentPla cement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_V ALUE)
.addComponent(jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addComponent(jButton3,
javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 66, Short.MAX_V ALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addPreferredGap(ponentPlacement.UNRELAT ED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_V ALUE)
.addGap(7, 7, 7))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_V ALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_V ALUE)
);
pack();
}// </editor-fold>
/**数据的输入:
* 程序开始时,程序初始化文本框的内容为“0”,点击数字按钮,
* if语句判断,若文本框数据非0,则当下按的数字键对应的数字,作为字符串接入之前数据尾部,
* else,文本框显示当下按的数字键所对应的数字。
*/
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"4");
else
jTextField1.setText("4");
flag = false;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//if语句判定当前字符串是否为空,若不为空,则将字符串长度减1之后,再赋值给原字符串;否则,将0复制给原字符串。
String s = jTextField1.getText();
if(!s.equals(""))
s = s.substring(0,s.length()-1);
if(s.equals(""))
s = "0";
jTextField1.setText(s);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//复位操作
jTextField1.setText("0");
d1 = 0;
d2 = 0;
op = "";
flag = false;
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
//清零操作
jTextField1.setText("0");
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"7");
else
jTextField1.setText("7");
flag = false;
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"8");
else
jTextField1.setText("8");
flag = false;
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"9");
else
jTextField1.setText("9");
flag = false;
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"5");
else
jTextField1.setText("5");
flag = false;
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"6");
else
jTextField1.setText("6");
flag = false;
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"1");
else
jTextField1.setText("1");
flag = false;
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"2");
else
jTextField1.setText("2");
flag = false;
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"3");
else
jTextField1.setText("3");
flag = false;
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
if(flag == false&&!jTextField1.getText().equals("0"))
jTextField1.setText(jTextField1.getText()+"0");
else
jTextField1.setText("0");
flag = false;
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//正负号
double d = Double.parseDouble(jTextField1.getText());
d = 0 - d;
jTextField1.setText(""+d);
}
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//点号
if(jTextField1.getText().equals("")||flag == true)
jTextField1.setText("0.");
else
jTextField1.setText(jTextField1.getText()+".");
flag = false;
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//除号
d1 = Double.parseDouble(jTextField1.getText());
flag = true;
op = "/";
}
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//乘号
d1 = Double.parseDouble(jTextField1.getText());
flag = true;
op = "*";
}
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//减号
d1 = Double.parseDouble(jTextField1.getText());
flag = true;
op = "-";
}
//运算符的控制:当点击运算符控制按钮时,若连续点击多个运算符,则以最后点击的运算
符作为当下操作。
private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//加号
d1 = Double.parseDouble(jTextField1.getText());
flag = true;
op = "+";
}
private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//开方
double d = Double.parseDouble(jTextField1.getText());
jTextField1.setText(""+Math.sqrt(d));
}
private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//千位符分隔符
d1 = Double.parseDouble(jTextField1.getText());
flag = true;
op = "$";
DecimalFormat f = new DecimalFormat(",###");
jTextField1.setText(""+f.format(d1));
}
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//求倒数
double d = Double.parseDouble(jTextField1.getText());
if(d==0)
jTextField1.setText("0");
else
jTextField1.setText(""+(1/d));
}
//计算并输出结果:定义的两个Double型d1,d2,将前后两次输入数据强制转换为Double 型,
//接着调用op存储的运算符,计算出运算结果并显示。
private void jButton23ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(op.equals(""))
return;
d2 = Double.parseDouble(jTextField1.getText());
double result = 0;
if(op.equals("+"))
{
result = d1 + d2;
}
else if(op.equals("-"))
{
result = d1 - d2;
}
else if(op.equals("*"))
{
result = d1 * d2;
}
else if(op.equals("/"))
{
result = d1 / d2;
if(d2 == 0)
result = 0;
}
else if(op.equals("%"))
{
result = d1 % d2;
if(d2 == 0)
result = 0;
}
jTextField1.setText(""+result);
flag = true;
}
private void jButton24ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//求余
d1 = Double.parseDouble(jTextField1.getText());
flag = true;
op = "%";
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see /javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton23;
private javax.swing.JButton jButton24;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
五、程序运行的测试与分析
1.简单的运算:(以加法为例:123+456)
分析:计算的结果为579.0,为双精度型。
2.取余的运算:(以123%2为例)
分析:输出的结果如图所示123模2得到1.0,取余功能实现,所得结果也可再进行相应的计算,没有影响。
3.开方的运算:(以121为例)
分析:输出的结果如图所示121开方得到11.0,开方功能实现,所得结果也可再进行相应的计算,没有影响。
4.求倒数的运算:(以123为例)
分析:输出的结果如图所示123求倒得到0.008130081300813009,求倒数功能实现,所得结果也可再进行相应的计算,没有影响。
5.有小数点加入运算:(以123.45+567.89为例)
分析:输出的结果如图所示123.45+567.89得到691.34,实现了小数点的加入,所得结果也可再进行相应的计算,没有影响。
6.有正负号的加入(以666为例)
分析:输出的结果如图所示666点击“±”按钮后得到-666,实现了正负号的加入,所得结果也可再进行相应的计算,没有影响。
7. 退格的功能:(以1234567.89为例)
分析:输出的结果如图所示,本计算器退格键有一缺点,虽然能很好的实现退格,但不能很好的智能化的处理小数点的问题,因为小数点也是字符串的一部分。
8.复位和清零的功能:(以12345为例)
分析:输出的结果如图所示,实现了复位和清零的功能。
9. 千位符分隔符的功能:(以1234567890为例)
分析:输出的结果如图所示,实现了千位符分隔符的功能。
六、结论与心得
1.本次设计的GUI计算器基本运算没有问题,清零、正负号、求倒数、退格功能都能很好的实现,总体上完成了一个计算器的基本功能。
本次的java程序设计,不仅让我巩固了在课堂上学习的java编程知识,而且促使我找到相关的书籍辅导和拓展自己的思路,提高了我的程序分析能力和解决实际问题的能力。
2.本次程序设计我遇到的问题是不会编写有关千位符分隔符的程序代码,在查找了相关资料后我导入了一个名为“java.text.DecimalFormat”的包,调用了函数“DecimalFormat(",###")”。
3.本次程序设计仍有许多地方需要改进,比如该计算器还没能很好的实现连续计算的功能,必须每次按下等号按钮计算出结果后,再用产生的结果接着进行下一次的计算,改进的方法是在运算符上同时注册Result类,让运算符同时拥有计算结果的功能。
七、参考资料
[1] 朱庆生古平. 《Java程序设计》 .清华大学出版社.2011.5
[2] 李晋.《Java程序设计:基于JDK6和Net Beans实现》.清华大学出版社.2011.2
31。