问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求Java编的简单计算器源代码

发布网友 发布时间:2024-01-18 04:48

我来回答

3个回答

热心网友 时间:2024-07-25 17:27

http://hi.baidu.com/zpsailor/blog/item/6918f41696e1d9084b90a737.html
到这里去下哈

热心网友 时间:2024-07-25 17:23

package edu.hpu.yyf;
import java.awt.*;
import java.awt.event.*;
public class MyCalculator {
private static double d1 = 0.0;
private static double d2 = 0.0;
private static String s1 = new String("0");
private static String s2 = new String("0");
private static char c1 ='0';
private static void judgec1(TextField text){
switch(c1){
case '+':s2 = String.valueOf(d1+d2);d1=d1+d2;d2=0;text.setText(s2);s2="0";break;
case '-':s2 = String.valueOf(d1-d2);d1=d1-d2;d2=0;text.setText(s2);s2="0";break;
case '*':s2 = String.valueOf(d1*d2);d1=d1*d2;d2=0;text.setText(s2);s2="0";break;
case '/':if(d2==0) {text.setText("0");d1=0;break;}s2 = String.valueOf(d1/d2);d1=d1/d2;d2=0;text.setText(s2);s2="0";break;}
}
public static void main(String [] args){
Frame cclt = new Frame("我的计算器");
cclt.setBounds(300,150,300,265);
cclt.setLayout(null);
final TextField text = new TextField();
text.setBounds(10, 30, 280, 35);
text.setText("0");
text.setEditable(false);
Font font = new Font("", 5, 25);
text.setFont(font);
cclt.add(text);
Panel panel = new Panel();
Font font_1 = new Font("", 5, 20);
panel.setFont(font_1);
panel.setBounds(5, 67, 290, 193);
panel.setBackground(Color.GREEN);
panel.setLayout(new GridLayout(5,4,5,5));
cclt.add(panel);
Button space = new Button();
panel.add(space);
Button Backspace = new Button("Backspace");
Font font_2 = new Font("", 0, 14);
Backspace.setFont(font_2);
panel.add(Backspace);
Button CE = new Button("CE");
panel.add(CE);
Button C = new Button("C");
panel.add(C);
Button seven = new Button("7");
panel.add(seven);
Button eight = new Button("8");
panel.add(eight);
Button nine = new Button("9");
panel.add(nine);
Button but = new Button("/");
panel.add(but);
Button four = new Button("4");
panel.add(four);
Button five = new Button("5");
panel.add(five);
Button six = new Button("6");
panel.add(six);
Button ride = new Button("*");
panel.add(ride);
Button one = new Button("1");
panel.add(one);
Button two = new Button("2");
panel.add(two);
Button three = new Button("3");
panel.add(three);
Button substract = new Button("-");
panel.add(substract);
Button zero = new Button("0");
panel.add(zero);
Button space_1 = new Button(" ");
panel.add(space_1);
Button equal = new Button("=");
panel.add(equal);
Button add = new Button("+");
panel.add(add);
CE.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
d1=0;
d2=0;
s1="0";
s2="0";
c1 ='0';
text.setText("0");
}
});
seven.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="7";
else {text.setText(s1+"7");
s1+="7";}
text.setText(s1);
}
});
eight.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="8";
else {text.setText(s1+"8");
s1+="8";}
text.setText(s1);
}
});
nine.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="9";
else {text.setText(s1+"9");
s1+="9";}
text.setText(s1);
}
});
four.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="4";
else {text.setText(s1+"4");
s1+="4";}
text.setText(s1);
}
});
five.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="5";
else {text.setText(s1+"5");
s1+="5";}
text.setText(s1);
}
});
six.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="6";
else {text.setText(s1+"6");
s1+="6";}
text.setText(s1);
}
});
one.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="1";
else {text.setText(s1+"1");
s1+="1";}
text.setText(s1);
}
});
two.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="2";
else {text.setText(s1+"2");
s1+="2";}
text.setText(s1);
}
});
three.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="3";
else {text.setText(s1+"3");
s1+="3";}
text.setText(s1);
}
});
zero.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(!s1.equals("0")) s1+="0";
text.setText(s1);
}
});
but.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '/';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '/';}
s1="0";
}
});
substract.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '-';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '-';}
s1="0";
}
});
ride.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '*';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '*';}
s1="0";
}
});
add.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '+';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '+';}
s1="0";
}
});
/* Backspace.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(s1.length()!=0){
if(c1=='0') {text.setText(s1.substring(0, s1.length()-1));s1=s1.substring(0, s1.length()-1);}
else if (c1!='0'&&!s2.equals("0")) {text.setText(s2.substring(0, s1.length()-1));s2=s2.substring(0, s2.length()-1);}
}}
});*/
equal.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') d1=Double.parseDouble(s1);
else {d2=Double.parseDouble(s1);
judgec1(text);}
s1="0";
c1='0';
}
});
cclt.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
((Frame) e.getComponent()).dispose();
}
});
cclt.setResizable(false);
cclt.setVisible(true);
}
}
我们老师写的哦

