java程序计算器源代码 java计算器程序代码calculator

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编写的科学计算器源代码

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计算器代码

import java.awt.*;

import java.awt.event.*;

public class lvhaiya{

int tmp,sum,sum1,sum2=1,sum3=1;

String a,b,c,d;String s="";

Frame f=new Frame("计算器");

private String[]name={

"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","空格"

};

public Button[] button=new Button[name.length];

TextField t=new TextField("",30);

Panel p=new Panel();

Panel p1=new Panel();

Color color=new Color(100,170,90);

public lvhaiya(){

p1.setLayout(new GridLayout(5,5));

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

button[i]=new Button(name[i]);

p1.add(button[i]);

}

p.setLayout(new FlowLayout(FlowLayout.LEFT));

p.setBackground(color);

p.add(t);

f.add(p,BorderLayout.NORTH);

f.add(p1,BorderLayout.SOUTH);

f.pack();

f.setVisible(true);

f.addWindowListener( new WindowClose());

t.setText("0.");

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

button[i].addActionListener(new ButtonEventl());

}

}

public static void main(String[]args){

new lvhaiya();

}

class WindowClose extends WindowAdapter{

public void windouClosing(WindowEvent e){

System.exit(0);

}

}

class ButtonEventl implements ActionListener{

public void actionPerformed(ActionEvent e)throws ArithmeticException{

String command=e.getActionCommand();

if(command.equals("+")){

sum=Integer.parseInt(t.getText())+sum;

t.setText(String.valueOf(sum));

a=t.getText();

s="";

}

else if(command.equals("-")){

sum1=Integer.parseInt(t.getText());

b=t.getText();

s="";

}

else if(command.equals("*")){

sum2=Integer.parseInt(t.getText())*sum2;

t.setText(String.valueOf(sum2));

c=t.getText();

s="";

}

else if(command.equals("/")){

sum3=Integer.parseInt(t.getText());

d=t.getText();

s="";

}

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

if(a!=null){

t.setText(String.valueOf(sum+tmp));

}

if(b!=null){

t.setText(String.valueOf(sum1-tmp));

}

if(c!=null){

t.setText(String.valueOf(sum2*tmp));

}

try{

if(d!=null){

t.setText(String.valueOf(sum3/tmp));

}

}

catch(ArithmeticException a){

t.setText("除数不能为零");

}

}

else if(command.equals("空格")){

sum=0;

a=null;

b=null;

c=null;

d=null;

sum1=0;

sum2=1;

sum3=1;

tmp=0;

t.setText("0");

s="";

}

else{

s=s+command;

t.setText(s);

tmp=Integer.parseInt(s);

}

}

}

}

//按钮可以自己美化一下 希望可以帮到你

文章标题:java程序计算器源代码 java计算器程序代码calculator
分享网址:https://www.cdcxhl.com/article22/doepijc.html

成都网站建设公司_创新互联,为您提供静态网站定制开发网站制作软件开发网站改版

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营