资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用java做计算器代码,java实现计算器代码

求用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.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; //导包 public class Jisuanqi extends JFrame implements ActionListener { //继承JFrame 实现事件监听 private JTextField reasult; private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnAC, btnAdd, btnSub, btnReasult, btnD, btnAbout, btnCancel; private boolean add, sub, end, s, c; private String str; private double num1, num2; public Jisuanqi() { //构造属性 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); TitledBorder tb = new TitledBorder("输出"); tb.setTitleColor(Color.BLUE); //标题边框底端线 设置颜色 btnAbout = new JButton(" 关于 "); btnCancel = new JButton("Cancel"); //两个按钮 btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.exit(0); } }); //给Cancel添加事件监听 当鼠标点击时 程序结束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黄盖!!", "消息", JOptionPane.INFORMATION_MESSAGE); } //给“关于”添加事件监听 当鼠标点击时 弹出对话框 显示黄盖 }); p3.add(btnAbout); p3.add(btnCancel); // JPanel p4=new JPanel(); // JPanel p5=new JPanel(); // reasult.setBorder(tb); reasult = new JTextField("0", 20); reasult.setEditable(false); //设置不能修改 reasult.setHorizontalAlignment(JTextField.RIGHT); // 设置文本的水平对齐方式。 reasult.setForeground(Color.BLUE); //颜色 p1.setBorder(tb); p1.add(reasult); btn0 = new JButton("0"); btn0.addActionListener(this); btn1 = new JButton("1"); btn1.addActionListener(this); btn2 = new JButton("2"); btn2.addActionListener(this); btn3 = new JButton("3"); btn3.addActionListener(this); btn4 = new JButton("4"); btn4.addActionListener(this); btn5 = new JButton("5"); btn5.addActionListener(this); btn6 = new JButton("6"); btn6.addActionListener(this); btn7 = new JButton("7"); btn7.addActionListener(this); btn8 = new JButton("8"); btn8.addActionListener(this); btn9 = new JButton("9"); btn9.addActionListener(this); btnD = new JButton("."); btnD.addActionListener(this); btnD.setForeground(Color.RED); btnAC = new JButton("AC"); btnAC.addActionListener(this); btnAC.setBackground(Color.PINK); btnAdd = new JButton("+"); btnAdd.addActionListener(this); btnAdd.setForeground(Color.BLUE); btnSub = new JButton("—"); btnSub.addActionListener(this); btnSub.setForeground(Color.BLUE); btnReasult = new JButton("="); btnReasult.addActionListener(this); btnReasult.setForeground(Color.RED); //事件监听 + 颜色 p2.add(btn1); p2.add(btn2); p2.add(btn3); p2.add(btn4); p2.add(btn5); p2.add(btn6); p2.add(btn7); p2.add(btn8); p2.add(btn9); p2.add(btn0); p2.add(btnD); p2.add(btnAC); p2.add(btnAdd); p2.add(btnSub); p2.add(btnReasult); //面板上添加按钮 p2.setLayout(new GridLayout(5, 3)); //面板上设置对齐方式 add(p1, BorderLayout.NORTH); add(p2, BorderLayout.CENTER); add(p3, BorderLayout.SOUTH); //将p1 p2 p3 面板对象添加到JFrame } public void num(int i) { String s = null; s = String.valueOf(i); if (end) { // 如果数字输入结束,则将文本框置零,重新输入 reasult.setText("0"); end = false; } if ((reasult.getText()).equals("0")) { // 如果文本框的内容为零,则覆盖文本框的内容 reasult.setText(s); } else { // 如果文本框的内容不为零,则在内容后面添加数字 str = reasult.getText() + s; reasult.setText(str); } }/* * * String s=null; * * s=String.valueOf(i); * * str=reasult.getText()+s; * * reasult.setText(str); * * } */ public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) num(1); else if (e.getSource() == btn2) num(2); else if (e.getSource() == btn3) num(3); else if (e.getSource() == btn4) num(4); else if (e.getSource() == btn5) num(5); else if (e.getSource() == btn6) num(6); else if (e.getSource() == btn7) num(7); else if (e.getSource() == btn8) num(8); else if (e.getSource() == btn9) num(9); else if (e.getSource() == btn0) num(0); else if (e.getSource() == btnAdd) { sign(1); btnD.setEnabled(true); } else if (e.getSource() == btnSub) { sign(2); btnD.setEnabled(true); } else if (e.getSource() == btnAC) { btnD.setEnabled(true); reasult.setText("0"); } else if (e.getSource() == btnD) { str = reasult.getText(); str += "."; reasult.setText(str); btnD.setEnabled(false); } else if (e.getSource() == btnReasult) { btnD.setEnabled(true); num2 = Double.parseDouble(reasult.getText()); if (add) { num1 = num1 + num2; } else if (sub) { num1 = num1 - num2; } reasult.setText(String.valueOf(num1)); end = true; } } public void sign(int s) { if (s == 1) { add = true; sub = false; } else if (s == 2) { add = false; sub = true; } num1 = Double.parseDouble(reasult.getText()); end = true; } //设计计算的过程 public static void main(String[] args) { Jisuanqi j = new Jisuanqi(); j.setTitle("+/-简易计算器"); j.setLocation(500, 280); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认关闭可以关闭程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 这个计算机,绝对让你满意

