?? calculator.java
字號(hào):
//frame版程序源代碼如下,疏漏之處,望批評指正。
//數(shù)字分組沒有編寫,科學(xué)型計(jì)算器沒有編寫,其他已經(jīng)完善。
import java.awt.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Calculator
implements ActionListener {
JFrame frame;
JTextField textAnswer;
JPanel panel, panel1, panel2, panel3;
JMenuBar mainMenu;
JTextField textMemory;
JLabel labelMemSpace; //labelMemSpace單純做擺設(shè),控制面板的形狀
JButton buttonBk, buttonCe, buttonC;
JButton button[];
JButton buttonMC, buttonMR, buttonMS, buttonMAdd;
JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
buttonDiv, buttonMod;
JButton buttonSqrt, buttonDao, buttonEqual;
JMenu editMenu, viewMenu, helpMenu;
JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal;
DecimalFormat df; //設(shè)置數(shù)據(jù)輸出精度
boolean clickable; //控制當(dāng)前能否按鍵
double memoryd; //使用內(nèi)存中存儲(chǔ)的數(shù)字
int memoryi;
double vard, answerd; //用來保存double型數(shù)據(jù)的中間值(vard)和最后結(jié)果(answerd)
short key = -1, prekey = -1; //key用來保存當(dāng)前進(jìn)行何種運(yùn)算,prekey用來保存前次進(jìn)行何種運(yùn)算
String copy; //做復(fù)制用
JTextArea help; //幫助
JScrollPane scrollHelp;
//構(gòu)造函數(shù)
public Calculator() {
clickable = true;
answerd = 0;
frame = new JFrame("計(jì)算器");
df = new DecimalFormat("0.##############"); //設(shè)置數(shù)據(jù)輸出精度(對于double型值)
textAnswer = new JTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(new Color(255, 255, 255));
panel = new JPanel();
frame.getContentPane().add(panel);
panel1 = new JPanel();
panel2 = new JPanel();
panel.setLayout(new BorderLayout());
//設(shè)計(jì)整個(gè)面板
mainMenu = new JMenuBar();
editMenu = new JMenu("編輯(E)");
viewMenu = new JMenu("查看(V)");
helpMenu = new JMenu("幫助(H)");
copyItem = new JMenuItem(" 復(fù)制(C) Ctrl+C");
copyItem.addActionListener(this);
pasteItem = new JMenuItem(" 粘貼(V) Ctrl+V");
pasteItem.addActionListener(this);
editMenu.add(copyItem);
editMenu.add(pasteItem);
tItem = new JMenuItem("●標(biāo)準(zhǔn)型(T)");
tItem.addActionListener(this);
sItem = new JMenuItem(" 科學(xué)型(S)");
sItem.addActionListener(this);
numberGroup = new JMenuItem(" 數(shù)字分組(I)");
numberGroup.addActionListener(this);
viewMenu.add(tItem);
viewMenu.add(sItem);
viewMenu.add(numberGroup);
topHelp = new JMenuItem(" 幫助主題(H)");
topHelp.addActionListener(this);
help = new JTextArea(5, 20);
scrollHelp = new JScrollPane(help);
help.setEditable(false);
help.append("執(zhí)行簡單計(jì)算\n");
help.append("1. 鍵入計(jì)算的第一個(gè)數(shù)字。\n");
help.append("2. 單擊“+”執(zhí)行加、“-”執(zhí)行減、“*”執(zhí)行乘或“/”執(zhí)行除。\n");
help.append("3. 鍵入計(jì)算的下一個(gè)數(shù)字。\n");
help.append("4. 輸入所有剩余的運(yùn)算符和數(shù)字。\n");
help.append("5. 單擊“=”。\n");
aboutCal = new JMenuItem(" 關(guān)于計(jì)算器(A)");
aboutCal.addActionListener(this);
helpMenu.add(topHelp);
helpMenu.add(aboutCal);
mainMenu.add(editMenu);
mainMenu.add(viewMenu);
mainMenu.add(helpMenu);
panel.add(mainMenu, BorderLayout.NORTH);
panel.add(textAnswer, BorderLayout.CENTER);
panel.add(panel1, BorderLayout.SOUTH);
panel1.setLayout(new BorderLayout());
textMemory = new JTextField(3);
textMemory.setEditable(false);
textMemory.setBackground(new Color(217, 217, 217));
labelMemSpace = new JLabel(" ");
buttonBk = new JButton("Backspace");
buttonBk.setForeground(new Color(255, 0, 0));
buttonCe = new JButton("CE");
buttonCe.setForeground(new Color(255, 0, 0));
buttonC = new JButton("C");
buttonC.setForeground(new Color(255, 0, 0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2, BorderLayout.NORTH);
panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel2.add(textMemory);
panel2.add(labelMemSpace);
panel2.add(buttonBk);
panel2.add(buttonCe);
panel2.add(buttonC);
panel3 = new JPanel();
panel1.add(panel3, BorderLayout.CENTER);
button = new JButton[10];
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(Integer.toString(i));
button[i].setForeground(new Color(0, 0, 255));
}
buttonMC = new JButton("MC");
buttonMC.setForeground(new Color(255, 0, 0));
buttonMR = new JButton("MR");
buttonMR.setForeground(new Color(255, 0, 0));
buttonMS = new JButton("MS");
buttonMS.setForeground(new Color(255, 0, 0));
buttonMAdd = new JButton("M+");
buttonMAdd.setForeground(new Color(255, 0, 0));
buttonDot = new JButton(".");
buttonDot.setForeground(new Color(0, 0, 255));
buttonAddAndSub = new JButton("+/-");
buttonAddAndSub.setForeground(new Color(0, 0, 255));
buttonAdd = new JButton("+");
buttonAdd.setForeground(new Color(255, 0, 0));
buttonSub = new JButton("-");
buttonSub.setForeground(new Color(255, 0, 0));
buttonMul = new JButton("*");
buttonMul.setForeground(new Color(255, 0, 0));
buttonDiv = new JButton("/");
buttonDiv.setForeground(new Color(255, 0, 0));
buttonMod = new JButton("%");
buttonMod.setForeground(new Color(0, 0, 255));
buttonSqrt = new JButton("sqrt");
buttonSqrt.setForeground(new Color(0, 0, 255));
buttonDao = new JButton("1/x");
buttonDao.setForeground(new Color(0, 0, 255));
buttonEqual = new JButton("=");
buttonEqual.setForeground(new Color(255, 0, 0));
//將所有行為與監(jiān)聽綁定
panel3.setLayout(new GridLayout(4, 6));
panel3.add(buttonMC);
buttonMC.addActionListener(this);
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel3.add(buttonSqrt);
buttonSqrt.addActionListener(this);
panel3.add(buttonMR);
buttonMR.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel3.add(buttonMod);
buttonMod.addActionListener(this);
panel3.add(buttonMS);
buttonMS.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(buttonDao);
buttonDao.addActionListener(this);
panel3.add(buttonMAdd);
buttonMAdd.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonAddAndSub);
buttonAddAndSub.addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
//設(shè)置各個(gè)按鈕行為
public void actionPerformed(ActionEvent event) {
boolean sign = false; //判斷是否是double型數(shù)參與運(yùn)算,是為true,不是為false
Object temp = event.getSource();
try {
//如果按下數(shù)據(jù)按鈕,將按下的按鈕代表的數(shù)據(jù)插入的當(dāng)前文本框字符串之后
for (int i = 0; i <= 9; i++)
if (temp == button[i] && clickable == true)
textAnswer.setText(textAnswer.getText() + Integer.toString(i));
//按下'.'按鈕時(shí),判斷當(dāng)前文本框內(nèi)字符串中含不含'.',如果已含,則不允許再插入'.'
if (temp == buttonDot && clickable == true) {
boolean isDot = false;
if (textAnswer.getText().length() == 0)
isDot = true;
for (int i = 0; i < textAnswer.getText().length(); i++)
if ('.' == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
if (isDot == false)
textAnswer.setText(textAnswer.getText() + ".");
}
if ( (temp == buttonAdd || temp == buttonSub || temp == buttonMul ||
temp == buttonDiv) && clickable == true) {
//'+'操作
if (temp == buttonAdd) {
switch (prekey) {
case 0:
answerd += Double.parseDouble(textAnswer.getText());
break;
case 1:
answerd -= Double.parseDouble(textAnswer.getText());
break;
case 2:
answerd *= Double.parseDouble(textAnswer.getText());
break;
case 3:
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除數(shù)不能為零");
clickable = false;
}
else
answerd /= Double.parseDouble(textAnswer.getText());
break;
default:
answerd = Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey = key = 0;
}
//'-'操作
if (temp == buttonSub) {
switch (prekey) {
case 0:
answerd += Double.parseDouble(textAnswer.getText()); textAnswer.setText(df.format(Double.toString(answerd)));
break;
case 1:
answerd -= Double.parseDouble(textAnswer.getText()); textAnswer.setText(df.format(Double.toString(answerd)));
break;
case 2:
answerd *= Double.parseDouble(textAnswer.getText()); textAnswer.setText(df.format(Double.toString(answerd)));
break;
case 3:
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除數(shù)不能為零");
clickable = false;
}
else
answerd /= Double.parseDouble(textAnswer.getText());
break;
default:
answerd = Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey = key = 1;
}
//'*'操作
if (temp == buttonMul) {
switch (prekey) {
case 0:
answerd += Double.parseDouble(textAnswer.getText()); textAnswer.setText(df.format(Double.toString(answerd)));
break;
case 1:
answerd -= Double.parseDouble(textAnswer.getText()); textAnswer.setText(df.format(Double.toString(answerd)));
break;
case 2:
answerd *= Double.parseDouble(textAnswer.getText()); textAnswer.setText(df.format(Double.toString(answerd)));
break;
case 3:
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除數(shù)不能為零");
clickable = false;
}
else
answerd /= Double.parseDouble(textAnswer.getText());
break;
default:
answerd = Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -