要用java编的计算器代码

[复制链接]
查看11 | 回复3 | 2008-1-11 11:22:55 | 显示全部楼层 |阅读模式
给你一个比较简单的吧。以前写的。
共两个类。还只是完成+、-、×、÷运算而已。
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() 0)displayField.setText(str.substring(0, str.length() - 1));} else if (input.equals("CE") || input.equals("C")) {displayField.setText("0");start = true;} elsedisplayField.setText(displayField.getText() + input);}}}private class CommandAction implements ActionListener {public void actionPerformed(ActionEvent evt) {String command = evt.getActionCommand();if (start) {lastCommand = command;} else {calculate(Double.parseDouble(displayField.getText()));lastCommand = command;start = true;}}}public void calculate(double x) {if (lastCommand.equals("+"))result += x;else if (lastCommand.equals("-"))result -= x;else if (lastCommand.equals("*"))result *= x;else if (lastCommand.equals("/"))result /= x;else if (lastCommand.equals("="))result = x;displayField.setText("" + result);}public static void main(String[] args) {Calculator calculator = new Calculator();calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}
回复

使用道具 举报

千问 | 2008-1-11 11:22:55 | 显示全部楼层
import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * @authortclzsmk * 一个简单地计算器 */public class Caculator extends JPanel{ private static final long serialVersionUID = 1L; private JButton buttonDvision,buttonPlus,buttonMinus; private JButton buttonD,buttonEqual,buttonNC,buttonMupl;
private JButton[] buttons = new JButton[10]; private JTextField field; private String x = "0"; private String y = "0"; private String numbers = ""; private String symbol; private double answer = 0; private boolean exist = false;public static void main(String args[]){
GUI(); }public static void GUI(){
JFrame frame = new JFrame("caculator");frame.setContentPane(new Caculator());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}public Caculator(){
JPanel panelLeft = new JPanel();JPanel panelRight = new JPanel();JPanel panelUp = new JPanel();
for(int i = 9 ; i >= 0 ; i--){
int j = i; if(i % 3 == 0 && i >= 3){
j = j - 2; } if((i + 2) % 3 == 0 ){
j =j + 2; } buttons = new JButton("" + j); panelLeft.add(buttons);}
panelLeft.setLayout(new GridLayout(4, 3));panelRight.setLayout(new GridLayout(4, 1));
buttonD = new JButton(".");buttonEqual = new JButton("=");buttonPlus = new JButton("+");buttonMinus = new JButton("-");buttonMupl = new JButton("*");buttonDvision = new JButton("%");buttonNC = new JButton("NC");field = new JTextField("0.0",14);field.setVisible(true);field.setEditable(false);
for(int i = 0 ; i < 10 ; i++){
buttons.addActionListener(new ListenerNumber());
}
buttonD.addActionListener(new ListenerD());buttonEqual.addActionListener(new ListenerEqual());buttonPlus.addActionListener(new ListenerSymbol());buttonMinus.addActionListener(new ListenerSymbol());buttonMupl.addActionListener(new ListenerSymbol());buttonDvision.addActionListener(new ListenerSymbol());buttonNC.addActionListener(new ListenerNC());
panelLeft.add(buttonD);panelLeft.add(buttonEqual);
panelRight.add(buttonPlus);panelRight.add(buttonMinus);panelRight.add(buttonMupl);panelRight.add(buttonDvision);panelUp.add(field); panelUp.add(buttonNC);
setLayout(new BorderLayout());add(panelLeft,BorderLayout.CENTER);add(panelRight,BorderLayout.EAST);add(panelUp,BorderLayout.NORTH); }
class ListenerNumber implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource() instanceof JButton){
JButton actButton = (JButton)(event.getSource());
numbers = numbers + actButton.getText();
field.setText(numbers);
}
}
}
class ListenerD implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(exist == false && numbers != ""){
numbers+=".";
field.setText(numbers);
exist = true;
}
}
}
class ListenerSymbol implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource() instanceof JButton){
JButton actButton = (JButton)(event.getSource());
symbol = actButton.getText();
x = field.getText();
field.setText("");
numbers = "";
}
}
}
class ListenerEqual implements ActionListener {
public void actionPerformed(ActionEvent event) {try{
double a;
double b;
y = field.getText();
a = Double.parseDouble(x);
b = Double.parseDouble(y);
if(symbol == "+"){
answer = a + b;
}else if(symbol == "-"){
answer = a - b;
}else if(symbol == "*"){
answer = a * b;
}else if(symbol == "%"){
answer = a / b;
}else{
answer = b;
}
field.setText("" + answer);
exist = false;
numbers = "";
answer = 0;
symbol = "";
x = "0";
y = "0";
}catch(NumberFormatException e ){
exist = false;
numbers = "";
field.setText("0.0");
answer = 0;
symbol = "";
x = "0";
y = "0";
}
}
}
class ListenerNC implements ActionListener {
public void actionPerformed(ActionEvent event) {
symbol = "";
x = "0";
y = "0";
exist = false;
numbers = "";
field.setText("0.0");
}
} }
回复