我们注重客户提出的每个要求,我们充分考虑每一个细节,我们积极的做好成都网站建设、网站设计服务,我们努力开拓更好的视野,通过不懈的努力,成都创新互联公司赢得了业内的良好声誉,这一切,也不断的激励着我们更好的服务客户。 主要业务:网站建设,网站制作,网站设计,小程序制作,网站开发,技术开发实力,DIV+CSS,PHP及ASP,ASP.Net,SQL数据库的技术开发工程师。

用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 CalcAppDemo extends Frame{

private TextField t_result;

private Panel p_main; //主面板

private Panel p_num; //数字面板

private Panel p_oper; //操作符面板

private Panel p_show; //显示面板

private Button b_num[]; //数字按钮

private Button b_oper[]; //操作符面板

public CalcAppDemo(String title){

setTitle(title);

t_result = new TextField("0.0", 21);

p_main = new Panel();

p_num = new Panel();

p_oper = new Panel();

p_show = new Panel();

p_main.setLayout(new BorderLayout());

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

p_oper.setLayout(new GridLayout(4, 2, 1, 1));

b_num = new Button[12];

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

{

b_num[i] = new Button(new Integer(i+1).toString());

}

b_num[9] = new Button("0");

b_num[10] = new Button("cls");

b_num[11] = new Button(".");

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

{

p_num.add(b_num[i]);

}

b_oper = new Button[8];

b_oper[0] = new Button("+");

b_oper[1] = new Button("-");

b_oper[2] = new Button("*");

b_oper[3] = new Button("/");

b_oper[4] = new Button("pow");

b_oper[5] = new Button("sqrt");

b_oper[6] = new Button("+/-");

b_oper[7] = new Button("=");

for(int i=0; i8; i++) //

{

p_oper.add(b_oper[i]);

}

t_result.setEditable(false);

p_show.add(t_result, BorderLayout.NORTH);

p_main.add(p_show, BorderLayout.NORTH);

p_main.add(p_num, BorderLayout.WEST);

p_main.add(p_oper, BorderLayout.EAST);

this.add(p_main, BorderLayout.CENTER);

setSize(400, 400);

setResizable(false);

pack();

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

ButtonListener b1 = new ButtonListener();

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

{

b_num[i].addActionListener(b1);

}

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

{

b_oper[i].addActionListener(b1);

}

}

class ButtonListener implements ActionListener

{

private String lastOp; //存储上一此操作符

private String strVal; //存储数字对应的字符串

private double total; //总数

private double number; //存储新输入的数

private boolean firsttime; //判断是否第一次按下的是操作符按钮

private boolean operatorPressed;//判断是否已经按过操作符按钮

ButtonListener()

{

firsttime = true;

strVal = "";

}

//事件处理器

public void actionPerformed(ActionEvent e)

{

String s = ((Button)e.getSource()).getLabel().trim();

if(Character.isDigit(s.charAt(0)))

{//判断是操作数还是操作符

handleNumber(s);

}

else

{

calculate(s);

}

}

//判断是一元操作符还是二元操作符,并根据操作符类型做计算

void calculate(String op)

{

operatorPressed = true;

if(firsttime! isUnary(op))

{

total = getNumberOnDisplay();

firsttime = false;

}

if(isUnary(op))

{

handleUnaryOp(op);

}

else if(lastOp != null)

{

handleBinaryOp(lastOp);

}

if(! isUnary(op))

{

lastOp = op;

}

}

//判断是否一元操作符

boolean isUnary(String s)

{

return s.equals("=")

||s.equals("cls")||s.equals("sqrt")

||s.equals("+/-")||s.equals(".");

}

//处理一元操作符

void handleUnaryOp(String op)

{

if(op.equals("+/-"))

{//

number = negate(getNumberOnDisplay() + "");

t_result.setText("");

t_result.setText(number + "");

return;

}else if(op.equals("."))

{

handleDecPoint();

return;

}else if(op.equals("sqrt"))

{

number = Math.sqrt(getNumberOnDisplay());

t_result.setText("");

t_result.setText(number + "");

return;

}else if(op.equals("="))

{//

if(lastOp!= null !isUnary(lastOp))

{

handleBinaryOp(lastOp);

}

lastOp = null;

firsttime = true;

return;

}else

{

clear();

}

}

//处理二元运算符

void handleBinaryOp(String op)

{

if(op.equals("+"))

{

total +=number;

}else if(op.equals("-"))

{

total -=number;

}else if(op.equals("*"))

{

total *=number;

}else if(op.equals("/"))

{

try

{

total /=number;

}catch(ArithmeticException ae){}

}else if(op.equals("pow"))

total = Math.pow(total, number);

//t_result.setText("");

lastOp = null;

// strVal = "";

number = 0;

t_result.setText(total + "");

}

//该方法用于处理数字按钮

void handleNumber(String s)

{

if(!operatorPressed)

{

strVal += s;

}else

{

operatorPressed = false;

strVal = s;

}

//

number = new Double(strVal).doubleValue();

t_result.setText("");

t_result.setText(strVal);

}

//该方法用于按下"."按钮

void handleDecPoint()

{

operatorPressed = false;

//

if(strVal.indexOf(".")0)

{

strVal += ".";

}

t_result.setText("");

t_result.setText(strVal);

}

//该方法用于将一个数求反

double negate(String s)

{

operatorPressed = false;

//如果是一个整数,去掉小数点后面的0

if(number == (int)number)

{

s = s.substring(0,s.indexOf("."));

}

//如果无"-"增加在该数的前面

if(s.indexOf("-")0)

{

strVal = "-" + s;

}

else

{

strVal = s.substring(1);

}

return new Double(strVal).doubleValue();

}

//将显示框中的值转换成Double

double getNumberOnDisplay()

{

return new Double(t_result.getText()).doubleValue();

}

//清除屏幕并设置所有的标识

void clear()

{

firsttime = true;

lastOp = null;

strVal = "";

total = 0;

number = 0;

t_result.setText("0");

}

}

public static void main(String[] args) {

CalcAppDemo c = new CalcAppDemo("简单的计算器程序");

c.setVisible(true);

}

}

