求JAVA计算器模拟程序源代码

功能要求:该程序显示GUI用户界面,能实现整数的加、减、乘、除四则运算。
设计思路:该程序使用一个TextArea来输入参与计算运算数和运算符;十个Button来存放0~9共十个数字的按键,用以输入运算数;提供四个Button分别表示“+”、“-”、“*”、“/”的运算符。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class testZ extends JFrame implements ActionListener{
private JPanel jPanel1,jPanel2;
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;

public testZ(){
super("计算器");
setSize(300,240);
Container con=getContentPane();
con.setLayout(new BorderLayout());
jPanel1=new JPanel();
jPanel1.setLayout(new GridLayout(1,1));
jPanel2=new JPanel();
jPanel2.setLayout(new GridLayout(4,4));
resultField=new JTextField("0");
jPanel1.add(resultField);
con.add(jPanel1,BorderLayout.NORTH);
s1=new JButton(" 1 "); s1.addActionListener(this);
s2=new JButton(" 2 "); s2.addActionListener(this);
s3=new JButton(" 3 "); s3.addActionListener(this);
s4=new JButton(" 4 "); s4.addActionListener(this);
s5=new JButton(" 5 "); s5.addActionListener(this);
s6=new JButton(" 6 "); s6.addActionListener(this);
s7=new JButton(" 7 "); s7.addActionListener(this);
s8=new JButton(" 8 "); s8.addActionListener(this);
s9=new JButton(" 9 "); s9.addActionListener(this);
s0=new JButton(" 0 "); s0.addActionListener(this);
b1=new JButton(" + "); b1.addActionListener(this);
b2=new JButton(" - "); b2.addActionListener(this);
b3=new JButton(" * "); b3.addActionListener(this);
b4=new JButton(" / "); b4.addActionListener(this);
f1=new JButton(" . "); f1.addActionListener(this);
f2=new JButton(" = "); f2.addActionListener(this);
jPanel2.add(s1);
jPanel2.add(s2);
jPanel2.add(s3);
jPanel2.add(b1);
jPanel2.add(s4);
jPanel2.add(s5);
jPanel2.add(s6);
jPanel2.add(b2);
jPanel2.add(s7);
jPanel2.add(s8);
jPanel2.add(s9);
jPanel2.add(b3);
jPanel2.add(s0);
jPanel2.add(f1);
jPanel2.add(f2);
jPanel2.add(b4);
con.add(jPanel2,BorderLayout.CENTER);

}
public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText("0");
end=false;

}
if((resultField.getText()).equals("0")){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);

}
else{
//如果文本框的内容不为零,则在内容后面添加数字
str = resultField.getText() + s;
resultField.setText(str);

}
}

public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==s1)
num(1);
else if(e.getSource()==s2)
num(2);
else if(e.getSource()==s3)
num(3);
else if(e.getSource()==s4)
num(4);
else if(e.getSource()==s5)
num(5);
else if(e.getSource()==s6)
num(6);
else if(e.getSource()==s7)
num(7);
else if(e.getSource()==s8)
num(8);
else if(e.getSource()==s9)
num(9);
else if(e.getSource()==s0)
num(0);