热心网友 时间:2024-07-25 17:22

package util;
import java.awt.*;

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

public class Calculator extends JFrame {

private String num = "0";

private String operator = "+";

private String result = "0";

private final JTextField textField;

/*
* public static void main(String[] args) { // TODO Auto-generated method
* stub Calculator frame=new Calculator(); }
*/
public Calculator() {

super();
setTitle("计算器");
setResizable(false);
setBounds(100, 100, 208, 242);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
final JPanel viewPanel = new JPanel();
getContentPane().add(viewPanel, BorderLayout.NORTH);
textField = new JTextField();
textField.setText(num);
textField.setColumns(18);
textField.setEditable(false);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBackground(Color.white);
textField.setFont(new Font("Courier New", Font.BOLD, 18));
viewPanel.add(textField);
getContentPane().add(viewPanel, BorderLayout.NORTH);
final JPanel clearBottonPanel = new JPanel();
getContentPane().add(clearBottonPanel, BorderLayout.CENTER);
String[] clearButtonNames = { "<-", "CE", "C" };
for (int i = 0; i < clearButtonNames.length; i++) {
final JButton button = new JButton(clearButtonNames[i]);
button.setFont(new Font("Courier New", Font.PLAIN, 14));
button.setForeground(Color.red);
button.addActionListener(new ClearButtonActionListener());
clearBottonPanel.add(button);
}
final JPanel inpuptButtonPanel = new JPanel();
final GridLayout girdLayout = new GridLayout(4, 0);
girdLayout.setVgap(10);
girdLayout.setHgap(10);
inpuptButtonPanel.setLayout(girdLayout);
getContentPane().add(inpuptButtonPanel, BorderLayout.SOUTH);
String[][] inputButtonNames = { { "1", "2", "3", "+", },
{ "4", "5", "6", "-", }, { "7", "8", "9", "*", },
{ ".", "0", "=", "/", } };
for (int row = 0; row < inputButtonNames.length; row++) {
for (int col = 0; col < inputButtonNames.length; col++) {
final JButton button = new JButton(inputButtonNames[row][col]);
button.setName(row + "" + col);
button.setFont(new Font("Courier New", Font.BOLD, 15));
if (col == 3) {
button.setFont(new Font("Courier New", Font.PLAIN, 15));
button.setForeground(Color.red);
}
button.addActionListener(new InputButtonActionListener());
inpuptButtonPanel.add(button);
}
}
// setVisible(true);
}

class InputButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

JButton button = (JButton) e.getSource();
String name = button.getName();
Integer row = Integer.valueOf(name.substring(0, 1));
Integer col = Integer.valueOf(name.substring(1, 2));
if (col == 3) {
count();
textField.setText(result);
operator = button.getText();
} else if (row == 3) {
if (col == 0) {
if (num.indexOf(".") < 0) {
num = num + button.getText();
textField.setText(num);
}
} else if (col == 1) {
if (num.indexOf(".") > 0) {
num = num + button.getText();
textField.setText(num);
} else {
if (!num.substring(0, 1).equals("0")) {
num = num + button.getText();
textField.setText(num);
}
}

} else {
count();
textField.setText(result);
operator = "+";
}
} else {
if (num.equals("0"))
num = button.getText();
else
num = num + button.getText();
textField.setText(num);

}
}

