界面效果:
java
/** * */ package calculator; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.GridLayout; import javax.swing.JLabel; import java.awt.GridBagLayout; import javax.swing.JTextField; import java.awt.GridBagConstraints; import java.awt.FlowLayout; import javax.swing.SpringLayout; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Font; import javax.swing.JSpinner; import javax.swing.JFormattedTextField; import javax.swing.JCheckBox; import javax.swing.JTable; import javax.swing.JProgressBar; import javax.swing.JTextArea; import java.awt.Color; /** * @author Ocean * */ public class Calc_Main extends JFrame { private JTextField textField; private JTable table; String s = "";// 顯示算式 long[] num = new long[100];// 儲存運算數據 char[] b = new char[100];// 儲存運算符 int i = 0;// 輸入運算數據和運算符是給數組計數 int c;// 計算結果時計數 long jieguo = 0;// 計算結果 /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calc_Main frame = new Calc_Main(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * 構造方法 * Create the frame. */ public Calc_Main() { setFont(new Font("Dialog", Font.PLAIN, 12)); setTitle("計算器"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(800, 300, 382, 494); getContentPane().setLayout(null); textField = new JTextField(); textField.setForeground(new Color(102, 51, 255)); textField.setFont(new Font("宋體", Font.PLAIN, 18)); textField.setBounds(10, 10, 344, 50); getContentPane().add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("C"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s=""; textField.setText(s);//清空文本框 for(c=0;c<100;c++) { num[c]=0;//清空運算數據 b[c]=0;//清空運算符 } i=0; c=0; jieguo=0; } }); btnNewButton.setBounds(20, 99, 65, 40); getContentPane().add(btnNewButton); JButton button = new JButton("/"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s=s+"/"; textField.setText(s);//在文本框中顯示相應運算符 b[i]='/'; i++; //輸入運算符號後給數組地址+1存放後面的輸入 } }); button.setBounds(107, 99, 65, 40); getContentPane().add(button); JButton button_1 = new JButton("*"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s=s+"*"; textField.setText(s);//在文本框中顯示相應運算符 b[i]='*'; i++; } }); button_1.setBounds(189, 99, 65, 40); getContentPane().add(button_1); JButton button_2 = new JButton("-"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s=s+"-"; textField.setText(s);//在文本框中顯示相應運算符 b[i]='-'; i++; } }); button_2.setBounds(276, 99, 65, 40); getContentPane().add(button_2); JButton button_3 = new JButton("1"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { s += "1"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 1; } }); button_3.setBounds(20, 170, 65, 40); getContentPane().add(button_3); JButton button_4 = new JButton("2"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "2"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 2; } }); button_4.setBounds(107, 170, 65, 40); getContentPane().add(button_4); JButton button_5 = new JButton("3"); button_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "3"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 3; } }); button_5.setBounds(189, 170, 65, 40); getContentPane().add(button_5); JButton button_6 = new JButton("+"); button_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s=s+"+"; textField.setText(s);//在文本框中顯示相應運算符 b[i]='+'; i++; } }); button_6.setBounds(276, 170, 65, 116); getContentPane().add(button_6); JButton button_7 = new JButton("4"); button_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "4"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 4; } }); button_7.setBounds(20, 246, 65, 40); getContentPane().add(button_7); JButton button_8 = new JButton("5"); button_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "5"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 5; } }); button_8.setBounds(107, 246, 65, 40); getContentPane().add(button_8); JButton button_9 = new JButton("6"); button_9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "6"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 6; } }); button_9.setBounds(189, 246, 65, 40); getContentPane().add(button_9); JButton button_11 = new JButton("7"); button_11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "7"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 7; } }); button_11.setBounds(20, 315, 65, 40); getContentPane().add(button_11); JButton button_12 = new JButton("8"); button_12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "8"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 8; } }); button_12.setBounds(107, 315, 65, 40); getContentPane().add(button_12); JButton button_13 = new JButton("9"); button_13.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "9"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 9; } }); button_13.setBounds(189, 315, 65, 40); getContentPane().add(button_13); JButton button_14 = new JButton("="); button_14.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { switch(b[0])//爲了方便用循環來計算,我想到的是先把第一次計算提出來 { case '+': { jieguo=num[0]+num[1]; break; } case '-': { jieguo=num[0]-num[1]; break; } case '*': { jieguo=num[0]*num[1]; break; } case '/': { jieguo=num[0]/num[1]; break; } } c++; // 一個算式屢次運算 for(c=1;c<i;c++) { switch(b[c]) { case '+': { jieguo=jieguo+num[c+1]; break; } case '-': { jieguo=jieguo-num[c+1]; break; } case '*': { jieguo=jieguo*num[c+1]; break; } case '/': { jieguo=jieguo/num[c+1]; break; } } } textField.setText(String.valueOf(jieguo));//.setText()的參數須要String,因此用String.valueOf()將long轉換爲String } }); button_14.setBounds(276, 315, 65, 104); getContentPane().add(button_14); JButton button_15 = new JButton("0"); button_15.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s += "0"; textField.setText(s);//在文本框中顯示相應數字 num[i] = num[i] * 10 + 0; } }); button_15.setBounds(20, 379, 152, 40); getContentPane().add(button_15); JButton button_17 = new JButton("."); button_17.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //還沒想出來怎麼整,有知道的大佬還請多指教`` } }); button_17.setBounds(189, 379, 65, 40); getContentPane().add(button_17); JLabel label = new JLabel("注意本計算機沒有計算優先級(一概從左到右計算)"); label.setBounds(22, 430, 321, 15); getContentPane().add(label); } }