JAVA编程 模拟科学计算器 要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功能:加、减、模拟科学计算器要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/28 02:16:10
JAVA编程 模拟科学计算器 要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功能:加、减、模拟科学计算器要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功

JAVA编程 模拟科学计算器 要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功能:加、减、模拟科学计算器要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功
JAVA编程 模拟科学计算器 要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功能:加、减、
模拟科学计算器
要求:界面模拟Windows中的计算器程序.
实现基本数学运算、函数等功能:加、减、乘、除.
实现要点:添加相关组件并进行按钮事件处理.
要求提交Application和Applet两个版本的程序.

JAVA编程 模拟科学计算器 要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功能:加、减、模拟科学计算器要求:界面模拟Windows中的计算器程序.实现基本数学运算、函数等功
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator implements ActionListener{
private double data1=0.0,data2=0.0;
private String t_content;
boolean number=false;
//key用来标示用户所按下的按钮即加减乘除
//key为0标示+,为1表示-,为2表示*,为3表示/
short key=-1;
//pkey的取值为-1~5
//pkey为-1表示第一次按下=按钮,为5说明不是第一次按下=按钮
//pkey为其他值0,1,2,3时分别代表+,-,*,/
short pkey=-1;
Frame frame=new Frame("Calculator");//定义标题为Calculator的窗体
TextField textfield=new TextField(30);//定义计算机
//定义backspace,ce,c0按钮
Button backspace=new Button("Backspace");
Button ce=new Button("CE");
Button c0=new Button("C");
//定义面板,其中backspace,ce,c0按钮和计算机兰textfeild放在面板p1中
//其余按钮放在面板p2中
Panel p1=new Panel();
Panel p2=new Panel();
//定义界面上的按钮数组,即除去backspace,ce,c0按钮的所有按钮
String names[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
Button bb[]=new Button[names.length];
public static void main(String[] args){
Calculator cal=new Calculator();
cal.go();
}
public void go(){
frame.setSize(300,200);
frame.setLayout(new BorderLayout());
Font fonts=new Font("楷体_GB2312",Font.PLAIN,12); // 第一页
//设置面板p1的字体和布局管理类型,将textfeild对象添加到面板中
p1.setFont(fonts);
p1.setLayout(new GridLayout(2,1,5,10));
textfield.setFont(fonts);
p1.add(textfield,null);
//backspace,ce,c0按钮注册addActionListener方法
backspace.addActionListener(this);
ce.addActionListener(this);
c0.addActionListener(this);
//将backspace,ce,c0按钮添加到p1面板中
p1.add(backspace,null);
p1.add(ce,null);
p1.add(c0,null);
//设置面板p2的布局管理器类型为GridLayout
p2.setLayout(new GridLayout(4,5,5,5));
//为按钮组中的按钮注册addActionListener方法
for(int i=0;i