Java计算机程序Java计算机程序
Java程序设计课程设计报告
Java简易计算器程序设计
专业 信息与计算科学 姓名 任航
班级 70803 学号 7080308 指导教师 王薇
日期 2011年4月12日
1(题目:Java简易计算器程序设计
2. 课程设计内容:
通过GUI界面的计算机程序,设计简易计算器,可以进行简单的加,减,乘运算。
3.功能简介
1.按下数字键在文本框上会显示数字,这是计算器最基本的功能; 2.上面一个文本框显示计算过程,下面一个文本框显示输入的数字 4.当按下的运算符号时前面已经按下过运算符...
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(this);
p2.setLayout(new GridLayout(4, 4));
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bSub);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bAdd);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bDiv);
p2.add(bCal);
jpRight.setLayout(new BorderLayout());
jpRight.add(p2, BorderLayout.NORTH);
jpInput.setLayout(new BorderLayout());
jpInput.add(jpLeft, BorderLayout.WEST);
jpInput.add(jpRight, BorderLayout.CENTER);
Container pane = this.getContentPane();
pane.setSize(250, 200);
this.setBounds(100,100,200,200);
this.setLocation(250, 200);
this.setLayout(new BorderLayout());
pane.add(jpDisplay, BorderLayout.CENTER);
pane.add(jpInput, BorderLayout.SOUTH);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton tempB=(JButton)e.getSource();
String po=tempB.getLabel();
if(po.equals("+"))
{
opClean();
op=1;
}else if(po.equals("-"))
{
opClean();
op=2;
}else if(po.equals("*"))
{
opClean();
op=3;
}else if(po.equals("/"))
{
opClean();
op=4;
}else if(po.equals("="))
{
s2=tf.getText();
operate();
}
else
{
tf.setText(tf.getText()+po);
}
}
public void opClean()
{
s1=tf.getText();
tf.setText("");
}
private void operate()
{
double x1=Double.parseDouble(s1);
double y=Double.parseDouble(s2);
String output;
switch(op)
{
case 1:
output=String.valueOf(y+x1);
tf.setText(output);
break;
case 2:
x1=x1-y;
tf.setText(String.valueOf(x1));
break;
case 3:
x1=y*x1;
tf.setText(String.valueOf(x1));
break;
case 4:
if(y==0)
{
output="错误,分母不能为0";
tf.setText(output);
}
else
{
x1=x1/y;
tf.setText(String.valueOf(x1));
}
break;
}
}
}
class result
{
public static void main(String[] args)
{
new calculator();
}
}
6. 测试
9/3=3.0
1> 输入9
2> 输入3
3> 输入3.0
7. 总结
在完成课题的过程中也不断充实了自己,学习到了很多以前没有学习到的知识,收获很大。最大的收获是在弯完成过程中培养的解决问题的能力。
Java计算器设计使得我们对所学的专业课有了更为深刻的认识,使得知识得到了巩固和提高。
本文档为【Java计算机程序】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。