用Java程序设计语言设计一个计算器

用java程序设计语言设计一个计算器,最好是图形界面类型的。
要求系统不能过小,至少代码有400行左右。
最好有些适当的解释说明~
可以的话会高分感谢的!
最好发我邮箱吧~zjccan@yeah.net

import javax.swing.*;//新的窗口组件包
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 显示栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator()
{
setTitle("简单计算器");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false); //设置文本框的可编辑性
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("NC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i);
else if(! operated)
operation.setText(operation.getText() + i);
else
{
operation.setText("" + i);
operated = false;
}
}
if (btn == dot && ! dotExist)
{
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(230, 280);
calc.setVisible(true);
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-06-09
四百行有点多啊,我自己写的一个,挺小的 ,发你邮箱了
第2个回答  2010-06-24
400行确实有点难啊!!

用JAVA编写的科学计算器源代码
这个科学计算器支持基本的四则运算(加、减、乘、除)以及一些科学运算(正弦、余弦、正切、对数、指数和平方根)。用户可以通过输入相应的操作和两个数字来执行计算。例如,输入“add 2 3”将计算2加3的结果。代码首先导入了`Scanner`类,用于从用户处获取输入。然后,在`main`方法中,创建...

java编写程序:创建一个 计算器 的 界面,包含 0-9 个 数字 , A-F个字 ...
import java.awt.*;import javax.swing.*;public class MyCalculator extends JFrame{public MyCalculator() {String ss1 = "0123456789ABCDEF";String[] ss2 = {"+","-","*","\/","√ ̄","aⁿ","CE","="};\/\/有些字符不在GBK中,所以文本使用了UTF-8编码。如果要用GBK编码,...

java怎样写一个简单的计算器
import java.util.Scanner;public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("Enter first number:");double num1 = scanner.nextDouble();System.out.println("Enter second number:");double num2 = scanner.nex...

采用java语言编写一个计算器,该计算器能对正数、负数(这里的正数、负数...
mb1.add(mf1); \/\/菜单栏中加入菜单 mb1.add(me1); mb1.add(mh1); mf1.add(new MenuItem("复制")); \/\/生成菜单项并加入到菜单 mf1.add(new MenuItem("粘贴")); mf1.addActionListener(this); \/\/为菜单注册事件监听程序 me1.add(new MenuItem("标准型")); me1.add(new MenuItem("科学型"...

求JAVA编写的 简易计算器程序,附上注释
1、首先准备好软件即eclipse和java,下载安装完成后打开eclipse。2、点击左上角的file 新建一个project。3、给project取一个名字,其他的选项都是默认然后点击finish。4、接下来是新建一个class。5、在给class取名字的时候注意用英文名的首字母要大写。完成后点击finish。6、这就是开始写代码的工作台,将...

求一计算器java代码,最好简单一点(图形界面)
Frame fm = new Frame("简单计算器"); for (int i = 0; i <= 16; i++) { b[i] = new Button(ss[i]); } for (int i = 0; i <= 15; i++) { p2.add(b[i]); } \/\/创建按钮 并添加到P2 b[16].setBackground(Color.yellow); txt = new TextField(15); txt.setEditable(fal...

用JAVA编写一个简单的计算器,要求如下
以下是上图计算器的代码:package Computer;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Stack;import javax.swing.JApplet;i...

Java程序--编写一个具有图形界面的计算器程序
import java.io.*;public class Test implements ActionListener { Frame f = new Frame("一步运算计算器");\/\/创建窗架 TextField[] tf = new TextField[3];Label La1 = new Label("+");Button btn = new Button("=");int[] a = new int[3]; \/\/储存数据 public Test(){ f....

用java编得计算器程序软件和源代码
java计算器源程序java计算器import java.awt.*; import javax.swing.*; import java.awt.event.*; class calculation extends JFrame {public calculation() \/*构造方法*\/ {super("计数器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initTextPanel(); \/*文本框*\/ initControlPanel(); ...

用Java程序设计语言设计一个计算器
private char lastOperator; \/\/ 表示上一运算符 private JTextField operation; \/\/ 显示栏 private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; \/\/ 运算符 private JButton[] numbers; \/\/ 数字 \/\/ 构造者 public Calculator(){ setTitle("简单计算器");\/\/ 初始化...

相似回答