用Java写的计算器源代码

如题所述

计算器的代码我有,不过我看不了你的图片,不知道你是不是划的计算器界面还是什么。代码复制给你,直接扔到Eclipse就能跑,能用你就给分,不能用就算了哈。

package arno.shortcode;

import java.awt.Button;
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 Calculator extends WindowAdapter implements ActionListener {
private double result = 0;
private double data1 = 0;
private double radixPointDepth = 1;
private boolean radixPointIndicate = false;
private boolean resultIndicate = false;
private char prec = '+';
private Frame f;
private TextField tf;
private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18;
private Panel p;
private Panel p2;

static public void main(String args[]) {
Calculator de = new Calculator();
de.go();
}

public void go() {
f = new Frame("计算器");
p = new Panel();
p2 = new Panel();
p.setLayout(new GridLayout(4,4));
p2.setLayout(new GridLayout(1, 1));
tf = new TextField(30);
b1 = new Button("7");
b2 = new Button("8");
b3 = new Button("9");
b4 = new Button("+");
b5 = new Button("4");
b6 = new Button("5");
b7 = new Button("6");
b8 = new Button("-");
b9 = new Button("1");
b10 = new Button("2");
b11 = new Button("3");
b12 = new Button("*");
b13 = new Button("0");
b14 = new Button(".");
b15 = new Button("=");
b16 = new Button("/");
b17 = new Button("clear");
b18 = new Button("exit");
f.add(tf, "North");
f.add(p, "Center");
f.add(b18, "East");
f.add(p2, "South");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
p2.add(b17);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
f.addWindowListener(this);
f.setSize(250, 190);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
String s;
s = e.getActionCommand();
switch (s.charAt(0)) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (resultIndicate) {
result = 0;
data1 = 0;
prec = '+';
}
Integer Int1 = new Integer(s);
if (radixPointIndicate) {
radixPointDepth = radixPointDepth / 10;
data1 = data1 + (Int1.intValue()) * radixPointDepth;
} else {
data1 = data1 * 10 + (Int1.intValue());
}
Double displayNumber = new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate = false;
break;
case '+':
case '-':
case '*':
case '/':
case '=':
if (s.charAt(0) != '=' && resultIndicate) {
prec = s.charAt(0);
resultIndicate = false;
} else {
switch (prec) {
case '+':
result = result + data1;
break;
case '-':
result = result - data1;
break;
case '*':
result = result * data1;
break;
case '/':
result = result / data1;
break;
}
}
radixPointIndicate = false;
radixPointDepth = 1;
displayNumber = new Double(result);
tf.setText(displayNumber.toString());
if (s.charAt(0) != '=') {
data1 = 0;
prec = s.charAt(0);
} else {
resultIndicate = true;
}
break;
case '.':
radixPointIndicate = true;
break;
}
if (s.equals("clear")) {
result = 0;
data1 = 0;
radixPointDepth = 1;
tf.setText("");
}
if (s.equals("exit")) {
System.exit(0);
}
}

public void windowClosing(WindowEvent e) {
System.exit(0);
}
}

祝你好运

希望能够帮到你追问

不行。。。

追答

哦。那算了吧。我看不了图片,也不知道你的需求。

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-05-04
什么语言写计算器原理都是一样的、我用C#写过类似的函数计算器,如果需要拷给你
第2个回答  2012-04-23
这个的确要花点功夫啊追问

分数很多哦。。。

第3个回答  2012-04-23
我只有简易的,没有这个的

用JAVA编写的科学计算器源代码
以下是一个简单的用Java编写的科学计算器的源代码示例:java import java.util.Scanner;public class ScientificCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("Welcome to the Scientific Calculator!");System.out.println("Ente...

java最基础的代码(java基础代码实例)
importjavax.swing.*;importjavax.swing.border.Border;importjava.awt.*;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;importjava.math.BigDecimal;importjava.math.RoundingMode;importjava.util.HashMap;\/ 我的计算器。MyCalculator继承于JFrame,是计算器的界面 \/ publicclassCalcul...

求JAVA编写的 简易计算器程序,附上注释
import java.awt.*;import java.awt.event.*;public class Calculator implements ActionListener { String s="",s1;double d1,d2;JFrame jf = new JFrame("小计算器by Graduate") ;JTextField tf = new JTextField();public void init()\/\/实现计算器界面 { Container c=jf.getContentPane();...

用java编得计算器程序软件和源代码
import javax.swing.*; import java.awt.event.*; class calculation extends JFrame {public calculation() \/*构造方法*\/ {super("计数器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initTextPanel(); \/*文本框*\/ initControlPanel(); \/*控制键*\/ initKeyPanel(); \/*数字和运算符...

用JAVA编写一个计算器
\/\/ 计算器数字按钮定义面板class Number_Key extends JPanel {\/*** *\/private static final long serialVersionUID = 1L;JButton zero = new JButton("0"); \/\/ 数字键0JButton one = new JButton("1"); \/\/ 数字键1JButton two = new JButton("2"); \/\/ 数字键2JButton three = new JButton...

java怎样写一个简单的计算器
以下是一个简单的例子,这个计算器可以执行加、减、乘、除操作:import java.util.Scanner;public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("Enter first number:");double num1 = scanner.nextDouble();System....

java计算器源代码
} private void initComponents() { jPanel1 = new javax.swing.JPanel();jLabel1 = new javax.swing.JLabel();jLabel2 = new javax.swing.JLabel();jLabel3 = new javax.swing.JLabel();num1 = new javax.swing.JTextField();num2 = new javax.swing.JTextField();result = new javax...

java 写的计算器源代码只实现加减乘除四则运算即可
JFrame frame=new JFrame("sunshine---计算器");JTextField jg_TextField=new JTextField(jg,20);\/\/20列 JButton clear_Button=new JButton("清除");JButton button0=new JButton("0");JButton button1=new JButton("1");JButton button2=new JButton("2");JButton button3=new JButton...

求一个java计算器原代码,最好有文档说明。
f=new Frame("计算器");p=new Panel();p.setLayout(new GridLayout(4,4));tf=new TextField(30);b1=new Button("7");b2=new Button("8");b3=new Button("9");b4=new Button("+");b5=new Button("4");b6=new Button("5");b7=new Button("6");b8=new Button("-");b9=...

求JAVA计算器源代码!!!
f=new JFrame("计算器");p=new Panel();\/\/运算界面p p.setLayout(new GridLayout(4,4)); \/\/ 设置p的布局为GridLayout,四行四列 tf=new TextField(30);\/\/实例化按钮 b1=new Button("7");b2=new Button("8");b3=new Button("9");b4=new Button("+");b5=new Button("4");b6...

相似回答