private void count() {
float n = Float.valueOf(num);
float r = Float.valueOf(result);
if (r == 0) {
result = num;
num = "0";
} else {
if (operator.equals("+")) {
r = r + n;
} else if (operator.equals("-")) {
r = r - n;
} else if (operator.equals("*")) {
r = r * n;
} else {
r = r / n;
}
num = "0";
result = r + "";

}
}
}

public class ClearButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String text = button.getText().trim();
if (text.equals("<-")) {
int length = num.length();
if (length == 1)
num = "0";
else
num = num.substring(0, length - 1);

} else if (text.equals("CE")) {
num = "0";

} else {
num = "0";
operator = "+";
result = "0";

}
textField.setText(num);
}
}
}

谢谢采纳!!
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
巴西龟最长活多久,家养!!! 养胃的药最好的是什么啊 婴儿积食发烧不愿吃药怎么办 板门穴位在哪个部位 手机设置放偷看的方法? 凝结水回收器生产厂家? 个人账户养老金预测公式:现有5万元,缴费20年,能领多少钱? 临沂比较有名的男装品牌 呼伦贝尔市悦动网络科技有限公司怎么样? 呼伦贝尔中汇实业有限公司怎么样? 李雪健主演的电视剧水浒传中的林冲在什么情况下死的 荧光绿毛领呢大衣适合多大年龄穿 ...最早应用于医疗的麻醉剂之一.下列说法合理的是( ) A.依据等电子原理... ...结石,最大8*5mm,伴中度局限性积水,右肾上盏扩张。血尿3+.蛋白+... 郎靖风白阮什么小说 一个普通家庭要实现财富自由,需要达到哪些要求? 电话线能代替电源线用吗? 彬县到宝鸡班车一天几趟 从旬邑发彬县的早班车是几点?? ...我的汽车修理厂油漆车间承包给别人 请大家给我写一份承包合同... 穿在钢丝绳上的高度限位器脱落如何连接 翾的韩文怎么写? apple watch上酷狗怎么下载音乐? 如意皇后修剪方法 在电梯偶遇领导时,如何才能做到礼貌而不尴尬,你记住了吗? 三通玩法 为什么我不玩探探了他还扣费,48元一个月,大神们要怎么退出才不扣费... 白阮郎靖风叫什么 实名认证两个,其中一个支付时说限额10万,但是这两个加一... 工商银行卡待入账是什么意思_百度问一问 12万的车第一年保险多少钱 张曼玉气质真好,一身花里胡哨的花裙子,依旧能够穿出时髦高定感_百度知 ... 您的手机号在最近24小时内绑定过三个,已达到限制,...24小时后... 同一身份证两个手机注册两个一个不能用零钱转帐 一张身份证绑定两个一个能用零钱支付一个不能用怎么回事?_百度... 鄂州信基_池绿化率是多少? 道家思想对中国古代哪些文人有影响?具体的诗歌创作之类的,最好详细些... 改一年内怎么改第二次 小公司分公司抵押的担保如何认定 一年只能改一次怎么改第二次 青藏高原的隆起及其对地理环境的影响 entj代表人物 上了4天班一个月休二天,那一个月工资是多少,衣服本来扣150,黑扣35,保 ... 网易魔兽世界中带刷小副本频繁掉线的问题 怎样修改第二次 滚筒洗衣机有洗羽绒服功能可以洗羽绒服吗 作文 追求美也要采用正确方法 怎么二次修改 具体操作步骤 佛教的六字真言是什么 一年内第二次修改技巧