Java编译计算器的代码 java编写计算器程序代码

JAVA 编写计算器 要代码最简单的

学java的时候自己编的,很简单,能够连续输入计算式后进行计算

10年积累的网站设计、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有庄浪免费网站建设让你可以放心的选择与我们合作。

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.NumberFormat;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**简易计算器,能够进行简单的计算

*

* @see 2008.12.9

*/

public class CalculatorA

implements ActionListener{

private JFrame frame;

private JTextField field;

private JButton[] allButtons;

private JButton clearButton;

// private JButton backButton;

String result="";//保存结果

StringBuilder sb = new StringBuilder();//保存要进行的计算式

int x = 0; //用来判断上一次的事件类型

String str = "123+456-789*0.=/";

ArrayListString arrayList = new ArrayListString();//保存计算式,通过方法进行运算

public CalculatorA(){

frame = new JFrame("我的计算器v1.1");

frame.setLocation(300,300);

field = new JTextField(25);

allButtons = new JButton[16];

for(int i=0;iallButtons.length;i++){

allButtons[i]= new JButton(str.substring(i,i+1));

}

clearButton = new JButton("CLEAR");

// backButton = new JButton("——");

init();

setFondAndColor();

addEventHander();

}

public void init(){

frame.setLayout(new BorderLayout());

JPanel northPanel = new JPanel();

JPanel centerPanel = new JPanel();

JPanel southPanel = new JPanel();

northPanel.setLayout(new FlowLayout());

centerPanel.setLayout(new GridLayout(4,4));

southPanel.setLayout(new FlowLayout());

northPanel.add(field);

for(int i=0;iallButtons.length;i++){

centerPanel.add(allButtons[i]);

}

southPanel.add(clearButton);

//southPanel.add(backButton);

frame.add(northPanel,BorderLayout.NORTH);

frame.add(centerPanel,BorderLayout.CENTER);

frame.add(southPanel,BorderLayout.SOUTH);

}

//设置输入字体

public void setFondAndColor(){

field.setFont(new Font("宋体",Font.BOLD,24));

field.setBackground(new Color(100,200,200));

field.setForeground(Color.RED);

//设置字体从右起始

field.setHorizontalAlignment(JTextField.RIGHT);

}

public void showMi(){

frame.pack();

frame.setResizable(false);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void addEventHander(){

for(int i=0;iallButtons.length;i++){

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(this);

// backButton.addActionListener(this);

}

@Override

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();//取得当前事件返回的值

if("0123456789.".indexOf(str)!=-1){

if(x == 0){ //当x为0时表示还没有进行输入

result=str;

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 1){

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 2){

sb.delete(0,sb.length());

result = result+str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 3){

result = str;

sb.delete(0,sb.length());

arrayList.clear();

field.setText(str);

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

result = str;

sb.append(str);

field.setText(str);

x = 1;

}

else{

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

}

else if("+*-/".indexOf(str)!=-1){

if(x == 0){

field.setText("");

x = 2;

}

else if(x == 1){

result = result + str;

arrayList.add(sb.toString());

arrayList.add(str);

sb.append(str);

field.setText(result);

x = 2;

}

else if(x == 2){

x = 2;

}

else if(x == 3){

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

x = 2;

}

else{

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

}

else if("=".equals(str)){

if(x == 0){

field.setText("0");

arrayList.clear();

result = "0";

x = 3;

}

else if(x == 1){

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("数据格式异常");

x = 0;

}

}

else if(x == 2){

field.setText("数据格式错误.....");

arrayList.clear();

x = 0;

}

else if(x == 3){

field.setText(result);

x = 3;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

x = 3;

}

else {

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("数据格式异常");

x = 0;

}

}

}

else if("CLEAR".equals(str)){

arrayList.clear();

field.setText("0");

arrayList.add("0");

x = 4;

}

else{

if(result.length()1){

result = result.substring(0,result.length()-1);

if(sb.length()0){

sb.delete(sb.length()-1,sb.length());

}

else {

sb.delete(0,1);

}

field.setText(result);

x = 5;

}

else{

result = "";

sb.delete(0,sb.length());

arrayList.clear();

field.setText("0");

x = 0;

}

}

}

public static ArrayListString getResult(ArrayListString list){

String res = null;

String[] s = {"/","*","-","+"};

int i=0;

if(list.size()1){

for(;is.length;){

if(s[i].equals("/")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));

//本地的数据格式

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("*")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("-")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getNumberInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else {

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

}

}

return list;

}

//对数字字符串进行排除不必要符号

public static String getChange(String res){

String s_temp = "";

char[] c = new char[res.length()];

for(int k=0;kc.length;k++){

c[k] = res.charAt(k);

}

for(int k=0;kc.length;k++){

if((c[k]= '0' c[k]= '9')|| c[k] == '.'){

s_temp += c[k];

}

}

return s_temp;

}

public static void main(String[] args){

new CalculatorA().showMi();

}

}

求编写一个计算器的java代码

package com;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.text.DecimalFormat;

public class calculator implements ActionListener { //导入动作监听接口

//设计面板中的单位

JFrame frame;

JTextField textAnswer;

JPanel panel, panel1, panel2, panel3;

JMenuBar mainMenu;

JTextField textMemory;

JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状

JButton buttonBk, buttonCe, buttonC;

JButton button[];

JButton buttonMC, buttonMR, buttonMS, buttonMAdd;

JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,

buttonDiv, buttonMod;

JButton buttonSqrt, buttonDao, buttonEqual;

JMenu editMenu, viewMenu, helpMenu;

JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal;

DecimalFormat df; //设置数据输出精度

boolean clickable; //控制当前能否按键

double memoryd; //使用内存中存储的数字

int memoryi;

double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)

short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算

String copy; //做复制用

JTextArea help; //帮助

JScrollPane scrollHelp;

//构造函数

@SuppressWarnings({ "static-access", "deprecation" })

public calculator() {

clickable = true;

answerd = 0;

frame = new JFrame("计算器");

df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)

textAnswer = new JTextField(15);

textAnswer.setText("");

textAnswer.setEditable(false);

textAnswer.setBackground(new Color(255, 255, 255));

panel = new JPanel();

frame.getContentPane().add(panel);

panel1 = new JPanel();

panel2 = new JPanel();

panel.setLayout(new BorderLayout());

//设计整个面板

用JAVA编写一个计算器

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

public class Jisuanqi extends JFrame implements ActionListener {

/**

*

*/

private static final long serialVersionUID = 1L;

Result result = new Result(); // 定义text的面板

Number_Key number_key = new Number_Key(); // 定义按钮面板

// 当点击按钮+、-、*、/时,com = true

boolean com = false;

// 当i=0时说明是我们第一次输入,字符串text不会累加

int i = 0;

// 存放text的内容

String text = "";

// 存放点击按钮+、-、*、/之前的数值

double defbutton = 0;

// +、-、*、/的代号分别为1,2,3,4

int symbol = 0;

// 构造函数

Jisuanqi() {

super("计算器"); // 设定标题

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定关闭窗体时退出程序

JPanel pane = new JPanel(); // 定义主面板

pane.setLayout(new BorderLayout());

setBounds(380, 220, 30, 80); // 前两个参数是在屏幕上显示的坐标,后两个是大小

// 替换图标

ImageIcon icon = new ImageIcon("F:1.GIF");

// Jisuanqi.class.getResource("APPLE.GIF")

// );

setIconImage(icon.getImage());

pane.add(result, BorderLayout.NORTH);

pane.add(number_key, BorderLayout.CENTER);

pane.add(number_key.equal, BorderLayout.SOUTH);

number_key.one.addActionListener(this); // 对1按钮添加监听事件

number_key.two.addActionListener(this); // 对2按钮添加监听事件

number_key.three.addActionListener(this); // 对3按钮添加监听事件

number_key.four.addActionListener(this); // 对4按钮添加监听事件

number_key.five.addActionListener(this); // 对5按钮添加监听事件

number_key.six.addActionListener(this); // 对6按钮添加监听事件

number_key.seven.addActionListener(this); // 对7按钮添加监听事件

number_key.eight.addActionListener(this); // 对8按钮添加监听事件

number_key.nine.addActionListener(this); // 对9按钮添加监听事件

number_key.zero.addActionListener(this); // 对0按钮添加监听事件

number_key.ce.addActionListener(this); // 对置零按钮添加监听事件

number_key.plus.addActionListener(this); // 对+按钮添加监听事件

number_key.equal.addActionListener(this); // 对=按钮添加监听事件

number_key.sub.addActionListener(this); // 对-按钮添加监听事件

number_key.mul.addActionListener(this); // 对*按钮添加监听事件

number_key.div.addActionListener(this); // 对/按钮添加监听事件

number_key.point.addActionListener(this); // 对.按钮添加监听事件

setContentPane(pane);

pack(); // 初始化窗体大小为正好盛放所有按钮

}

// 各个按钮触发的事件

public void actionPerformed(ActionEvent e) {

/*

* 如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true 如果没有com=

* false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;

*/

if (e.getSource() == number_key.one) {

if (com || i == 0) {

result.text.setText("1");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "1");

}

} else if (e.getSource() == number_key.two) {

if (com || i == 0) {

result.text.setText("2");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "2");

}

} else if (e.getSource() == number_key.three) {

if (com || i == 0) {

result.text.setText("3");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "3");

}

} else if (e.getSource() == number_key.four) {

if (com || i == 0) {

result.text.setText("4");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "4");

}

} else if (e.getSource() == number_key.five) {

if (com || i == 0) {

result.text.setText("5");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "5");

}

} else if (e.getSource() == number_key.six) {

if (com || i == 0) {

result.text.setText("6");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "6");

}

} else if (e.getSource() == number_key.seven) {

if (com || i == 0) {

result.text.setText("7");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "7");

}

} else if (e.getSource() == number_key.eight) {

if (com || i == 0) {

result.text.setText("8");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "8");

}

} else if (e.getSource() == number_key.nine) {

if (com || i == 0) {

result.text.setText("9");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "9");

}

}

/*

* 对于0这个按钮有一定的说法,在程序里不会出现如00000这样的情况,加了判断条件就是

* 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添 加0;否则保持原来的数值不变

*/

else if (e.getSource() == number_key.zero) { // result.text.getText()是得到text里内容的意思

if (com || i == 0) {

result.text.setText("0");

com = false;

i = 1;

} else {

text = result.text.getText();

if (Float.parseFloat(text) 0 || Float.parseFloat(text) 0) { // Float.parseFloat(text)就是类型转换了,下面都是一样

result.text.setText(text + "0");

} else {

if (text.trim().indexOf(".") == -1) {

result.text.setText(text);

} else {

result.text.setText(text + "0");

}

}

}

} else if (e.getSource() == number_key.ce) {

result.text.setText("0");

i = 0;

com = true;

// text = "";

defbutton = 0;

}

/*

* 本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加, 不存在就添加.

*/

else if (e.getSource() == number_key.point) {

if (com || i == 0) {

result.text.setText("0.");

com = false;

i = 1;

} else {

text = result.text.getText();

if (text.trim().indexOf(".") == -1) {

result.text.setText(text + ".");

} else {

result.text.setText(text);

}

}

} // 获得点击+之前的数值

else if (e.getSource() == number_key.plus) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

symbol = 1;

} // 获得点击-之前的数值

else if (e.getSource() == number_key.sub) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

symbol = 2;

} // 获得点击*之前的数值

else if (e.getSource() == number_key.mul) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

System.out.println(defbutton);

symbol = 3;

} // 获得点击/之前的数值

else if (e.getSource() == number_key.div) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

symbol = 4;

} else if (e.getSource() == number_key.equal) {

switch (symbol) {

case 1: { // 计算加法

double ad = defbutton

+ Double.parseDouble(result.text.getText());

result.text.setText(ad + "");

i = 0;

text = "";

break;

}

case 2: { // 计算减法

double ad = defbutton

- Double.parseDouble(result.text.getText());

result.text.setText(String.valueOf(ad));

i = 0;

text = "";

break;

}

case 3: { // 计算乘法

double ad = defbutton

* Double.parseDouble(result.text.getText());

result.text.setText(ad + "");

i = 0;

text = "";

break;

}

case 4: { // 计算除法

double ad = defbutton

/ Double.parseDouble(result.text.getText());

result.text.setText(ad + "");

i = 0;

text = "";

break;

}

}

System.out.println(com);

}

System.out.println(result.text.getText());

}

@SuppressWarnings("deprecation")

public static void main(String[] args) {

Jisuanqi loveyou = new Jisuanqi();

loveyou.show();

}

}

// 计算器数字按钮定义面板