//符号事件
else if(e.getSource()==b1)
sign(1);
else if(e.getSource()==b2)
sign(2);
else if(e.getSource()==b3)
sign(3);
else if(e.getSource()==b4)
sign(4);
//等号
else if(e.getSource()==f1){
str=resultField.getText();
if(str.indexOf(".")<=1){
str+=".";
resultField.setText(str);
}
}
else if(e.getSource()==f2){
num2=Double.parseDouble(resultField.getText());

if(add){
num1=num1 + num2;}
else if(sub){
num1=num1 - num2;}
else if(mul){
num1=num1 * num2;}
else if(div){
num1=num1 / num2;}
resultField.setText(String.valueOf(num1));
end=true;
}

}
public void sign(int s){
if(s==1){
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
num1=Double.parseDouble(resultField.getText());
end=true;
}
public static void main(String[] args){
testZ th1=new testZ();
th1.show();
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-01-10
package eversino.tdaims.mcc.webprovider.cam;

import java.applet.Applet;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JTextField;

/**
* 计算器
*
* @author capuchin
* @date Jan 10, 2008
*
*/
public class Calculator extends Applet

{
private static final long serialVersionUID = 1L;

private GridBagLayout layout;

private GridBagConstraints constraints;

private JTextField displayField;// 计算结果显示区

private String lastCommand;// 保存+,-,*,/,=命令

private double result;// 保存计算结果

private boolean start;// 判断是否为数字的开始

public Calculator()

{

layout = new GridBagLayout();

setLayout(layout);

constraints = new GridBagConstraints();

start = true;

result = 0;

lastCommand = "=";

displayField = new JTextField(20);

displayField.setHorizontalAlignment(JTextField.RIGHT);

constraints.gridx = 0;

constraints.gridy = 0;

constraints.gridwidth = 4;

constraints.gridheight = 1;

constraints.fill = GridBagConstraints.BOTH;

constraints.weightx = 100;

constraints.weighty = 100;

layout.setConstraints(displayField, constraints);

this.add(displayField);

ActionListener insert = new InsertAction();

ActionListener command = new CommandAction();

addButton("Backspace", 0, 1, 2, 1, insert);

addButton("CE", 2, 1, 1, 1, insert);

addButton("C", 3, 1, 1, 1, insert);

addButton("7", 0, 2, 1, 1, insert);

addButton("8", 1, 2, 1, 1, insert);

addButton("9", 2, 2, 1, 1, insert);

addButton("/", 3, 2, 1, 1, command);

addButton("4", 0, 3, 1, 1, insert);

addButton("5", 1, 3, 1, 1, insert);

addButton("6", 2, 3, 1, 1, insert);

addButton("*", 3, 3, 1, 1, command);

addButton("1", 0, 4, 1, 1, insert);

addButton("2", 1, 4, 1, 1, insert);

addButton("3", 2, 4, 1, 1, insert);

addButton("-", 3, 4, 1, 1, command);

addButton("0", 0, 5, 1, 1, insert);

addButton("+/-", 1, 5, 1, 1, insert);// 只显示"-"号,"+"没有实用价值

addButton(".", 2, 5, 1, 1, insert);

addButton("+", 3, 5, 1, 1, command);

addButton("=", 0, 6, 4, 1, command);

setSize(300, 300);

setVisible(true);

}

private void addButton(String label, int row, int column, int with,
int height, ActionListener listener)

{

JButton button = new JButton(label);

constraints.gridx = row;

constraints.gridy = column;

constraints.gridwidth = with;

constraints.gridheight = height;

constraints.fill = GridBagConstraints.BOTH;

button.addActionListener(listener);

layout.setConstraints(button, constraints);

this.add(button);

}

private class InsertAction implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

String input = event.getActionCommand();

if (start)

{

displayField.setText("");

start = false;

if (input.equals("+/-"))

displayField.setText(displayField.getText() + "-");

}

if (!input.equals("+/-"))

{

if (input.equals("Backspace"))

{

String str = displayField.getText();

if (str.length() > 0)

displayField
.setText(str.substring(0, str.length() - 1));

}

else if (input.equals("CE") || input.equals("C"))

{

displayField.setText("0");

start = true;

}

else

displayField.setText(displayField.getText() + input);

}

}

}

private class CommandAction implements ActionListener

{

public void actionPerformed(ActionEvent evt)

{

String command = evt.getActionCommand();

if (start)

{

lastCommand = command;

}

else

{

calculate(Double.parseDouble(displayField.getText()));

lastCommand = command;

start = true;

}

}

}

public void calculate(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("="))
result = x;

displayField.setText("" + result);

}
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网