使用道具 举报

千问 | 2008-1-11 11:22:55 | 显示全部楼层
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Keel extends JFrame implements ActionListener { private JPanel jPanel1, jPanel2; private JTextField resultField; private JButton s1, s2, s3, s4, s5, s6, s7, s8, s9, s0, b1, b2, b3, b4, f1, f2; private boolean end, add, sub, mul, div; private String str; private double num1, num2; public Keel() {super("计算器");setSize(300, 240);Container con = getContentPane(); //返回此窗体的 contentPane 对象con.setLayout(new BorderLayout()); // 布局管理设置 为BorderLayoutjPanel1 = new JPanel();
//实例化2个面板的布局jPanel1.setLayout(new GridLayout(1, 1)); //以矩形网格形式对容器的组件进行布置一行一列jPanel2 = new JPanel();jPanel2.setLayout(new GridLayout(4, 4));//以矩形网格形式对容器的组件进行布置4行4列resultField = new JTextField("0");
resultField.setHorizontalAlignment(JTextField.RIGHT); // 显示 右对齐this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ; //推出时关闭con.add(jPanel1, BorderLayout.NORTH); //把jPanel1 放入容器,北面,并把按钮加到里面con.add(jPanel2, BorderLayout.CENTER); //jPanel2 放入里面,中间jPanel1.add(resultField);
// 把JTextField 加到这个面板里面s1 = new JButton(" 1 ");s1.addActionListener(this);s2 = new JButton(" 2 ");s2.addActionListener(this);s3 = new JButton(" 3 ");s3.addActionListener(this);s4 = new JButton(" 4 ");s4.addActionListener(this);s5 = new JButton(" 5 ");s5.addActionListener(this);s6 = new JButton(" 6 ");s6.addActionListener(this);s7 = new JButton(" 7 ");s7.addActionListener(this);s8 = new JButton(" 8 ");s8.addActionListener(this);s9 = new JButton(" 9 ");s9.addActionListener(this);s0 = new JButton(" 0 ");s0.addActionListener(this);b1 = new JButton(" + ");b1.addActionListener(this);b2 = new JButton(" - ");b2.addActionListener(this);b3 = new JButton(" * ");b3.addActionListener(this);b4 = new JButton(" / ");b4.addActionListener(this);f1 = new JButton(" . ");f1.addActionListener(this);f2 = new JButton(" = ");f2.addActionListener(this);jPanel2.add(s1);jPanel2.add(s2);jPanel2.add(s3);jPanel2.add(b1);jPanel2.add(s4);jPanel2.add(s5);jPanel2.add(s6);jPanel2.add(b2);jPanel2.add(s7);jPanel2.add(s8);jPanel2.add(s9);jPanel2.add(b3);jPanel2.add(s0);jPanel2.add(f1);jPanel2.add(f2);jPanel2.add(b4);} public void num(int i) {
// 数字String s = null;s = String.valueOf(i);if (end) { // 如果数字输入结束,则将文本框置零,重新输入 resultField.setText("0"); end = false;}if ((resultField.getText()).equals("0")) { // 如果文本框的内容为零,则覆盖文本框的内容 resultField.setText(s);} else { // 如果文本框的内容不为零,则在内容后面添加数字 str = resultField.getText() + s; resultField.setText(str);} } public void actionPerformed(ActionEvent e) { if (e.getSource() == s1) num(1);else if (e.getSource() == s2) num(2);else if (e.getSource() == s3) num(3);else if (e.getSource() == s4) num(4);else if (e.getSource() == s5) num(5);else if (e.getSource() == s6) num(6);else if (e.getSource() == s7) num(7);else if (e.getSource() == s8) num(8);else if (e.getSource() == s9) num(9);else if (e.getSource() == s0) num(0);else if (e.getSource() == b1) sign(1);else if (e.getSource() == b2) sign(2);else if (e.getSource() == b3) sign(3);else if (e.getSource() == b4) sign(4);else if (e.getSource() == f1) { str = resultField.getText(); if (str.indexOf(".") <= 1) {
str += ".";
resultField.setText(str); }} else if (e.getSource() == f2) { num2 = Double.parseDouble(resultField.getText()); if (add) {
num1 = num1 + num2; } else if (sub) {
num1 = num1 - num2; } else if (mul) {
num1 = num1 * num2; } else if (div) {
num1 = num1 / num2; } resultField.setText(String.valueOf(num1)); end = true;} } public void sign(int s) {// 符号if (s == 1) { add = true; sub = false; mul = false; div = false;} else if (s == 2) { add = false; sub = true; mul = false; div = false;} else if (s == 3) { add = false; sub = false; mul = true; div = false;} else if (s == 4) { add = false; sub = false; mul = false; div = true;}num1 = Double.parseDouble(resultField.getText());end = true; }}class Keeltest{public static void main(String[] args) {Keel th1 = new Keel();th1.setVisible(true); }}
回复

使用道具 举报

千问 | 2008-1-11 11:22:55 | 显示全部楼层
好程序
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行