怎么用JAVA编程编写一个计算器?

打开IED:打开自己java编程的软件,采用的是eclipse软件。

建立java工程。

编写类。

编写类的详细步骤

1.类的基本结构:

由于这里用到了界面,所以要进行窗口界面的编程,按钮事件的处理,和计算处理界面;

package MyCaculator;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyCaculator extends JFrame {

private int add=1,sub=2,mul=3,div=4;

private int op=0;

boolean ifOp;

private String output="0";

private Button[] jba=new Button[]{new Button("7"),new Button("8"),

new Button("9"),new Button("+"),

new Button("4"),new Button("5"),new Button("6"),new Button("-"),

new Button("1"),new Button("2"),new Button("3"),new Button("*"),

new Button("0"),new Button("."),new Button("="),new Button("/")};

private JPanel jpt=new JPanel();

private JPanel jpb=new JPanel();

private JTextField jtf=new JTextField("");

private MyCaculator(){

}

private void operate(String x){

}

public String add(String x){

return output;

}

public String subtract(String x){

return output;

}

public String multiply(String x){

return output;

}

public String divide(String x){

return output;

}

public String Equals(String x){

return output;

}

public void opClean(){

}

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

public static void main(String[] args) {

}

}

2.建立构造方法:

所谓构造方法就是,对自己的主类进行初始化,代码如下:

private MyCaculator(){

jpt.setLayout(new BorderLayout());

jpt.add(jtf);

this.add(jpt,BorderLayout.NORTH);

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

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

jpb.add(jba[i]);

if(i==3||i==7||i==11||i==15||i==14)

jba[i].addActionListener(new setOperate_Act());

else

jba[i].addActionListener(new setLabel_Act());

}

