這個計算器的介面模仿Windows自帶的簡易計算器,包括互動介面和和對各種輸入的回應。目前尚未實現選單列和記憶類別按鈕的功能
複製代碼代碼如下:
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicBorders;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
Container c = getContentPane();
StringBuilder number1 = new StringBuilder("");// 儲存第1個數字字串
StringBuilder number2 = new StringBuilder("");// 儲存第2個數字字串
StringBuilder operator = new StringBuilder("");// 儲存運算子
StringBuilder result = new StringBuilder("");// 儲存運算結果
JTextField numbershow = new JTextField("0.");// 數字顯示區域,初始顯示為"0."
public static void main(String[] args) {// 主方法
Calculator c1 = new Calculator();
c1.setVisible(true);
}
public Calculator() {// 計算器建構方法
setTitle("計算器");
setBounds(100, 100, 260, 245);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenuBar();// 建立功能表列
createNumberShow();// 建立數字顯示區域
createMemoShow();// 建立記憶顯示
createClearButtons();// 建立Backspace,CE,C等三個按鈕
createButtonArea();// 建立數字按鈕區域
}
private void createMenuBar() {// 建立選單列的方法
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu menu1 = new JMenu("編輯");
JMenu menu2 = new JMenu("查看");
JMenu menu3 = new JMenu("幫助");
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
JMenuItem menu1item1 = new JMenuItem("複製");
JMenuItem menu1item2 = new JMenuItem("貼上");
JMenuItem menu2item1 = new JMenuItem("標準型");
JMenuItem menu2item2 = new JMenuItem("科學型");
JMenuItem menu2item3 = new JMenuItem("科學分組");
JMenuItem menu3item1 = new JMenuItem("幫助主題");
JMenuItem menu3item2 = new JMenuItem("關於計算器");
menu1.add(menu1item1);
menu1.add(menu1item2);
menu2.add(menu2item1);
menu2.add(menu2item2);
menu2.add(menu2item3);
menu3.add(menu3item1);
menu3.add(menu3item2);
}
private void createNumberShow() {// 建立數字顯示區域的方法
numbershow.setHorizontalAlignment(JTextField.RIGHT);
numbershow.setBounds(5, 0, 245, 22);
numbershow.setEnabled(false);
numbershow.setDisabledTextColor(Color.BLACK);
c.add(numbershow);
}
private void createMemoShow() {// 建立記憶顯示的方法
JTextField memoshow = new JTextField();
memoshow.setEditable(false);
memoshow.setBounds(10, 30, 28, 22);
memoshow.setBorder(new BasicBorders.FieldBorder(Color.black,
Color.black, Color.white, Color.white));
c.add(memoshow);
}
private void createClearButtons() {// 建立Backspace,CE,C等三個按鈕的方法
JButton[] clearbutton = new JButton[3];
String[] clearbuttontext = { "Backspace", "CE", "C" };
for (int i = 0; i < 3; i++) {
clearbutton[i] = new JButton();
clearbutton[i].setText(clearbuttontext[i]);
clearbutton[i].setHorizontalAlignment(SwingConstants.CENTER);
clearbutton[i].setMargin(new Insets(0, 0, 0, 0));
clearbutton[i].setFont(new java.awt.Font("Arial", 0, 9));
clearbutton[i].setForeground(Color.blue);
clearbutton[i].setBounds(48 + i * 68, 30, 63, 22);
clearbutton[i].setForeground(Color.red);
c.add(clearbutton[i]);
}
clearbutton[0].addActionListener(new ActionListener() {// 為Backspace按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 如果未做任何輸入
showNumber(numbershow, number1);// 顯示number1
} else if (operator.toString().equals("")) {// 如果只輸入了number1
number1.deleteCharAt(number1.toString().length() - 1);// 將number1的最後一個字元去掉
showNumber(numbershow, number1);// 顯示number1
} else if (number2.toString().equals("")) {// 如果只輸入了number1和operator
showNumber(numbershow, number1);// 不做任何處理,顯示number1
} else {// 若輸入了number1、operator、number2
number2.deleteCharAt(number2.toString().length() - 1);// 將number2的最後一個字元去掉
showNumber(numbershow, number2);// 顯示number2
}
}
});
clearbutton[1].addActionListener(new ActionListener() {// 為CE按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 如果未做任何輸入
showNumber(numbershow, number1);// 顯示number1
} else if (operator.toString().equals("")) {// 如果只輸入了number1
number1.setLength(0);// 清除number1
showNumber(numbershow, number1);// 顯示number1
} else if (number2.toString().equals("")) {// 若輸入了number1和operator
showNumber(numbershow, number2);// 不做任何處理,顯示number2
} else {// 若輸入了number1、operator、number2
number2.setLength(0);// 清除number2
showNumber(numbershow, number2);// 顯示number2
}
}
});
clearbutton[2].addActionListener(new ActionListener() {// 為C按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {// 將所有儲存清零
number1.setLength(0);
number2.setLength(0);
operator.setLength(0);
numbershow.setText("0.");
result.setLength(0);
}
});
}
private void createButtonArea() {// 建立數字按鈕區域的方法
JPanel ButtonArea = new JPanel();
ButtonArea.setBounds(5, 55, 245, 125);
ButtonArea.setLayout(new GridLayout(4, 6, 5, 5));
c.add(ButtonArea);
JButton[] numberbutton = new JButton[24];
String[] numberbuttontext = { "MC", "7", "8", "9", "/", "sqrt", "MR",
"4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/X", "M+",
"0", "+/-", ".", "+", "=" };
for (int i = 0; i < 24; i++) {// 使用迴圈為這24個按鈕新增標識
numberbutton[i] = new JButton(numberbuttontext[i]);
ButtonArea.add(numberbutton[i]);
if (i % 6 == 0 || i % 6 == 4 || i == 23) {// 運算子按鈕設定為紅色
numberbutton[i].setHorizontalAlignment(SwingConstants.CENTER);
numberbutton[i].setMargin(new Insets(0, 0, 0, 0));
numberbutton[i].setFont(new java.awt.Font("Arial", 0, 9));
numberbutton[i].setForeground(Color.red);
} else {// 其它設定為藍色
numberbutton[i].setHorizontalAlignment(SwingConstants.CENTER);
numberbutton[i].setMargin(new Insets(0, 0, 0, 0));
numberbutton[i].setFont(new java.awt.Font("Arial", 0, 9));
numberbutton[i].setForeground(Color.blue);
}
}
int[] numbers = { 19, 13, 14, 15, 7, 8, 9, 1, 2, 3 };// 此數組中的數字分別代表0-9等數字在numberbuttontext數組中序號
for (int i = 0; i <= 9; i++) { // 使用迴圈為這0-9這十個數字按鈕新增監聽器
final String str = String.valueOf(i);
numberbutton[numbers[i]].addActionListener(new ActionListener() {// 為0-9按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (operator.toString().equals("")) {// 沒有輸入operator之前
add(number1, str); // 只設定number1的值
showNumber(numbershow, number1);// 只顯示number1的值
} else {// 輸入operator之後
add(number2, str);// 只設定number2的值
showNumber(numbershow, number2);// 只顯示number2的值
}
}
});
}
numberbutton[20].addActionListener(new ActionListener() { // 為"+/-"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (operator.toString().equals("")) {// 沒有輸入operator之前
add(number1, "+/-");// 只設定number1的值
showNumber(numbershow, number1);// 只顯示number1的值
} else {// 輸入operator之後
add(number2, "+/-");// 只設定number2的值
showNumber(numbershow, number2);// 只顯示number2的值
}
}
});
numberbutton[21].addActionListener(new ActionListener() { // 為"."按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (operator.toString().equals("")) {// 在輸入operator之前,只顯示number1的值
add(number1, ".");
showNumber(numbershow, number1);
} else {// 輸入operator之後,只顯示number2的值
add(number2, ".");
showNumber(numbershow, number2);
}
}
});
numberbutton[22].addActionListener(new ActionListener() { // 為"+"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("+");
}
});
numberbutton[16].addActionListener(new ActionListener() { // 為"-"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("-");
}
});
numberbutton[10].addActionListener(new ActionListener() { // 為"*"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("*");
}
});
numberbutton[4].addActionListener(new ActionListener() { // 為"/"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("/");
}
});
numberbutton[23].addActionListener(new ActionListener() { // 為"="按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 當number1為空時
showNumber(numbershow, number1);
} else if (operator.toString().equals("")) {// 當number1不為空,而operator為空時
showNumber(numbershow, number1);
} else if (number2.toString().equals("")) {// 當number1、operator皆不為空,而number2為空時
switch (operator.toString()) {
case ("+"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
+ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("-"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
- Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("*"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
* Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("/"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
/ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
}
} else {// 當number1、operator、number2皆不為空時
switch (operator.toString()) {
case ("+"): {
double d = Double.parseDouble(number1
.toString())
+ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("-"): {
double d = Double.parseDouble(number1
.toString())
- Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("*"): {
double d = Double.parseDouble(number1
.toString())
* Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("/"): {
double d = Double.parseDouble(number1
.toString())
/ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
}
}
}
});
numberbutton[17].addActionListener(new ActionListener() { // 為"1/x"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 沒有輸入number1時
numbershow.setText("除數不能為零");
} else if (operator.toString().equals("")) {// 輸入了number1,但沒有輸入operator
if (Double.parseDouble(number1.toString()) == 0) {// 若number1的值為零
numbershow.setText("除數不能為零");
} else {// 若number1的值不為零
double d = 1 / (Double.parseDouble(number1
.toString()));
number1.setLength(0);
number1.append(d);// 將number1的值開放並儲存
showNumber(numbershow, number1);
}
} else if (number2.toString().equals("")) {// 輸入了number1、operator,但沒有輸入number2
double d = 1 / (Double.parseDouble(number1
.toString()));
number2.append(d);// 將number1的值開放並儲存
showNumber(numbershow, number2);
} else {// 輸入了number1、operator,number2
double d = 1 / (Double.parseDouble(number2
.toString()));
number2.setLength(0);
number2.append(d);// 將number2的值開放並儲存
showNumber(numbershow, number2);
}
}
});
numberbutton[11].addActionListener(new ActionListener() { // 為"%"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
// 暫時空缺
}
});
numberbutton[5].addActionListener(new ActionListener() { // 為"sqrt"按鈕新增監聽器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 沒有輸入number1時
showNumber(numbershow, number1);
} else if (operator.toString().equals("")) {// 輸入了number1,但沒有輸入operator
if (Double.parseDouble(number1.toString()) < 0) {// number1小於0
numbershow.setText("函數輸入無效");
} else {// number1大於0
double d = Math.sqrt(Double.parseDouble(number1
.toString()));
number1.setLength(0);
number1.append(d);// 將number1的值開放並儲存
showNumber(numbershow, number1);
}
} else if (number2.toString().equals("")) {// 輸入了number1、operator,但沒有輸入number2
double d = Math.sqrt(Double.parseDouble(number1
.toString()));
number2.append(d);// 將number1的值開放並儲存
showNumber(numbershow, number2);
} else {// 輸入了number1、operator、number2
double d = Math.sqrt(Double.parseDouble(number2
.toString()));
number2.setLength(0);
number2.append(d);// 將number2的值開放並儲存
showNumber(numbershow, number2);
}
}
});
}
public void add(StringBuilder s1, String s2) {// 定義按鈕輸入後數字字串變化的方法
if (s2.equals("+/-")) {// 定義輸入"+/-"後數字字串的變化
if (s1.toString().equals("") || s1.toString().equals("0")) {// 如果數字字串為空或0,那麼不發生變化
s1.append("");
} else {// 如果數字字串不為空也不為0,那麼在數字字串前增加或刪除"-"字符
if (s1.toString().startsWith("-")) {
s1.deleteCharAt(0);
} else {
s1.insert(0, "-");
}
}
}
if (s2.equals(".")) {// 定義輸入"."後數字字串的變化
if (s1.toString().indexOf(".") == -1) {// 查找數字字串中是否含有"."字符,如果沒有則執行以下代碼
if (s1.toString().equals("")) {// 如果數字字串為空,那麼將數字字串設為"0."
s1.setLength(0);
s1.append("0.");
} else {
s1.append(".");
}
} else {// 若有,則不發生變化
s1.append("");
}
}
if (s2.equals("0")) {// 定義輸入"0"後數字字串的變化
if (s1.toString().equals("0")) {// 當數字的字串為"0"時,不發生變化
s1.append("");
} else {// 當數字的字串吧為"0"時,在其字串後增加"0"
s1.append("0");
}
}
for (int i = 1; i < 10; i++) {// 透過循環,定義輸入1-9後數字字串的變化
String str = String.valueOf(i);
if (s2.equals(str)) {// 定義輸入1-9後數字字串的變化
if (s1.toString().equals("0")) {
s1.setLength(0);
s1.append(str);
} else
s1.append(str);
}
}
}
public void showNumber(JTextField j, StringBuilder s) {// 定義數字顯示區域如何顯示數字字串的方法
if (s.toString().equals("") == true || s.toString().equals("0") == true) {
j.setText("0.");
} else if (s.toString().indexOf(".") == -1) {
j.setText(s.toString() + ".");
} else {
j.setText(s.toString());
}
}
}