class Number_Key extends JPanel {

/**

*

*/

private static final long serialVersionUID = 1L;

JButton zero = new JButton("0"); // 数字键0

JButton one = new JButton("1"); // 数字键1

JButton two = new JButton("2"); // 数字键2

JButton three = new JButton("3"); // 数字键3

JButton four = new JButton("4"); // 数字键4

JButton five = new JButton("5"); // 数字键5

JButton six = new JButton("6"); // 数字键6

JButton seven = new JButton("7"); // 数字键7

JButton eight = new JButton("8"); // 数字键8

JButton nine = new JButton("9"); // 数字键9

JButton plus = new JButton("+");

JButton sub = new JButton("-");

JButton mul = new JButton("*");

JButton div = new JButton("/");

JButton equal = new JButton("=");

JButton ce = new JButton("清零"); // 置零键

JButton point = new JButton(".");

Number_Key() {

setLayout(new GridLayout(4, 4, 1, 1)); // 定义布局管理器为网格布局

setBackground(Color.blue); // 设置背景颜色

// 添加按钮

add(one);

add(two);

add(three);

add(four);

add(five);

add(six);

add(seven);

add(eight);

add(nine);

add(zero);

add(plus);

add(sub);

add(mul);

add(div);

add(point);

add(equal);

add(ce);

}

}

// 计算器显示结果的窗体

class Result extends JPanel {

/**

*

*/

private static final long serialVersionUID = 1L;

// text先是输入和结果

JTextField text = new JTextField("0");

@SuppressWarnings("deprecation")

Result() { // 讲输入的数字或得到的结果在text的右边显示

text.setHorizontalAlignment(SwingConstants.RIGHT);

text.enable(false); // 文本框不能编辑

setLayout(new BorderLayout()); // 设定布局管理器边框布局

add(text, BorderLayout.CENTER); // text放置在窗体的中间

}

}

用Java编写计算器

import java.util.regex.*;

public class Calculation {

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

if (args.length != 3) {

System.out.println("MyCal need 3 parameters the formate is:");

System.out.println("MyCal num1 operate num2:");

return;

}

if (!isNumeric(args[0]) || !isNumeric(args[2])) {

System.out.println("MyCal need 3 parameters the formate is:");

System.out.println("MyCal num1 operate num2");

System.out.println("num1 and num2 must be number:");

return;

}

int num1 = Integer.parseInt(args[0]);

int num2 = Integer.parseInt(args[2]);

if (args[1].length() == 1) {

switch (args[1].charAt(0)) {

case '+':

System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));

return;

case '-':

System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));

return;

case 'x':

System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));

return;

case '/':

if (num2 == 0) {

System.out.println("can't / 0");

return;

}

System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));

return;

}

}

System.out.println("MyCal need 3 parameters the formate is:");

System.out.println("MyCal num1 operate num2");

System.out.println("operate be (+, -, x, /)");

return;

}

public static boolean isNumeric(String str) {

Pattern pattern = Pattern.compile("[0-9]*");

Matcher isNum = pattern.matcher(str);

if(!isNum.matches()) {

return false;

}

return true;

}

}

java编一个计算器的代码

界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!

代码如下:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Counter {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();

}

}

class CounterFrame extends JFrame {

public CounterFrame() {

JMenuBar menuBar = new JMenuBar();

JMenu menuFile = new JMenu();

JMenu menuFile1 = new JMenu();

JMenu menuFile2 = new JMenu();

JMenu menuFile3 = new JMenu();

JMenuItem menuFileExit = new JMenuItem();

menuFile.setText("文件");

menuFile1.setText("编辑");

menuFile2.setText("查看");

menuFile3.setText("帮助");

menuFileExit.setText("退出");

menuFileExit.addActionListener

(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

CounterFrame.this.windowClosed();

}

}

);

menuFile.add(menuFileExit);

menuBar.add(menuFile);

menuBar.add(menuFile1);

menuBar.add(menuFile2);

menuBar.add(menuFile3);

setTitle("计算器");

setJMenuBar(menuBar);

setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

CounterFrame.this.windowClosed();

}

}

);

}

protected void windowClosed() {

System.exit(0);

}

}

