ç»ä½ 个ç°æç,ç´æ¥ç¼è¯è¿è¡,请é纳
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* ä¸ä¸ªè®¡ç®å¨ï¼ä¸Windowsé件èªå¸¦è®¡ç®å¨çæ åçåè½ãçé¢ç¸ä»¿ã ä½è¿ä¸æ¯æé®çæä½ã
*/
public class JCalculator extends JFrame implements ActionListener {
/** 计ç®å¨ä¸çé®çæ¾ç¤ºåå */
private final String[] KEYS = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
"*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
/** 计ç®å¨ä¸çåè½é®çæ¾ç¤ºåå */
private final String[] COMMAND = { "Backspace", "CE", "C" };
/** 计ç®å¨å·¦è¾¹çMçæ¾ç¤ºåå */
private final String[] M = { " ", "MC", "MR", "MS", "M+" };
/** 计ç®å¨ä¸é®çæé® */
private JButton keys[] = new JButton[KEYS.length];
/** 计ç®å¨ä¸çåè½é®çæé® */
private JButton commands[] = new JButton[COMMAND.length];
/** 计ç®å¨å·¦è¾¹çMçæé® */
private JButton m[] = new JButton[M.length];
/** 计ç®ç»æææ¬æ¡ */
private JTextField resultText = new JTextField("0");
// æ å¿ç¨æ·æçæ¯å¦æ¯æ´ä¸ªè¡¨è¾¾å¼ç第ä¸ä¸ªæ°å,æè
æ¯è¿ç®ç¬¦åç第ä¸ä¸ªæ°å
private boolean firstDigit = true;
// 计ç®çä¸é´ç»æã
private double resultNum = 0.0;
// å½åè¿ç®çè¿ç®ç¬¦
private String operator = "=";
// æä½æ¯å¦åæ³
private boolean operateValidFlag = true;
/**
* æé å½æ°
*/
public JCalculator() {
super();
// åå§å计ç®å¨
init();
// 设置计ç®å¨çèæ¯é¢è²
this.setBackground(Color.LIGHT_GRAY);
this.setTitle("计ç®å¨");
// å¨å±å¹(500, 300)åæ å¤æ¾ç¤ºè®¡ç®å¨
this.setLocation(500, 300);
// ä¸è®¸ä¿®æ¹è®¡ç®å¨ç大å°
this.setResizable(false);
// 使计ç®å¨ä¸åç»ä»¶å¤§å°åé
this.pack();
}
/**
* åå§å计ç®å¨
*/
private void init() {
// ææ¬æ¡ä¸çå
容éç¨å³å¯¹é½æ¹å¼
resultText.setHorizontalAlignment(JTextField.RIGHT);
// ä¸å
许修æ¹ç»æææ¬æ¡
resultText.setEditable(false);
// 设置ææ¬æ¡èæ¯é¢è²ä¸ºç½è²
resultText.setBackground(Color.white);
// åå§å计ç®å¨ä¸é®çæé®ï¼å°é®æ¾å¨ä¸ä¸ªç»æ¿å
JPanel calckeysPanel = new JPanel();
// ç¨ç½æ ¼å¸å±å¨ï¼4è¡ï¼5åçç½æ ¼ï¼ç½æ ¼ä¹é´çæ°´å¹³æ¹åé´é为3ä¸ªè±¡ç´ ï¼åç´æ¹åé´é为3个象ç´
calckeysPanel.setLayout(new GridLayout(4, 5, 3, 3));
for (int i = 0; i < KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
calckeysPanel.add(keys[i]);
keys[i].setForeground(Color.blue);
}
// è¿ç®ç¬¦é®ç¨çº¢è²æ 示ï¼å
¶ä»é®ç¨èè²è¡¨ç¤º
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);
// åå§ååè½é®ï¼é½ç¨çº¢è²æ 示ãå°åè½é®æ¾å¨ä¸ä¸ªç»æ¿å
JPanel commandsPanel = new JPanel();
// ç¨ç½æ ¼å¸å±å¨ï¼1è¡ï¼3åçç½æ ¼ï¼ç½æ ¼ä¹é´çæ°´å¹³æ¹åé´é为3ä¸ªè±¡ç´ ï¼åç´æ¹åé´é为3个象ç´
commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
for (int i = 0; i < COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
commandsPanel.add(commands[i]);
commands[i].setForeground(Color.red);
}
// åå§åMé®ï¼ç¨çº¢è²æ 示ï¼å°Mé®æ¾å¨ä¸ä¸ªç»æ¿å
JPanel calmsPanel = new JPanel();
// ç¨ç½æ ¼å¸å±ç®¡çå¨ï¼5è¡ï¼1åçç½æ ¼ï¼ç½æ ¼ä¹é´çæ°´å¹³æ¹åé´é为3ä¸ªè±¡ç´ ï¼åç´æ¹åé´é为3个象ç´
calmsPanel.setLayout(new GridLayout(5, 1, 3, 3));
for (int i = 0; i < M.length; i++) {
m[i] = new JButton(M[i]);
calmsPanel.add(m[i]);
m[i].setForeground(Color.red);
}
// ä¸é¢è¿è¡è®¡ç®å¨çæ´ä½å¸å±ï¼å°calckeysåcommandç»æ¿æ¾å¨è®¡ç®å¨çä¸é¨ï¼
// å°ææ¬æ¡æ¾å¨åé¨ï¼å°calmsç»æ¿æ¾å¨è®¡ç®å¨ç西é¨ã
// æ°å»ºä¸ä¸ªå¤§çç»æ¿ï¼å°ä¸é¢å»ºç«çcommandåcalckeysç»æ¿æ¾å¨è¯¥ç»æ¿å
JPanel panel1 = new JPanel();
// ç»æ¿éç¨è¾¹çå¸å±ç®¡çå¨ï¼ç»æ¿éç»ä»¶ä¹é´çæ°´å¹³ååç´æ¹åä¸é´éé½ä¸º3象ç´
panel1.setLayout(new BorderLayout(3, 3));
panel1.add("North", commandsPanel);
panel1.add("West", calckeysPanel);
// 建ç«ä¸ä¸ªç»æ¿æ¾ææ¬æ¡
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.add("Center", resultText);
// æ´ä½å¸å±
getContentPane().setLayout(new BorderLayout(3, 5));
getContentPane().add("North", top);
getContentPane().add("Center", panel1);
getContentPane().add("West", calmsPanel);
// 为åæé®æ·»å äºä»¶ä¾¦å¬å¨
// é½ä½¿ç¨åä¸ä¸ªäºä»¶ä¾¦å¬å¨ï¼å³æ¬å¯¹è±¡ãæ¬ç±»ç声æä¸æimplements ActionListener
for (int i = 0; i < KEYS.length; i++) {
keys[i].addActionListener(this);
}
for (int i = 0; i < COMMAND.length; i++) {
commands[i].addActionListener(this);
}
for (int i = 0; i < M.length; i++) {
m[i].addActionListener(this);
}
}
/**
* å¤çäºä»¶
*/
public void actionPerformed(ActionEvent e) {
// è·åäºä»¶æºçæ ç¾
String label = e.getActionCommand();
if (label.equals(COMMAND[0])) {
// ç¨æ·æäº"Backspace"é®
handleBackspace();
} else if (label.equals(COMMAND[1])) {
// ç¨æ·æäº"CE"é®
resultText.setText("0");
} else if (label.equals(COMMAND[2])) {
// ç¨æ·æäº"C"é®
handleC();
} else if ("0123456789.".indexOf(label) >= 0) {
// ç¨æ·æäºæ°åé®æè
å°æ°ç¹é®
handleNumber(label);
// handlezero(zero);
} else {
// ç¨æ·æäºè¿ç®ç¬¦é®
handleOperator(label);
}
}
/**
* å¤çBackspaceé®è¢«æä¸çäºä»¶
*/
private void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i > 0) {
// éæ ¼ï¼å°ææ¬æåä¸ä¸ªå符å»æ
text = text.substring(0, i - 1);
if (text.length() == 0) {
// å¦æææ¬æ²¡æäºå
容ï¼ååå§å计ç®å¨çåç§å¼
resultText.setText("0");
firstDigit = true;
operator = "=";
} else {
// æ¾ç¤ºæ°çææ¬
resultText.setText(text);
}
}
}
/**
* å¤çæ°åé®è¢«æä¸çäºä»¶
*
* @param key
*/
private void handleNumber(String key) {
if (firstDigit) {
// è¾å
¥ç第ä¸ä¸ªæ°å
resultText.setText(key);
} else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) {
// è¾å
¥çæ¯å°æ°ç¹ï¼å¹¶ä¸ä¹å没æå°æ°ç¹ï¼åå°å°æ°ç¹éå¨ç»æææ¬æ¡çåé¢
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
// å¦æè¾å
¥çä¸æ¯å°æ°ç¹ï¼åå°æ°åéå¨ç»æææ¬æ¡çåé¢
resultText.setText(resultText.getText() + key);
}
// 以åè¾å
¥çè¯å®ä¸æ¯ç¬¬ä¸ä¸ªæ°åäº
firstDigit = false;
}
/**
* å¤çCé®è¢«æä¸çäºä»¶
*/
private void handleC() {
// åå§å计ç®å¨çåç§å¼
resultText.setText("0");
firstDigit = true;
operator = "=";
}
/**
* å¤çè¿ç®ç¬¦é®è¢«æä¸çäºä»¶
*
* @param key
*/
private void handleOperator(String key) {
if (operator.equals("/")) {
// é¤æ³è¿ç®
// å¦æå½åç»æææ¬æ¡ä¸çå¼çäº0
if (getNumberFromText() == 0.0) {
// æä½ä¸åæ³
operateValidFlag = false;
resultText.setText("é¤æ°ä¸è½ä¸ºé¶");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("1/x")) {
// åæ°è¿ç®
if (resultNum == 0.0) {
// æä½ä¸åæ³
operateValidFlag = false;
resultText.setText("é¶æ²¡æåæ°");
} else {
resultNum = 1 / resultNum;
}
} else if (operator.equals("+")) {
// å æ³è¿ç®
resultNum += getNumberFromText();
} else if (operator.equals("-")) {
// åæ³è¿ç®
resultNum -= getNumberFromText();
} else if (operator.equals("*")) {
// ä¹æ³è¿ç®
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
// å¹³æ¹æ ¹è¿ç®
resultNum = Math.sqrt(resultNum);
} else if (operator.equals("%")) {
// ç¾åå·è¿ç®ï¼é¤ä»¥100
resultNum = resultNum / 100;
} else if (operator.equals("+/-")) {
// æ£æ°è´æ°è¿ç®
resultNum = resultNum * (-1);
} else if (operator.equals("=")) {
// èµå¼è¿ç®
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// å精度浮ç¹æ°çè¿ç®
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
// è¿ç®ç¬¦çäºç¨æ·æçæé®
operator = key;
firstDigit = true;
operateValidFlag = true;
}
/**
* ä»ç»æææ¬æ¡ä¸è·åæ°å
*
* @return
*/
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e) {
}
return result;
}
public static void main(String args[]) {
JCalculator JCalculator1 = new JCalculator();
JCalculator1.setVisible(true);
JCalculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
追é®è°¢è°¢
追çä¸å®¢æ°