我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java计算器源代码资源 java计算器简单代码

用JAVA编写的科学计算器源代码

import java.awt.*;

滨江网站建设公司成都创新互联,滨江网站设计制作,有大型网站制作公司丰富经验。已为滨江1000+提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的滨江做网站的公司定做!

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.BorderLayout;

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

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;

}

}

}

}

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

}

}

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计算器简单代码
浏览地址:http://shouzuofang.com/article/ddjoeoi.html

其他资讯