class Tool {

public static Tool instance;

private JTextField field;

private Tool() {

this.field=new JTextField(30);

this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()

{

if(instance==null)

{

instance=new Tool();

}

return instance;

}

public JTextField getfield()

{

return (this.field);

}

}

class Allpanel extends JPanel {

public Allpanel() {

this.setLayout(new BorderLayout(0,7));

Northpanel np=new Northpanel();

Centerpanel cp=new Centerpanel();

this.add(np,BorderLayout.NORTH);

this.add(cp,BorderLayout.CENTER);

}

}

class Centercenter extends JPanel {

static Vector Vec=new Vector();

static Vector vc=new Vector();

static Vector vc1=new Vector();

static Vector vc2=new Vector();

static Vector vc3=new Vector();

static String begin="yes";

static double add;

static double jq;

static double cs;

static double cq;

static double dy;

static String jg;

static String what;

static double tool=0;

static String to="yes";

/**

* Method Centercenter

*

*

*/

public Centercenter() {

// TODO: Add your code here

final JTextField text=Tool.getinstance().getfield();

this.setLayout(new GridLayout(4,5,3,3));

String arg[] ={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};

for(int i=0;i20;i++)

{

final JButton b=new JButton(arg[i]);

//this.add(new JButton(arg[i]));

this.add(b);

if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mark=b.getText();

String ma=text.getText();

if(vc3.contains("v3"))

{

text.setText("0."+mark);

vc3.clear();

}

else if(vc.contains("a"))

{

if(vc2.contains("v2"))

{

text.setText("0."+mark);

vc.clear();

vc2.clear();

}

else

{

text.setText(mark);

vc.clear();

Vec.clear();

Vec.add(mark);

}

}

else

{

text.setText(ma.trim()+mark);

Vec.add(mark);

}

begin="no";

to="yes";

}

});

}

if(i==17)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mar=b.getText();

String m=text.getText();

if("yes".equals(begin))

{

vc3.add("v3");

}

if(vc1.contains("v1"))

{

vc2.add("v2");

vc1.clear();

}

if(!Vec.contains(".")!vc.contains("a"))

{

text.setText(m.trim()+mar);

Vec.add(".");

}

}

});

}

if(i==18)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

add=Double.parseDouble(ma);

if(what==null)

{

tool=add;

what="add";

}

else

{

tool=tool+add;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="+";

}

});

}

if(i==13)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

jq=Double.parseDouble(ma);

if(what==null)

{

tool=jq;

what="jq";

}

else

{

tool=tool-jq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="-";

}

});

}

if(i==3)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

if(what==null)

{

tool=cq;

what="cq";

}

else

{

tool=tool/cq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="/";

}

});

}

if(i==4)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

text.setText(String.valueOf(Math.sqrt(cq)));

}

});

}

if(i==8)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cs=Double.parseDouble(ma);

if(what==null)

{

tool=cs;

what="cs";

}

else

{

tool=tool*cs;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="*";

}

});

}

if(i==19)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

dy=Double.parseDouble(ma);

if(what=="add")

{

jg=String.valueOf((tool+dy));

}

if(what=="jq")

{

jg=String.valueOf((tool-dy));

}

if(what=="cs")

{

jg=String.valueOf((tool*dy));

}

if(what=="cq")

{

jg=String.valueOf((tool/dy));

}

if(what==null)

{

if(to=="+")

{

tool=add;

jg=String.valueOf(tool+dy);

}

else if(to=="-")

{

tool=jq;

jg=String.valueOf(dy-tool);

}

else if(to=="*")

{

tool=cs;

jg=String.valueOf(dy*tool);

}

else if(to=="/")

{

tool=cq;

jg=String.valueOf(dy/tool);

}

else

{

jg=String.valueOf(dy);

}

}

text.setText(jg);

Vec.clear();

Vec.add(".");

vc.add("a");

vc1.add("v1");

what=null;

tool=0;

}

});

}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

JButton jb1=new JButton("Backspace");

JButton jb2=new JButton(" CE ");

JButton jb3=new JButton(" C ");

this.add(jb1);

this.add(jb2);

this.add(jb3);

jb1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

String back=Tool.getinstance().getfield().getText();

text.setText(backmethod(back));

Centercenter.Vec.remove(Centercenter.Vec.size()-1);

}

});

jb3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

text.setText("0.");

Centercenter.Vec.clear();

Centercenter.Vec.add(".");

Centercenter.vc.add("a");

Centercenter.begin="yes";

Centercenter.vc1.clear();

Centercenter.what=null;

Centercenter.tool=0;

}

});

}

public String backmethod(String str)

{

return str.substring(0,str.length()-1);

}

}

