当前位置:首页 > 代码 > 正文

java简易计算器源代码(简易计算器编程java)[20240504更新]

admin 发布:2024-05-04 17:02 134


今天给各位分享java简易计算器源代码的知识,其中也会对简易计算器编程java进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用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的时候自己编的,很简单,能够连续输入计算式后进行计算

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 main;

import java.awt.Button;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.math.BigDecimal;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class app7 extends JFrame implements ActionListener {

static Panel pan = new Panel();

static JTextField textField = new JTextField("0");

static Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp, ba, bs, bm, bd,

be, bc, bt, bf, bh;

private StringBuffer temp = new StringBuffer("");

private String optValue = "0";

private String optType="";

private boolean isChoiseOptType=true;

public void init() {

b0 = new Button("0");

b0.addActionListener(this);

b1 = new Button("1");

b1.addActionListener(this);

b2 = new Button("2");

b2.addActionListener(this);

b3 = new Button("3");

b3.addActionListener(this);

b4 = new Button("4");

b4.addActionListener(this);

b5 = new Button("5");

b5.addActionListener(this);

b6 = new Button("6");

b6.addActionListener(this);

b7 = new Button("7");

b7.addActionListener(this);

b8 = new Button("8");

b8.addActionListener(this);

b9 = new Button("9");

b9.addActionListener(this);

bp = new Button(".");

bp.addActionListener(this);

ba = new Button("+");

ba.addActionListener(this);

bs = new Button("-");

bs.addActionListener(this);

bm = new Button("*");

bm.addActionListener(this);

bd = new Button("/");

bd.addActionListener(this);

be = new Button("=");

be.addActionListener(this);

bc = new Button("c");

bc.addActionListener(this);

bt = new Button("退格");

bt.addActionListener(this);

bf = new Button("1/x");

bf.addActionListener(this);

bh = new Button("+/-");

bh.addActionListener(this);

this.setTitle("计算机");

this.setLayout(null);

this.setSize(260, 300);

this.setResizable(false);

GridLayout grid = new GridLayout(4, 5);

pan.setLayout(grid);

pan.setBounds(20, 60, 150, 120);

textField.setBounds(20, 35, 150, 20);

textField.setBackground(Color.cyan);

textField.setHorizontalAlignment(textField.RIGHT);

textField.setEditable(false);

pan.add(b1);

pan.add(b2);

pan.add(b3);

pan.add(ba);

pan.add(bc);

pan.add(b4);

pan.add(b5);

pan.add(b6);

pan.add(bs);

pan.add(bt);

pan.add(b7);

pan.add(b8);

pan.add(b9);

pan.add(bm);

pan.add(bf);

pan.add(b0);

pan.add(bh);

pan.add(bp);

pan.add(bd);

pan.add(be);

this.add(textField);

this.add(pan);

}

public static void main(String[] args) {

app7 frm = new app7();

frm.init();

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setVisible(true);

}

@SuppressWarnings("static-access")

@Override

public void actionPerformed(ActionEvent e) {

String value="0";

if (e.getSource().equals(b0)) {

this.temp.append(b0.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b1)) {

this.temp.append(b1.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b2)) {

this.temp.append(b2.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b3)) {

this.temp.append(b3.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b4)) {

this.temp.append(b4.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b5)) {

this.temp.append(b5.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b6)) {

this.temp.append(b6.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b7)) {

this.temp.append(b7.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b8)) {

this.temp.append(b8.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b9)) {

this.temp.append(b9.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(bp)) {

if(this.temp.length()=0)

this.temp.append("0");

this.temp.append(bp.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(ba)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=ba.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bs)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bs.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bm)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bm.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bd)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bd.getLabel();

isChoiseOptType=true;

}else if (e.getSource().equals(be)) {

if(!this.optType.equals("")){

BigDecimal opt1=new BigDecimal(this.optValue);

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

BigDecimal opt2=new BigDecimal(value);

BigDecimal result=new BigDecimal(0);

if(this.optType.equals("+")){

result=opt1.add(opt2);

}else if(this.optType.equals("-")){

result=opt1.subtract(opt2);

}else if(this.optType.equals("*")){

result=opt1.multiply(opt2);

}else if(this.optType.equals("/")){

result=opt1.divide(opt2);

}else if(this.optType.equals("%")){

result=opt1.remainder(opt2);

}

this.textField.setText(result.toString());

this.temp=new StringBuffer("");

isChoiseOptType=false;

this.optValue="0";

}

} else if (e.getSource().equals(bc)) {

this.temp=new StringBuffer();

this.textField.setText("0");

} else if (e.getSource().equals(bt)) {

value=this.textField.getText();

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

if(value.indexOf("-")=0 value.length()=1){

value="0";

this.temp=new StringBuffer("");

}else{

this.temp=new StringBuffer(value);

}

this.textField.setText(value);

}else if (e.getSource().equals(bh)) {

value=this.textField.getText();

if(value.indexOf("-")==0){

value=value.substring(1,value.length());

}else{

value="-"+value;

}

this.temp=new StringBuffer(value);

this.textField.setText(value);

} else if (e.getSource().equals(bf)) {

this.optValue=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

this.optValue=this.optValue.substring(0,this.optValue.length()-1);

}

Integer opt1=new Integer(this.optValue);

if(!opt1.toString().equals("0")){

this.textField.setText(1.0/opt1.intValue()+"");

System.out.println(1/opt1.intValue()+"");

}else{

this.textField.setText("0");

}

this.temp=new StringBuffer("");

this.optType="";

this.optValue="0";

}

}

}

记住类名是app7.java 包名是main. 如果有不对的地方 到时候在找我 我在线。

Java计算器源代码

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;public class CaculatorA {

private JFrame jf;

private JButton[] jbs;

private JTextField jtf;

private JButton clear;

private double num1,num2,jieguo;

private char c;

/**

* 构造方法实例化属性

*

*/

public CaculatorA(){

jf=new JFrame("我的计算器v1.0");

jtf=new JTextField(20);

clear=new JButton("clear");

jbs=new JButton[16];

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

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

jbs[i]=new JButton(str.charAt(i)+"");

}

init();

addEventHandler();

// setFont();

// setColor();

showMe();

}

/**

关于java简易计算器源代码和简易计算器编程java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/1096.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载