this.add(jpb,BorderLayout.CENTER);

this.setSize(250, 200);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

3.建立数据计算方法

这里的数据计算方法有6个,一个是主方法其他几个是加减乘除的处理方法,代码如下:

private void operate(String x){

double x1=Double.valueOf(x);

double y=Double.valueOf(output);

switch(op){

case 0:output=x;break;

case 1:output=String.valueOf(y+x1);break;

case 2:output =String.valueOf(y-x1);break;

case 3:output =String.valueOf(y*x1);break;

case 4:

if(x1!=0) output=String.valueOf(y/x1);

else output="不能为0";

break;

}

}

public String add(String x){

operate(x);

op=add;

return output;

}

public String subtract(String x){

operate(x);

op=sub;

return output;

}

public String multiply(String x){

operate(x);

op=mul;

return output;

}

public String divide(String x){

operate(x);

op=div;

return output;

}

public String Equals(String x){

operate(x);

op=0;

return output;

}

public void opClean(){

op=0;

output ="0";

}

4.事件处理方法

这里的时间处理方法,没有建立一个整体的方法,二是在为了便于处理的方法,将按钮事件分成两个部分,并采用两个子类来实现,这两个类时内部类要写在主类内部的,代码如下:

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jba[3]){

jtf.setText(add(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[7]){

jtf.setText(subtract(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[11]){

jtf.setText(multiply(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[15]){

jtf.setText(divide(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[14]){

jtf.setText(Equals(jtf.getText()));

ifOp=true;

}

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

Button tempb=(Button)e.getSource();

if(ifOp){

jtf.setText(tempb.getLabel());

ifOp=false;

}else {

jtf.setText(jtf.getText()+tempb.getLabel());

}

}

}

5.建立main方法:

要想实现我们的代码,我们需在main方法中,实例化我们的对象。

public static void main(String[] args) {

new MyCaculator();

}

要用java编的计算器代码

给你一个比较简单的吧。以前写的。

共两个类。还只是完成+、-、×、÷运算而已。

GUI只是用了AWT,很简单,相信一看就能懂了。

Calculator.java

public class Calculator{

private String result = "0";

private int op = 0,add = 1,sub = 2,mul = 3,div = 4;

private double stringToDouble(String x){

double y = Double.parseDouble(x);

return y;

}

private void operate(String x){

double x1 = stringToDouble(x);

double y = stringToDouble(result);

switch (op){

case 0:

result = x;

break;

case 1:

result = String.valueOf(y+x1);

break;

case 2:

result = String.valueOf(y-x1);

break;

case 3:

result = String.valueOf(y*x1);

break;

case 4:

if(x1!=0){

result = String.valueOf(y/x1);

}else{

result = "The divisor can't be zero!";

}

break;

}

}

public String opAdd(String x){

operate(x);

op = add;

return result;

}

public String opSubtract(String x){

operate(x);

op = sub;

return result;

}

public String opMultiply(String x){

operate(x);

op = mul;

return result;

}

public String opDivide(String x){

operate(x);

op = div;

return result;

}

public String opEquals(String x){

operate(x);

op = 0;

return result;

}

public void opClean(){

op = 0;

result = "0";

}

}

CalculatorGUI.java

import java.awt.*;

import java.awt.event.*;

import java.util.EventObject;

public class CalculatorGUI{

private Frame f;

private Panel p1,p2;

private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;

private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;

private TextField tf;

private String s,op;

private Calculator cal = new Calculator();

private boolean ifOp;

public CalculatorGUI(){

f = new Frame("Calculator");

p1 = new Panel();

p2 = new Panel();

b0 = new Button("0");

b1 = new Button("1");

b2 = new Button("2");

b3 = new Button("3");

b4 = new Button("4");

b5 = new Button("5");

b6 = new Button("6");

b7 = new Button("7");

b8 = new Button("8");

b9 = new Button("9");

bPoint = new Button(".");

bAdd = new Button("+");

bDec = new Button("-");

bMul = new Button("*");

bDiv = new Button("/");

bCal = new Button("=");

tf = new TextField(25);

tf.setEditable(false);

}

public void launchFrame(){

f.setSize(220,160);

f.setResizable(false);

f.addWindowListener(new myWindowListener());

p1.setLayout(new FlowLayout(FlowLayout.CENTER));

p1.add(tf);

f.add(p1,BorderLayout.NORTH);

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

b0.addActionListener(new setLabelText_ActionListener());

b1.addActionListener(new setLabelText_ActionListener());

b2.addActionListener(new setLabelText_ActionListener());

b3.addActionListener(new setLabelText_ActionListener());

b4.addActionListener(new setLabelText_ActionListener());

b5.addActionListener(new setLabelText_ActionListener());

b6.addActionListener(new setLabelText_ActionListener());

b7.addActionListener(new setLabelText_ActionListener());

b8.addActionListener(new setLabelText_ActionListener());

b9.addActionListener(new setLabelText_ActionListener());

bPoint.addActionListener(new setLabelText_ActionListener());

bAdd.addActionListener(new setOperator_ActionListener());

bDec.addActionListener(new setOperator_ActionListener());

bMul.addActionListener(new setOperator_ActionListener());

bDiv.addActionListener(new setOperator_ActionListener());

bCal.addActionListener(new setOperator_ActionListener());

p2.add(b7);

p2.add(b8);

p2.add(b9);

p2.add(bAdd);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.add(bDec);

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(bMul);

p2.add(b0);

p2.add(bPoint);

p2.add(bCal);

p2.add(bDiv);

f.add(p2,BorderLayout.SOUTH);

f.setVisible(true);

}

public void setTextFieldText_Temp(){

if (tf.getText().length()15 (tf.getText().indexOf(".")==-1 || !s.equals("."))){

tf.setText(tf.getText()+s);

}else{

tf.setText((tf.getText()+s).substring(0,15));

}

}

public void setTextFieldText(){

if(ifOp){

ifOp = false;

tf.setText("");

setTextFieldText_Temp();

}else{

setTextFieldText_Temp();

}

}

public static void main(String[] args){

CalculatorGUI calculator = new CalculatorGUI();

calculator.launchFrame();

}

class myWindowListener extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

class setLabelText_ActionListener implements ActionListener{

public void actionPerformed(ActionEvent e){

Button tempB = (Button)e.getSource();

s = tempB.getLabel();

setTextFieldText();

}

}

class setOperator_ActionListener implements ActionListener{

public void actionPerformed(ActionEvent e){

Button tempB = (Button)e.getSource();

op = tempB.getLabel();

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

tf.setText(cal.opAdd(tf.getText()));

ifOp = true;

}else if(op.equals("-")){

tf.setText(cal.opSubtract(tf.getText()));

ifOp = true;

}else if(op.equals("*")){

tf.setText(cal.opMultiply(tf.getText()));

ifOp = true;

}else if(op.equals("/")){

tf.setText(cal.opDivide(tf.getText()));

ifOp = true;

}else if(op.equals("=")){

tf.setText(cal.opEquals(tf.getText()));

ifOp = true;

}

}

}

}

计算器java代码

import java.awt.Color;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.WindowConstants;

import javax.swing.border.LineBorder;

class Normal{

double i,j;

public Normal(double num1,double num2){

i=num1;

j=num2;

}

public double puls(){

return i+j;

}

public double subtract(){

return i-j;

}

public double multiply(){

return i*j;

}

public double divide(){

return i/j;

}

public double surpuls(){

return i%j;

}

}

class scientific extends Normal{

public scientific(int num1, int num2) {

super(num1, num2);

}

}

public class calc extends JFrame{

public static void main(String[] args) {

viewNormal VN= new viewNormal("normal");

}

}

class viewNormal extends JFrame implements ActionListener{

JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));

JPanel jp2 = new JPanel(new GridLayout(5,1,5,5));

JLabel jl;

JButton[] jb;

JButton jbs,jbo,jba,jbb,jbc,jby;

StringBuffer sb = new StringBuffer();

Normal normal;

int dot=0;

double fnum=0;

double lnum=0;

double result;

String sign=null;

public viewNormal(String title){

setTitle(title);

setLayout(null);

setVisible(true);

setBounds(200,200,305,350);

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

jb= new JButton[12];

for(int i=0;i9;i++){

jb[i]=new JButton(""+(i+1));

jp1.add(jb[i]);

jb[i].addActionListener(this);

}

jb[9]=new JButton(".");

jb[10]=new JButton("0");

jb[11]=new JButton("=");

jb[9].addActionListener(this);

jb[10].addActionListener(this);

jb[11].addActionListener(this);

jp1.add(jb[9]);

jp1.add(jb[10]);

jp1.add(jb[11]);

jp1.setBounds(10, 100, 200, 200);

jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");

jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");

jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);

jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);

//jp2.add(jby);

jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);

jp2.setBounds(215, 100, 70, 200);

jl= new JLabel("0",JLabel.RIGHT);

jl.setFont(new Font("Batang",Font.BOLD, 20));

jl.setBorder(new LineBorder(Color.black,2));

jl.setBackground(Color.white);

jl.setBounds(10, 40, 275, 50);

jl.setOpaque(true);

add(jl);

add(jp1);

add(jp2);

}

//+

public void sum(){

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.puls();

result=fnum;

}

//-

private void sub() {

System.out.println(sb.toString());

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.subtract();

result=fnum;

}

//*

private void mul() {

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.multiply();

result=fnum;

}

// /

private void div() {

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.divide();

result=fnum;

}

//%

private void sur() {

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.surpuls();

result=fnum;

}

// =

private void same(){

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

sum();

}

if(sign.equals("-")){

sub();

}

if(sign.equals("*")){

mul();

}

if(sign.equals("/")){

div();

}

if(sign.equals("%")){

sur();

}

}

