用java计算_如何用java编程计算

展开全部

你是问算法 还是带62616964757a686964616fe58685e5aeb931333238656661界面?

package Counter;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

class pbl extends Frame

{

double x,y,a,b;

int z;

GridLayout gl1,gl2,gl3,gl4;

Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17;

JTextField tf1;

StringBuffer str;

Panel p1,p2,p3,p4;

public pbl()

{

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

jf.setSize(180,280);

jf.setLocation(150,150);

gl1=new GridLayout(1,1,5,5);

gl2=new GridLayout(5,3,5,5);

gl3=new GridLayout(3,1,5,5);

gl4=new GridLayout(1,1,5,5);

p1=new Panel();

p1.setLayout(gl1);

tf1=new JTextField("0");

tf1.setHorizontalAlignment(JTextField.RIGHT);

p1.add(tf1);

p1.setBounds(10, 20,153, 30);

tf1.setEditable(false);

str=new StringBuffer();

btn1=new Button("1");

btn1.setForeground(Color.BLUE);

btn1.addActionListener(new ac());

btn2=new Button("2");

btn2.setForeground(Color.BLUE);

btn2.addActionListener(new ac());

btn3=new Button("3");

btn3.setForeground(Color.BLUE);

btn3.addActionListener(new ac());

btn4=new Button("4");

btn4.setForeground(Color.BLUE);

btn4.addActionListener(new ac());

btn5=new Button("5");

btn5.setForeground(Color.BLUE);

btn5.addActionListener(new ac());

btn6=new Button("6");

btn6.setForeground(Color.BLUE);

btn6.addActionListener(new ac());

btn7=new Button("7");

btn7.setForeground(Color.BLUE);

btn7.addActionListener(new ac());

btn8=new Button("8");

btn8.setForeground(Color.BLUE);

btn8.addActionListener(new ac());

btn9=new Button("9");

btn9.setForeground(Color.BLUE);

btn9.addActionListener(new ac());

btn0=new Button("0");

btn0.setForeground(Color.BLUE);

btn0.addActionListener(new ac());

btn10=new Button("+");

btn10.setForeground(Color.RED);

btn10.addActionListener(new ac());

btn11=new Button("-");

btn11.setForeground(Color.RED);

btn11.addActionListener(new ac());

btn12=new Button("*");

btn12.setForeground(Color.red);

btn12.addActionListener(new ac());

btn13=new Button("/");

btn13.setForeground(Color.RED);

btn13.addActionListener(new ac());

btn14=new Button(".");

btn14.setForeground(Color.RED);

btn14.addActionListener(new ac());

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

btn15.setForeground(Color.RED);

btn15.addActionListener(new ac());

btn16=new Button("CE");

btn16.setForeground(Color.RED);

btn16.addActionListener(new ac());

btn17=new Button("=");

btn17.setForeground(Color.RED);

btn17.addActionListener(new ac());

p2=new Panel();

p2.setLayout(gl2);

p2.add(btn16);

p2.add(btn13);

p2.add(btn12);

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(btn14);

p2.add(btn15);

p2.setBounds(10, 75, 120, 150);

p3=new Panel();

p3.setLayout(gl3);

p3.add(btn10);

p3.add(btn17);

p3.setBounds(133,105,30,182);

p4=new Panel();

p4.setLayout(gl4);

p4.add(btn11);

p4.setBounds(133, 75, 30, 26);

jf.setLayout(null);

jf.setResizable(false);

jf.add(p1);

jf.add(p2);

jf.add(p3);

jf.add(p4);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

class ac implements ActionListener

{

public void actionPerformed(ActionEvent ce)

{

if(ce.getSource()==btn16)

{

tf1.setText("0");//ce

str.setLength(0);

}

else if(ce.getSource()==btn15)//(+/-)

{

a=Double.parseDouble(tf1.getText());

//a*=-1;

tf1.setText(""+(-a));

}

else if(ce.getSource()==btn14)//(.)

{

if(tf1.getText().trim().indexOf(".")!=-1)

{

}

else if(tf1.getText().trim().equals("0"))

{

tf1.setText("0"+str.append(ce.getActionCommand()).toString());

}

else

{

tf1.setText(str.append(ce.getActionCommand()).toString());

}

}

else if(ce.getSource()==btn10)//(+)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=1;

}

else if(ce.getSource()==btn11)//(-)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=2;

}

else if(ce.getSource()==btn12)//(*)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=3;

}

else if(ce.getSource()==btn13)//(/)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=4;

}

else if(ce.getSource()==btn17)//(=)

{

str.setLength(0);

y=Double.parseDouble(tf1.getText().trim());

switch (z)

{

case 1: tf1.setText(""+(x+y)) ;break;

case 2: tf1.setText(""+(x-y)) ;break;

case 3: tf1.setText(""+(x*y)) ;break;

case 4: tf1.setText(""+(x/y)) ;break;

}

z=0;

}

else

{

tf1.setText(str.append(ce.getActionCommand()).toString());

}

}

}

public static void main(String[] args)

{

new pbl();

}

}

本回答由网友推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值