class Centerpanel extends JPanel {

public Centerpanel() {

this.setLayout(new BorderLayout(8,7));

Centernorth cn=new Centernorth();

Centercenter cc=new Centercenter();

Centerwest cw=new Centerwest();

this.add(cn,BorderLayout.NORTH);

this.add(cc,BorderLayout.CENTER);

this.add(cw,BorderLayout.WEST);

}

}

class Centerwest extends JPanel {

public Centerwest() {

this.setLayout(new GridLayout(4,1,3,3));

this.add(new JButton("MC"));

this.add(new JButton("MR"));

this.add(new JButton("MS"));

this.add(new JButton("M+"));

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {

tf=Tool.getinstance().getfield();

this.add(tf);

}

}

---------------------------------------------------------------------------

=============《按你要求特意后改过的最简单功能的代码如下》========================

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Counter2 {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();

}

}

class CounterFrame extends JFrame {

public CounterFrame() {

setTitle("计算器");

setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

CounterFrame.this.windowClosed();

}

}

);

}

protected void windowClosed() {

System.exit(0);

}

}

class Tool {

public static Tool instance;

private JTextField field;

private Tool() {

this.field=new JTextField(30);

this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()

{

if(instance==null)

{

instance=new Tool();

}

return instance;

}

public JTextField getfield()

{

return (this.field);

}

}

class Allpanel extends JPanel {

public Allpanel() {

this.setLayout(new BorderLayout(0,7));

Northpanel np=new Northpanel();

Centerpanel cp=new Centerpanel();

this.add(np,BorderLayout.NORTH);

this.add(cp,BorderLayout.CENTER);

}

}

class Centercenter extends JPanel {

static Vector Vec=new Vector();

static Vector vc=new Vector();

static Vector vc1=new Vector();

static Vector vc2=new Vector();

static Vector vc3=new Vector();

static String begin="yes";

static double add;

static double jq;

static double cs;

static double cq;

static double dy;

static String jg;

static String what;

static double tool=0;

static String to="yes";

/**

* Method Centercenter

*

*

*/

public Centercenter() {

// TODO: Add your code here

final JTextField text=Tool.getinstance().getfield();

this.setLayout(new GridLayout(4,5,3,3));

String arg[] ={"7","8","9","/","4","5","6","*","1","2","3","-","0","=",".","+"};

for(int i=0;i16;i++)

{

final JButton b=new JButton(arg[i]);

//this.add(new JButton(arg[i]));

this.add(b);

if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mark=b.getText();

String ma=text.getText();

if(vc3.contains("v3"))

{

text.setText("0."+mark);

vc3.clear();

}

else if(vc.contains("a"))

{

if(vc2.contains("v2"))

{

text.setText("0."+mark);

vc.clear();

vc2.clear();

}

else

{

text.setText(mark);

vc.clear();

Vec.clear();

Vec.add(mark);

}

}

else

{

text.setText(ma.trim()+mark);

Vec.add(mark);

}

begin="no";

to="yes";

}

});

}

if(i==14)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mar=b.getText();

String m=text.getText();

if("yes".equals(begin))

{

vc3.add("v3");

}

if(vc1.contains("v1"))

{

vc2.add("v2");

vc1.clear();

}

if(!Vec.contains(".")!vc.contains("a"))

{

text.setText(m.trim()+mar);

Vec.add(".");

}

}

});

}

if(i==15)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

add=Double.parseDouble(ma);

if(what==null)

{

tool=add;

what="add";

}

else

{

tool=tool+add;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="+";

}

});

}

if(i==11)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

jq=Double.parseDouble(ma);

if(what==null)

{

tool=jq;

what="jq";

}

else

{

tool=tool-jq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="-";

}

});

}

if(i==3)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

if(what==null)

{

tool=cq;

what="cq";

}

else

{

tool=tool/cq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="/";

}

});

}

if(i==7)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cs=Double.parseDouble(ma);

if(what==null)

{

tool=cs;

what="cs";

}

else

{

tool=tool*cs;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="*";

}

});

}

if(i==13)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

dy=Double.parseDouble(ma);

if(what=="add")

{

jg=String.valueOf((tool+dy));

}

if(what=="jq")

{

jg=String.valueOf((tool-dy));

}

if(what=="cs")

{

jg=String.valueOf((tool*dy));

}

if(what=="cq")

{

jg=String.valueOf((tool/dy));

}

if(what==null)

