广东金融学院实验报告
课程名称:Java高级程序设计
实验编号及实验名称:实验一 计算器
系 别:计科系
姓 名:
学 号:
班 级:
实验地点:实验楼504
实验日期:20##-11-8
实验时数:6
指导老师:
同组其他成员:无
成 绩:
一、实验目的
1、巩固java开发基础
2、学习Java图形界面开发技术
二、实验环境
1.使用软件:Windows XP、jdk1.6
2.实验设备:微型计算机
三、实验内容及要求
参考书本87页程序Calculator.java,以此为基础扩展其功能,设计自己的计算器。
四、实验结果(附程序运行效果图)
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField tR = null;
private JPanel p = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JButton jButton4 = null;
private JButton jButton5 = null;
private JButton jButton6 = null;
private JButton jButton7 = null;
private JButton jButton8 = null;
private JButton jButton9 = null;
private JButton jButton10 = null;
private JButton jButton11 = null;
private JButton jButton12 = null;
private JButton jButton13 = null;
private JButton jButton14 = null;
private JButton jButton15 = null;
private double result=0;
private String lastCommand="="; // @jve:decl-index=0:
private boolean start=true;
private JButton jButton16 = null;
private JButton jButton17 = null;
private JButton jButton18 = null;
private JButton jButton19 = null;
/**
* This is the default constructor
*/
public Calculator() {
super();
initialize();
}
private void initialize() {
this.setSize(290, 190);
this.setResizable(false);
this.setLocation(new Point(100, 300));
this.setContentPane(getJContentPane());
this.setTitle("计算器");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getTR(), null);
jContentPane.add(getP(), null);
}
return jContentPane;
}
/**
* This method initializes tR
*
* @return javax.swing.JTextField
*/
private JTextField getTR() {
if (tR == null) {
tR = new JTextField();
tR.setBounds(new Rectangle(0, 0, 284, 22));
tR.setComponentOrientation(ComponentOrientation.UNKNOWN);
tR.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
char key=e.getKeyChar();
if(key=='0'||key=='1'||key=='2'||key=='3'||key=='4'||key=='5'
||key=='6'||key=='7'||key=='8'||key=='9'||key=='.'){
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText());
}
else if(key=='+'||key=='-'||key=='*'||key=='/'||key=='='){
if(start){
if(key=='-'){
tR.setText(String.valueOf(key));
start=false;
}else
lastCommand=String.valueOf(key);
}else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=String.valueOf(key);
start=true;
}
}
}
});
}
return tR;
}
/**
* This method initializes p
*
* @return javax.swing.JPanel
*/
private JPanel getP() {
if (p == null) {
GridLayout gridLayout = new GridLayout(5,4);
p = new JPanel();
p.setLayout(gridLayout);
p.setBounds(new Rectangle(0, 21, 284, 141));
p.add(getJButton16(), null);
p.add(getJButton17(), null);
p.add(getJButton18(), null);
p.add(getJButton19(), null);
p.add(getJButton2(), null);
p.add(getJButton9(), null);
p.add(getJButton13(), null);
p.add(getJButton15(), null);
p.add(getJButton14(), null);
p.add(getJButton11(), null);
p.add(getJButton10(), null);
p.add(getJButton7(), null);
p.add(getJButton6(), null);
p.add(getJButton1(), null);
p.add(getJButton(), null);
p.add(getJButton3(), null);
p.add(getJButton4(), null);
p.add(getJButton5(), null);
p.add(getJButton8(), null);
p.add(getJButton12(), null);
}
return p;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("6");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+input);
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("5");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("+");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
lastCommand=input;
}
else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=input;
start=true;
tR.setText("");
}
}
}
);
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setText(".");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton3;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if (jButton4 == null) {
jButton4 = new JButton();
jButton4.setText("1");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton4;
}
/**
* This method initializes jButton5
*
* @return javax.swing.JButton
*/
private JButton getJButton5() {
if (jButton5 == null) {
jButton5 = new JButton();
jButton5.setText("2");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton5;
}
/**
* This method initializes jButton6
*
* @return javax.swing.JButton
*/
private JButton getJButton6() {
if (jButton6 == null) {
jButton6 = new JButton();
jButton6.setText("4");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton6;
}
/**
* This method initializes jButton7
*
* @return javax.swing.JButton
*/
private JButton getJButton7() {
if (jButton7 == null) {
jButton7 = new JButton();
jButton7.setText("0");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+input);
}
});
}
return jButton7;
}
/**
* This method initializes jButton8
*
* @return javax.swing.JButton
*/
private JButton getJButton8() {
if (jButton8 == null) {
jButton8 = new JButton();
jButton8.setText("3");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton8;
}
/**
* This method initializes jButton9
*
* @return javax.swing.JButton
*/
private JButton getJButton9() {
if (jButton9 == null) {
jButton9 = new JButton();
jButton9.setText("-");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText(input);
start=false;
}
else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=input;
start=true;
tR.setText("");
}
}
});
}
return jButton9;
}
/**
* This method initializes jButton10
*
* @return javax.swing.JButton
*/
private JButton getJButton10() {
if (jButton10 == null) {
jButton10 = new JButton();
jButton10.setText("9");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton10;
}
/**
* This method initializes jButton11
*
* @return javax.swing.JButton
*/
private JButton getJButton11() {
if (jButton11 == null) {
jButton11 = new JButton();
jButton11.setText("8");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton11;
}
/**
* This method initializes jButton12
*
* @return javax.swing.JButton
*/
private JButton getJButton12() {
if (jButton12 == null) {
jButton12 = new JButton();
jButton12.setText("=");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
lastCommand=input;
}
else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=input;
start=true;
tR.setText(""+result);
}
}
});
}
return jButton12;
}
/**
* This method initializes jButton13
*
* @return javax.swing.JButton
*/
private JButton getJButton13() {
if (jButton13 == null) {
jButton13 = new JButton();
jButton13.setText("*");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
lastCommand=input;
}
else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=input;
start=true;
tR.setText("");
}
}
});
}
return jButton13;
}
/**
* This method initializes jButton14
*
* @return javax.swing.JButton
*/
private JButton getJButton14() {
if (jButton14 == null) {
jButton14 = new JButton();
jButton14.setText("7");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
tR.setText("");
start=false;
}
tR.setText(tR.getText()+ input);
}
});
}
return jButton14;
}
/**
* This method initializes jButton15
*
* @return javax.swing.JButton
*/
private JButton getJButton15() {
if (jButton15 == null) {
jButton15 = new JButton();
jButton15.setText("/");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
lastCommand=input;
}
else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=input;
start=true;
tR.setText("");
}
}
});
}
return jButton15;
}
public void calculator(double x){
if(lastCommand.equals("+"))
result += x;
else if(lastCommand.equals("-"))
result -=x;
else if(lastCommand.equals("*"))
result *=x;
else if(lastCommand.equals("/"))
result /=x;
else if(lastCommand.equals("squa"))
result=Math.pow(Double.parseDouble(tR.getText()),2);
else if(lastCommand.equals("sqrt"))
result=Math.sqrt(x);
else if(lastCommand.equals("="))
result =x;
}
/**
* This method initializes jButton16
*
* @return javax.swing.JButton
*/
private JButton getJButton16() {
if (jButton16 == null) {
jButton16 = new JButton();
jButton16.setText("squa");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
lastCommand=input;
}
else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=input;
start=true;
tR.setText("");
}
}
});
}
return jButton16;
}
/**
* This method initializes jButton17
*
* @return javax.swing.JButton
*/
private JButton getJButton17() {
if (jButton17 == null) {
jButton17 = new JButton();
jButton17.setText("sqrt");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String input=e.getActionCommand();
if(start){
lastCommand=input;
}
else{
calculator(Double.parseDouble(tR.getText()));
lastCommand=input;
start=true;
tR.setText("");
}
}
});
}
return jButton17;
}
/**
* This method initializes jButton18
*
* @return javax.swing.JButton
*/
private JButton getJButton18() {
if (jButton18 == null) {
jButton18 = new JButton();
jButton18.setText("C");
jButton18.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
tR.setText("");
}
});
}
return jButton18;
}
/**
* This method initializes jButton19
*
* @return javax.swing.JButton
*/
private JButton getJButton19() {
if (jButton19 == null) {
jButton19 = new JButton();
jButton19.setText("CE");
jButton19.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String a=tR.getText();
int i=a.length();
i--;
String str=a.substring(0,i);
tR.setText(str);
}
});
}
return jButton19;
}
} // @jve:decl-index=0:visual-constraint="10,10"
(通过图片、文字方式详细描述计算器功能及其操作过程)
Squa 是数字的平方,用法:先按squa,再按要平方的数据。
Sqrt是数字开方,用法:先按sqrt,再按要开方的数据。
C是清除键,将文本框的数据清空。
CE是退格键
计算过程:先按数字,再按操作符号,再按数字,最后按“=”。
五、实验总结(包括心得体会、问题回答及实验改进意见,可附页)
这个实验使我深深明白上课认真听讲的重要性,虽然计算器是一个简单的基础的程序,但还是用了我非常多的时间,还要边做边请教别人,计算器功能还不是很完善,以后在编程序的方面要加倍努力!
六、教师评语
1)完成所有规定的实验内容,实验步骤正确,结果正确;
2)完成绝大部分规定的实验内容,实验步骤正确,结果正确;
3)完成大部分规定的实验内容,实验步骤正确,结果正确;
4)基本完成规定的实验内容,实验步骤基本正确,所完成的结果基本正确;
5)未能很好的完成规定的实验内容或实验步骤不正确或结果不正确。
6)其他:
评定等级:优秀 良好 中等 及格 不及格
教师签名:
年 月 日
附:程序源代码