计算器代码

package JiSuanQi;


import java.awt.*;
import java.awt.event.*;


public class MyComputer extends Frame {
private Panel mainPanel;
private Panel opPanel;


private String[] names = { "7", "8", "9", "/", "4", "5", "6", "*", "1",
"2", "3", "-", "0", "=", ".", "+" };


private Button[] button = new Button[names.length];


private TextField input = new TextField(12);


int max = 5;


private double op1 = 0D;
private double op2 = 0D;


private String operator = "";
private boolean isOp = false;


private boolean isOver = false;


public MyComputer() {
super("计算器");
}


public void init() {
mainPanel = new Panel();
mainPanel.setLayout(new BorderLayout(1, 3));


opPanel = new Panel();
opPanel.setLayout(new GridLayout(4, 4, 3, 3));


for (int i = 0; i < names.length; i++) {
button[i] = new Button(names[i]);
opPanel.add(button[i]);


try {
int n = Integer.parseInt(names[i]);
button[i].addActionListener(new NumberButtonListener());
} catch (Exception e) {
// System.out.println(names[i] + "不是数字!!!");
}


// if (names[i].equals("+/-")) {
// button[i].addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// String s = input.getText();
// if (s.startsWith("-")) {
// input.setText(s.substring(1));
// } else {
// input.setText("-" + s);
// }
//
// }
// });
// }


if (names[i].equals(".")) {
button[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = input.getText();


int n = s.indexOf(".");


if (n == -1) {
input.setText(s + e.getActionCommand());
}


}
});
}


if (names[i].equals("+") || names[i].equals("-")
|| names[i].equals("*") || names[i].equals("/")) {
button[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
operator = e.getActionCommand();


if (isOp) {


} else {
try {
String s = input.getText();


if (s.equals("")) {
s = "0";
}


op1 = Double.parseDouble(s);


} catch (Exception e1) {


}


isOp = true;
}


input.setText("");


}
});


}


if (names[i].equals("=")) {
button[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {


if (isOp) {
try {
String s = input.getText();


if (s.equals("")) {
s = "0";
}


op2 = Double.parseDouble(s);


} catch (Exception e1) {


}


input.setText("");


isOp = false;


double result = 0;


if (operator.equals("+")) {
result = op1 + op2;
} else if (operator.equals("-")) {
result = op1 - op2;


} else if (operator.equals("*")) {
result = op1 * op2;
} else if (operator.equals("/")) {
result = op1 / op2;
}


input.setText(result + "");


isOver = true;


}


}
});
}
}


mainPanel.add(input, BorderLayout.NORTH);
mainPanel.add(opPanel, BorderLayout.SOUTH);


add(mainPanel);
pack();
setVisible(true);
setResizable(false);


addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);


}


});
}


class NumberButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {


String s = input.getText();


if (isOver) {
input.setText("");
input.setText(e.getActionCommand());


isOver = false;


} else if (s.length() == max) {


} else if (s.equals("") && e.getActionCommand().equals("0")) {


} else {
input.setText(input.getText() + e.getActionCommand());
}


}
}


public static void main(String[] args) {
MyComputer app = new MyComputer();
app.init();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值