{

if(to=="+")

{

tool=add;

jg=String.valueOf(tool+dy);

}

else if(to=="-")

{

tool=jq;

jg=String.valueOf(dy-tool);

}

else if(to=="*")

{

tool=cs;

jg=String.valueOf(dy*tool);

}

else if(to=="/")

{

tool=cq;

jg=String.valueOf(dy/tool);

}

else

{

jg=String.valueOf(dy);

}

}

text.setText(jg);

Vec.clear();

Vec.add(".");

vc.add("a");

vc1.add("v1");

what=null;

tool=0;

}

});

}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

}

}

class Centerpanel extends JPanel {

public Centerpanel() {

this.setLayout(new BorderLayout(8,7));

Centernorth cn=new Centernorth();

Centercenter cc=new Centercenter();

Centerwest cw=new Centerwest();

this.add(cn,BorderLayout.NORTH);

this.add(cc,BorderLayout.CENTER);

this.add(cw,BorderLayout.WEST);

}

}

class Centerwest extends JPanel {

public Centerwest() {

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {

tf=Tool.getinstance().getfield();

this.add(tf);

}

}

------------------------------------------------------------

才子_辉祝您愉快!

用JAVA编写的科学计算器源代码

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class Counter extends WindowAdapter

{

static JFrame f=new JFrame("计算器");

static JTextField text1=new JTextField("0.");

static String source="";

static String cal="";

static String object="";

static boolean flag=false;

static boolean flag1=true;

static boolean flag2=false;

public void init()

{

try

{

Container c=f.getContentPane();

JPanel pan1=new JPanel();

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 b0=new JButton("0");

JButton b11=new JButton("+");

JButton b12=new JButton("-");

JButton b13=new JButton("*");

JButton b14=new JButton("/");

JButton b15=new JButton(".");

JButton b16=new JButton("=");

JButton bclar=new JButton("清零");

text1.setHorizontalAlignment(JTextField.RIGHT);

c.add(text1,"North");

c.add(pan1);

A aa=new A();

Result re=new Result();

Opertion op=new Opertion();

Clar cl=new Clar();

b1.addActionListener(aa);

b2.addActionListener(aa);

b3.addActionListener(aa);

b4.addActionListener(aa);

b5.addActionListener(aa);

b6.addActionListener(aa);

b7.addActionListener(aa);

b8.addActionListener(aa);

b9.addActionListener(aa);

b0.addActionListener(aa);

b11.addActionListener(op);

b12.addActionListener(op);

b13.addActionListener(op);

b14.addActionListener(op);

b16.addActionListener(re);

b15.addActionListener(aa);

bclar.addActionListener(cl);

pan1.add(b1);

pan1.add(b2);

pan1.add(b3);

pan1.add(b11);

pan1.add(b4);

pan1.add(b5);

pan1.add(b6);

pan1.add(b12);

pan1.add(b7);

pan1.add(b8);

pan1.add(b9);

pan1.add(b13);

pan1.add(b0);

pan1.add(b15);

pan1.add(b16);

pan1.add(b14);

pan1.add(bclar);

f.setSize(200,220);

f.setVisible(true);

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

class A implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String a=text1.getText();

String s=e.getActionCommand();

if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))

text1.setText(s);

else {

if(flag2)

{

text1.setText(s);

flag2=false;

}

else

text1.setText(a+s);

}

}

}

class Opertion implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

cal=e.getActionCommand();

if(flag1==true)

source=text1.getText();

text1.setText(cal);

flag1=false;

flag=true;

}

}

class Result implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

double num1;

num1=Double.parseDouble(source);

object=text1.getText();

double num2;

num2=Double.parseDouble(object);

double result=0;

if(cal.equals("+"))

result=num1+num2;

if(cal.equals("-"))

result=num1-num2;

if(cal.equals("*"))

result=num1*num2;

if(cal.equals("/"))

if(num2==0)

text1.setText("除数不能为0");

else

result=num1/num2;

String s1=Double.toString(result);

text1.setText(s1);

flag1=true;

flag2=true;

}

}

class Clar implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

text1.setText("0.");

}

}

public static void main(String[] args)

{

Counter count=new Counter();

count.init();

}

public void windowClosing(WindowEvent e){

System.exit(1);

}

public void windowOpened(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowActivated(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

}


本文题目:Java编译计算器的代码 java编写计算器程序代码
本文路径:http://myzitong.com/article/dodieis.html