//result

public void Result(){

if(result%1!=0)

jl.setText(""+result);

else

{

int i=(int)result;

jl.setText(""+i);

}

}

@Override

public void actionPerformed(ActionEvent e) {

//System.out.println(sb.toString());

// 1~9

for(int i=0;i9;i++){

if(e.getSource()==jb[i]!sb.toString().equals("0")){

sb.append(jb[i].getText());

jl.setText(sb.toString());

}

else if(e.getSource()==jb[i]sb.toString().equals("0")){

int d=sb.length();

sb.delete(0, d);

sb.append(jb[i].getText());

jl.setText(sb.toString());

}

}

// 0

if(e.getSource()==jb[10]!sb.toString().equals("0")){

sb.append(jb[10].getText());

jl.setText(sb.toString());

}

// .

if(e.getSource()==jb[9]dot==0!sb.toString().equals("")){

dot++;

sb.append(jb[9].getText());

jl.setText(sb.toString());

}

// =

if(e.getSource()==jb[11]!sb.toString().equals("")){

same();

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

// +

if(e.getSource()==jbs!sb.toString().equals("")){

if(sign!="+"sign!=null)

same();

else

sum();

sign ="+";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

//-

if(e.getSource()==jbo!sb.toString().equals("")){

if(fnum==0)

fnum=2*Double.parseDouble(sb.toString());

if(sign!="-"sign!=null)

same();

else

sub();

sign ="-";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

//*

if(e.getSource()==jba!sb.toString().equals("")){

if(fnum==0)

fnum=1;

if(sign!="*"sign!=null)

same();

else

mul();

sign ="*";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

// /

if(e.getSource()==jbb!sb.toString().equals("")){

if(fnum==0)

fnum=Math.pow(Double.parseDouble(sb.toString()),2);

if(sign!="/"sign!=null)

same();

else

div();

sign ="/";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

//%

// if(e.getSource()==jby!sb.toString().equals("")){

// if(fnum==0){

// fnum=Double.parseDouble(sb.toString());

// result=fnum;

// }

// else {

// if(sign!="%"sign!=null)

// same();

// else{

// lnum=Double.parseDouble(sb.toString());

// normal=new Normal(fnum,lnum);

// fnum=normal.surpuls();

// result=fnum;

// }

// }

// sign ="%";

// Result();

// int d=sb.length();

// sb.delete(0, d);

// dot=0;

// }

//clear

if(e.getSource()==jbc){

int d=sb.length();

sb.delete(0, d);

jl.setText("0");

dot=0;

fnum=0;

lnum=0;

sign=null;

}

}

}

class viewScientific extends viewNormal{

public viewScientific(String title){

super(title);

setBounds(200,200,800,500);

}

}

//等号以后输入符号用不了, String转 double 本来就有错误,你可以用我的扩展成科学型的。


网站栏目:用java做计算器代码,java实现计算器代码
当前URL:http://www.cdkjz.cn/article/dsechos.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220