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