Java计算机程序
Java程序设计课程设计报告
Java简易计算器程序设计
专业 信息与计算科学
姓名 任航
班级 70803
学号 7080308
指导教师王薇
日期2011年4月12日
1.题目:Java简易计算器程序设计
2. 课程设计内容:
通过GUI界面的计算机程序,设计简易计算器,可以进行简单的加,减,乘运算。
3.功能简介
1.按下数字键在文本框上会显示数字,这是计算器最基本的功能;
2.上面一个文本框显示计算过程,下面一个文本框显示输入的数字
4.当按下的运算符号时前面已经按下过运算符号时,下面一个文本框显示上一个运算符号以及两个数之间的运算结果。
4.程度界面
5. 程序代码
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class calculator extends JFrame implements ActionListener
{
JTextField tf= new JTextField(25);
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton bPoint = new JButton(".");
JButton bDiv = new JButton("/");
JButton bMul = new JButton("*");
JButton bSub = new JButton("-");
JButton bAdd = new JButton("+");
JButton bCal = new JButton("=");
int op;
String s1;
String s2;
public calculator()
{
this.setTitle("计算器");
JPanel jpDisplay = new JPanel();
JPanel jpInput = new JPanel();
JPanel jpLeft = new JPanel();
JPanel jpRight = new JPanel();
tf.setText(" ");
tf.setHorizontalAlignment(JTextField.RIGHT);
jpDisplay.add(tf);
jpLeft.setLayout(new GridLayout(5, 1));
JPanel p2 = new JPanel();
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bPoint.addActionListener(this);
bDiv.addActionListener(this);
bMul.addActionListener(this);
bSub.addActionListener(this);
bAdd.addActionListener(this);
bCal.addActionListener(thi