下面是我和我朋友編寫的一個小程序,它們之間可以相互轉(zhuǎn)換。 它們是我們通過參考網(wǎng)上的代碼參考得來的。 下面有四個源文件,我都用----------幫你格開了。 ------------------------------------------------------------------------------------------------ //時間轉(zhuǎn)換 import java.awt.BorderLayout; import java.awt.Button; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextArea; import javax.swing.JTextField; import java.awt.Color; import javax.swing.JButton; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.util.Stack; import java.awt.event.ActionEvent; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import java.awt.Font; import java.awt.GridLayout; public class Calculator_Time extends JFrame implements ActionListener { private final String[] KEYS = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "=",}; /** 計算器上的功能鍵的顯示名字 final p86*/ private final String[] COMMAND = { "Backspace", "CE", "C" }; /** 計算器左邊的M的顯示名字 */ private final String[] M = {"小時化分鐘","分鐘化小時", "分鐘化秒","秒化分鐘","秒化小時","小時化秒", }; /** 計算器上鍵的按鈕 */ private JButton keys[] = new JButton[KEYS.length]; /** 計算器上的功能鍵的按鈕 */ private JButton commands[] = new JButton[COMMAND.length]; /** 計算器左邊的M的按鈕 */ private JButton m[] = new JButton[M.length]; /** 計算結(jié)果文本框 */ private JTextField resultText = new JTextField("0"); // 標(biāo)志用戶按的是否是整個表達(dá)式的第一個數(shù)字,或者是運(yùn)算符后的第一個數(shù)字 private boolean firstDigit = true; // 計算的中間結(jié)果,。 private double resultNum = 0.0; // 當(dāng)前運(yùn)算的運(yùn)算符 private String operator = "="; // 操作是否合法 private boolean operateValidFlag = true; StringBuffer sb = new StringBuffer(); // 存儲輸入的數(shù)字 String vl = null; // 存儲事件點(diǎn)擊的字符 //標(biāo)準(zhǔn)型計算器 public Calculator_Time() { setTitle("標(biāo)準(zhǔn)型"); // 初始化計算器 super p114 // 設(shè)置計算器的背景顏色 this.setBackground(Color.LIGHT_GRAY); this.setTitle("計算器"); // 在屏幕(500, 300)坐標(biāo)處顯示計算器 this.setLocation(500, 300); // 不許修改計算器的大小 this.setSize(600, 400); this.setResizable(false); // 使計算器中各組件大小合適 //使這個外部容器自動調(diào)整成剛好裝下這幾個按鈕的大小的尺寸,。 JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("功能"); menuBar.add(menu); JMenuItem menuItem = new JMenuItem("標(biāo)準(zhǔn)型"); menu.add(menuItem); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Calculate_Ordinary ordinary = new Calculate_Ordinary(); ordinary.setVisible(true); } }); JMenuItem menuItem_1 = new JMenuItem("科學(xué)型"); menuItem_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Calculator_Science science = new Calculator_Science(); science.setVisible(true); } }); menu.add(menuItem_1); JMenuItem menuItem_2 = new JMenuItem("時間換算"); menu.add(menuItem_2); JMenu menu_1 = new JMenu("關(guān)于"); menuBar.add(menu_1); JMenuItem menuItem_4 = new JMenuItem("使用幫助"); menu_1.add(menuItem_4); JMenuItem menuItem_5 = new JMenuItem("關(guān)于作者"); menu_1.add(menuItem_5); menuItem_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Calculator_Author calculator_Author = new Calculator_Author(); calculator_Author.setVisible(true); } }); resultText.setHorizontalAlignment(JTextField.RIGHT); // 設(shè)置文本框背景顏色為白色 resultText.setBackground(Color.white); resultText.setFont(new Font("TimesRoman",Font.BOLD,46)); // 初始化計算器上鍵的按鈕,,將鍵放在一個畫板內(nèi) JPanel calckeysPanel = new JPanel(); calckeysPanel.setLayout(new GridLayout(4, 3, 3, 3)); for (int i = 0; i < KEYS.length; i++) { keys[i] = new JButton(KEYS[i]); calckeysPanel.add(keys[i]); keys[i].setForeground(Color.white); keys[i].setBackground(Color.DARK_GRAY); keys[i].setFont(new Font("Courir",Font.PLAIN,20)); } // 運(yùn)算符鍵用紅色標(biāo)示,,其他鍵用黑色表示 // 初始化功能鍵,都用紅色標(biāo)示,。將功能鍵放在一個畫板內(nèi) JPanel commandsPanel = new JPanel(); // 用網(wǎng)格布局器,,1行,,3列的網(wǎng)格,網(wǎng)格之間的水平方向間隔為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].setBackground(Color.white); commands[i].setForeground(Color.DARK_GRAY); commands[i].setFont(new Font("Courir",Font.PLAIN,20)); } // 初始化M鍵,用紅色標(biāo)示,,將M鍵放在一個畫板內(nèi) JPanel calmsPanel = new JPanel(); // 用網(wǎng)格布局管理器,5行,,1列的網(wǎng)格,,網(wǎng)格之間的水平方向間隔為3個象素,垂直方向間隔為3個象素 calmsPanel.setLayout(new GridLayout(3, 2, 3, 3)); for (int i = 0; i < M.length; i++) { m[i] = new JButton(M[i]); m[i].setBackground(Color.white); calmsPanel.add(m[i]); } // 下面進(jìn)行計算器的整體布局,,將calckeys和command畫板放在計算器的中部, // 將文本框放在北部,將calms畫板放在計算器的西部。 // 新建一個大的畫板,將上面建立的command和calckeys畫板放在該畫板內(nèi) JPanel panel1 = new JPanel(); // 畫板采用邊界布局管理器,,畫板里組件之間的水平和垂直方向上間隔都為3象素 panel1.setLayout(new BorderLayout(3, 3)); panel1.add("North", commandsPanel); panel1.add("Center", calckeysPanel); panel1.add("East",calmsPanel); // 建立一個畫板放文本框 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); // 為各按鈕添加事件偵聽器 // 都使用同一個事件偵聽器,,即本對象。本類的聲明中有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); } } //添加事件點(diǎn)擊 public void actionPerformed(ActionEvent e) { // 獲取事件源的標(biāo)簽 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) { // 用戶按了數(shù)字鍵或者小數(shù)點(diǎn)鍵 handleNumber(label); // handlezero(zero); } else { // 用戶按了運(yùn)算符鍵 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) { // 如果文本沒有了內(nèi)容,,則初始化計算器的各種值 resultText.setText("0"); firstDigit = true; operator = "="; } else { // 顯示新的文本 resultText.setText(text); } } } /** * 處理數(shù)字鍵被按下的事件 * * @param key */ private void handleNumber(String key) { if (firstDigit) { // 輸入的第一個數(shù)字 resultText.setText(key); } else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) { // 輸入的是小數(shù)點(diǎn),并且之前沒有小數(shù)點(diǎn),,則將小數(shù)點(diǎn)附在結(jié)果文本框的后面 resultText.setText(resultText.getText() + "."); } else if (!key.equals(".")) { // 如果輸入的不是小數(shù)點(diǎn),,則將數(shù)字附在結(jié)果文本框的后面 resultText.setText(resultText.getText() + key); } // 以后輸入的肯定不是第一個數(shù)字了 firstDigit = false; } /** * 處理C鍵被按下的事件 */ private void handleC() { // 初始化計算器的各種值 resultText.setText("0"); firstDigit = true; operator = "="; } /** * 處理運(yùn)算符鍵被按下的事件 * * @param key */ private void handleOperator(String key) { if (operator.equals("小時化分鐘")) { // 除法運(yùn)算 // 如果當(dāng)前結(jié)果文本框中的值等于0 if (getNumberFromText() == 0.0) { // 操作不合法 operateValidFlag = false; resultText.setText("除數(shù)不能為零"); } else { resultNum =60* getNumberFromText(); } } else if (operator.equals("秒化分鐘")) { // 倒數(shù)運(yùn)算 if (resultNum == 0.0) { // 操作不合法 operateValidFlag = false; resultText.setText("零沒有倒數(shù)"); } else { resultNum = resultNum/60; } } else if (operator.equals("+")) { // 加法運(yùn)算 resultNum = 60*getNumberFromText(); } else if (operator.equals("秒化小時")) { // 減法運(yùn)算 resultNum = getNumberFromText()/3600; } else if (operator.equals("分鐘化小時")) { // 乘法運(yùn)算 resultNum = getNumberFromText()/60; } else if (operator.equals("小時化秒")) { // 平方根運(yùn)算 resultNum =3600*getNumberFromText() ; } else if (operator.equals("分鐘化秒")) { // 百分號運(yùn)算,除以100 resultNum = resultNum *60; } else if (operator.equals("+/-")) { // 正數(shù)負(fù)數(shù)運(yùn)算 resultNum = resultNum * (-1); } else if (operator.equals("=")) { // 賦值運(yùn)算 resultNum = getNumberFromText(); } if (operateValidFlag) { // 雙精度浮點(diǎn)數(shù)的運(yùn)算 long t1; double t2; t1 = (long) resultNum; t2 = resultNum - t1; if (t2 == 0) { resultText.setText(String.valueOf(t1)); } else { resultText.setText(String.valueOf(resultNum)); } } // 運(yùn)算符等于用戶按的按鈕 operator = key; firstDigit = true; operateValidFlag = true; } /** * 從結(jié)果文本框中獲取數(shù)字 * * @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[]) { Calculator_Time calculator1 = new Calculator_Time(); calculator1.setVisible(true); calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ---------------------------------------------------------------------------------------------- //科學(xué)計算器 import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Stack; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JTextArea; import javax.swing.JRadioButton; import javax.swing.JButton; import java.awt.Color; import java.awt.Font; public class Calculator_Science extends JFrame implements ActionListener { private JPanel contentPane; private JTextArea textArea; StringBuffer sb = new StringBuffer(); // 存儲輸入的數(shù)字 String vl = null; // 存儲事件點(diǎn)擊的字符 public Calculator_Science() { setTitle("科學(xué)型"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 515, 442); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("功能"); menuBar.add(menu); JMenuItem menuItem = new JMenuItem("標(biāo)準(zhǔn)型"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Calculate_Ordinary ordinary = new Calculate_Ordinary(); ordinary.setVisible(true); } }); menu.add(menuItem); JMenuItem menuItem_1 = new JMenuItem("科學(xué)型"); menu.add(menuItem_1); JMenuItem menuItem_2 = new JMenuItem("時間換算"); menu.add(menuItem_2); menuItem_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Calculator_Time time = new Calculator_Time(); time.setVisible(true); } }); JMenu menu_1 = new JMenu("關(guān)于"); menuBar.add(menu_1); JMenuItem menuItem_4 = new JMenuItem("使用幫助"); menu_1.add(menuItem_4); JMenuItem menuItem_5 = new JMenuItem("關(guān)于作者"); menu_1.add(menuItem_4); menuItem_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Calculator_Author calculator_Author = new Calculator_Author(); calculator_Author.setVisible(true); } }); menu_1.add(menuItem_5); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// setContentPane(contentPane); contentPane.setLayout(null); textArea = new JTextArea(); textArea.setLineWrap(true); textArea.setFont(new Font("微軟雅黑", Font.PLAIN, 20)); textArea.setBackground(new Color(255, 250, 240)); textArea.setText("0"); textArea.setBounds(10, 0, 479, 81); contentPane.add(textArea); ButtonGroup bg = new ButtonGroup(); JRadioButton radioButton = new JRadioButton("度", true); radioButton.setBounds(6, 87, 67, 40); contentPane.add(radioButton); JRadioButton radioButton_1 = new JRadioButton("弧度"); radioButton_1.setBounds(91, 87, 67, 40); contentPane.add(radioButton_1); JRadioButton radioButton_2 = new JRadioButton("梯度"); radioButton_2.setBounds(189, 87, 60, 40); contentPane.add(radioButton_2); bg.add(radioButton); bg.add(radioButton_1); bg.add(radioButton_2); JButton btnNewButton = new JButton(""); btnNewButton.setBorder(null); btnNewButton.setBounds(10, 132, 40, 40); contentPane.add(btnNewButton); btnNewButton.addActionListener(this); JButton btnLnv = new JButton("lg"); btnLnv.setBorder(null); btnLnv.setBounds(60, 132, 40, 40); contentPane.add(btnLnv); btnLnv.addActionListener(this); JButton btnLn = new JButton("ln"); btnLn.setBorder(null); btnLn.setBounds(105, 132, 40, 40); contentPane.add(btnLn); btnLn.addActionListener(this); JButton button_1 = new JButton("("); button_1.setBorder(null); button_1.setBounds(155, 132, 40, 40); contentPane.add(button_1); button_1.addActionListener(this); JButton button_2 = new JButton(")"); button_2.setBorder(null); button_2.setBounds(209, 132, 40, 40); contentPane.add(button_2); button_2.addActionListener(this); JButton btnInt = new JButton("int"); btnInt.setBorder(null); btnInt.setBounds(10, 182, 40, 40); contentPane.add(btnInt); btnInt.addActionListener(this); JButton btnSinh = new JButton("sinh"); btnSinh.setBorder(null); btnSinh.setBounds(60, 182, 40, 40); contentPane.add(btnSinh); btnSinh.addActionListener(this); JButton btnSin = new JButton("sin"); btnSin.setBorder(null); btnSin.setBounds(105, 182, 40, 40); contentPane.add(btnSin); btnSin.addActionListener(this); JButton btnX = new JButton("x2"); btnX.setBorder(null); btnX.setBounds(155, 182, 40, 40); contentPane.add(btnX); btnX.addActionListener(this); JButton btnN = new JButton("n!"); btnN.setBorder(null); btnN.setBounds(209, 182, 40, 40); contentPane.add(btnN); btnN.addActionListener(this); JButton btnDms = new JButton("dms"); btnDms.setBorder(null); btnDms.setBounds(10, 232, 40, 40); contentPane.add(btnDms); btnDms.addActionListener(this); JButton btnCosh = new JButton("cosh"); btnCosh.setBorder(null); btnCosh.setBounds(60, 232, 40, 40); contentPane.add(btnCosh); btnCosh.addActionListener(this); JButton btnCos = new JButton("cos"); btnCos.setBorder(null); btnCos.setBounds(105, 232, 40, 40); contentPane.add(btnCos); btnCos.addActionListener(this); JButton btnXy = new JButton("x^y"); btnXy.setBorder(null); btnXy.setBounds(155, 232, 40, 40); contentPane.add(btnXy); btnXy.addActionListener(this); JButton btnYx = new JButton("y√x"); btnYx.setBorder(null); btnYx.setBounds(209, 232, 40, 40); contentPane.add(btnYx); btnYx.addActionListener(this); JButton button_13 = new JButton("π"); button_13.setBorder(null); button_13.setBounds(10, 282, 40, 40); contentPane.add(button_13); button_13.addActionListener(this); JButton btnTanh = new JButton("tanh"); btnTanh.setBorder(null); btnTanh.setBounds(60, 282, 40, 40); contentPane.add(btnTanh); btnTanh.addActionListener(this); JButton btnTan = new JButton("tan"); btnTan.setBorder(null); btnTan.setBounds(105, 282, 40, 40); contentPane.add(btnTan); btnTan.addActionListener(this); JButton btnX_1 = new JButton("x^3"); btnX_1.setBorder(null); btnX_1.setBounds(155, 282, 40, 40); contentPane.add(btnX_1); btnX_1.addActionListener(this); JButton btnx = new JButton("3√x"); btnx.setBorder(null); btnx.setBounds(209, 282, 40, 40); contentPane.add(btnx); btnx.addActionListener(this); JButton btnFe = new JButton("F-E"); btnFe.setBorder(null); btnFe.setBounds(10, 332, 40, 40); contentPane.add(btnFe); btnFe.addActionListener(this); JButton btnExp = new JButton("Exp"); btnExp.setBorder(null); btnExp.setBounds(60, 332, 40, 40); contentPane.add(btnExp); btnExp.addActionListener(this); JButton btnMod = new JButton("Mod"); btnMod.setBorder(null); btnMod.setBounds(105, 332, 40, 40); contentPane.add(btnMod); btnMod.addActionListener(this); JButton btnLog = new JButton("log"); btnLog.setBorder(null); btnLog.setBounds(155, 332, 40, 40); contentPane.add(btnLog); btnLog.addActionListener(this); JButton btnx_1 = new JButton("10^x"); btnx_1.setBorder(null); btnx_1.setBounds(209, 332, 40, 40); contentPane.add(btnx_1); btnx_1.addActionListener(this); JButton button_23 = new JButton("←"); button_23.setBorder(null); button_23.setBounds(259, 132, 40, 40); contentPane.add(button_23); button_23.addActionListener(this); JButton btnCe = new JButton("CE"); btnCe.setBorder(null); btnCe.setBounds(309, 132, 40, 40); contentPane.add(btnCe); btnCe.addActionListener(this); JButton btnC = new JButton("C"); btnC.setBorder(null); btnC.setBounds(354, 132, 40, 40); contentPane.add(btnC); btnC.addActionListener(this); JButton button_26 = new JButton("±"); button_26.setBorder(null); button_26.setBounds(404, 132, 40, 40); contentPane.add(button_26); button_26.addActionListener(this); JButton button_27 = new JButton("√"); button_27.setBorder(null); button_27.setBounds(458, 132, 40, 40); contentPane.add(button_27); button_27.addActionListener(this); JButton btnMc = new JButton("MC"); btnMc.setBorder(null); btnMc.setBounds(259, 87, 40, 40); contentPane.add(btnMc); btnMc.addActionListener(this); JButton btnMr = new JButton("MR"); btnMr.setBorder(null); btnMr.setBounds(309, 87, 40, 40); contentPane.add(btnMr); btnMr.addActionListener(this); JButton btnMs = new JButton("MS"); btnMs.setBorder(null); btnMs.setBounds(354, 87, 40, 40); contentPane.add(btnMs); btnMs.addActionListener(this); JButton btnM = new JButton("M+"); btnM.setBorder(null); btnM.setBounds(404, 87, 40, 40); contentPane.add(btnM); btnM.addActionListener(this); JButton btnM_1 = new JButton("M-"); btnM_1.setBorder(null); btnM_1.setBounds(458, 87, 40, 40); contentPane.add(btnM_1); btnM_1.addActionListener(this); JButton button_33 = new JButton("7"); button_33.setBorder(null); button_33.setBounds(259, 182, 40, 40); contentPane.add(button_33); button_33.addActionListener(this); JButton button_34 = new JButton("8"); button_34.setBorder(null); button_34.setBounds(309, 182, 40, 40); contentPane.add(button_34); button_34.addActionListener(this); JButton button_35 = new JButton("9"); button_35.setBorder(null); button_35.setBounds(354, 182, 40, 40); contentPane.add(button_35); button_35.addActionListener(this); JButton button_36 = new JButton("/"); button_36.setBorder(null); button_36.setBounds(404, 182, 40, 40); contentPane.add(button_36); button_36.addActionListener(this); JButton button_37 = new JButton("%"); button_37.setBorder(null); button_37.setBounds(458, 182, 40, 40); contentPane.add(button_37); button_37.addActionListener(this); JButton button_38 = new JButton("4"); button_38.setBorder(null); button_38.setBounds(259, 232, 40, 40); contentPane.add(button_38); button_38.addActionListener(this); JButton button_39 = new JButton("5"); button_39.setBorder(null); button_39.setBounds(309, 232, 40, 40); contentPane.add(button_39); button_39.addActionListener(this); JButton button_40 = new JButton("6"); button_40.setBorder(null); button_40.setBounds(354, 232, 40, 40); contentPane.add(button_40); button_40.addActionListener(this); JButton button_41 = new JButton("*"); button_41.setBorder(null); button_41.setBounds(404, 232, 40, 40); contentPane.add(button_41); button_41.addActionListener(this); JButton button_42 = new JButton("1/x"); button_42.setBorder(null); button_42.setBounds(458, 232, 40, 40); contentPane.add(button_42); button_42.addActionListener(this); JButton button_43 = new JButton("1"); button_43.setBorder(null); button_43.setBounds(259, 282, 40, 40); contentPane.add(button_43); button_43.addActionListener(this); JButton button_44 = new JButton("2"); button_44.setBorder(null); button_44.setBounds(309, 282, 40, 40); contentPane.add(button_44); button_44.addActionListener(this); JButton button_45 = new JButton("3"); button_45.setBorder(null); button_45.setBounds(354, 282, 40, 40); contentPane.add(button_45); button_45.addActionListener(this); JButton button_46 = new JButton("-"); button_46.setBorder(null); button_46.setBounds(404, 282, 40, 40); contentPane.add(button_46); button_46.addActionListener(this); JButton button_50 = new JButton("."); button_50.setBorder(null); button_50.setBounds(354, 332, 40, 40); contentPane.add(button_50); button_50.addActionListener(this); JButton button_51 = new JButton("+"); button_51.setBorder(null); button_51.setBounds(404, 332, 40, 40); contentPane.add(button_51); button_51.addActionListener(this); JButton btnNewButton_1 = new JButton("="); btnNewButton_1.setBorder(null); btnNewButton_1.setBounds(458, 282, 40, 90); contentPane.add(btnNewButton_1); btnNewButton_1.addActionListener(this); JButton btnNewButton_2 = new JButton("0"); btnNewButton_2.setBounds(259, 332, 90, 40); contentPane.add(btnNewButton_2); btnNewButton_2.addActionListener(this); } //添加事件點(diǎn)擊 public void actionPerformed(ActionEvent e) { vl = e.getActionCommand(); try { if(vl.equals(".") || Integer.parseInt(vl) % 2 == 1 || Integer.parseInt(vl) % 2 == 0) { sb.append(vl); } } catch (Exception c) { if(vl.equals("+") || vl.equals("-") || vl.equals("*") || vl.equals("/") ||vl.equals("log") || vl.equals("√") || vl.equals("%") ||vl.equals("n!") || vl.equals("int") || vl.equals("sinh") ||vl.equals("lg") || vl.equals("sin") || vl.equals("x2") ||vl.equals("ln") || vl.equals("cosh") || vl.equals("cos") || vl.equals("(") || vl.equals("x^y") || vl.equals("y√x") || vl.equals(")") || vl.equals("tanh") || vl.equals("tan") || vl.equals("π") || vl.equals("x^3") || vl.equals("3√x") || vl.equals("Mod") || vl.equals("10^x") || vl.equals("1/x")) { sb.append(" "+vl+" "); }else if(vl.equals("=")) { sb.append(" "+"="+" "+ Algorithm(sb)); }else if(vl.equals("C")) { sb.delete(0, sb.length()); }else if(vl.equals("C")) { sb.delete(sb.length()-3, sb.length()); }else if(vl.equals("←")) { sb.delete(sb.length()-1, sb.length()); } } textArea.setText(sb.toString()); } //運(yùn)算的方法 private String Algorithm(StringBuffer str) { String a = str.toString(); String n[]; n = a.split(" "); double ans = 0d; for(int i = 1; i < n.length; i++) { if(n[i].equals("+")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i - 1]); ans = after+Front; } if(n[i].equals("-")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i - 1]); ans = Front-after; } if(n[i].equals("*")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i - 1]); ans = after*Front; } if(n[i].equals("/")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i - 1]); ans = Front/after; } if(n[i].equals("√")) { double after = Double.parseDouble(n[i + 1]); ans = Math.sqrt(after); } if(n[i].equals("%")) { double Front = Double.parseDouble(n[i - 1]); ans = Front/100; } if(n[i].equals("int")) { double after = Double.parseDouble(n[i + 1]); ans = Math.ceil(after); } if(n[i].equals("sinh")) { double after = Double.parseDouble(n[i + 1]); ans = Math.sinh(after); } if(n[i].equals("sinh")) { double after = Double.parseDouble(n[i + 1]); ans = Math.sin(after); } if(n[i].equals("lg")) { double after = Double.parseDouble(n[i + 1]); ans = Math.log10(after); } if(n[i].equals("x2")) { double Front = Double.parseDouble(n[i - 1]); ans = Front*Front; } if(n[i].equals("cosh")) { double after = Double.parseDouble(n[i + 1]); ans = Math.cosh(after); } if(n[i].equals("cos")) { double after = Double.parseDouble(n[i + 1]); ans = Math.cos(after); } if(n[i].equals("x^y")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i - 1]); ans = Math.pow(Front, after); } if(n[i].equals("y√x")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i - 1]); ans = Math.pow(Front, 1d/after); } if(n[i].equals("tanh")) { double after = Double.parseDouble(n[i + 1]); ans = Math.tanh(after); } if(n[i].equals("tan")) { double after = Double.parseDouble(n[i + 1]); ans = Math.tan(after); } if(n[i].equals("x^3")) { double Front = Double.parseDouble(n[i - 1]); ans = Front*Front*Front; } if(n[i].equals("3√x")) { double after = Double.parseDouble(n[i + 1]); ans = Math.pow(after, 1d/3); } if(n[i].equals("Mod")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i - 1]); ans = (Front%after); } if(n[i].equals("ln")) { double after = Double.parseDouble(n[i + 1]); ans =Math.log(after); } if(n[i].equals("10^x")) { double after = Double.parseDouble(n[i + 1]); ans = Math.pow(10, after); } if(n[i].equals("1/x")) { double after = Double.parseDouble(n[i + 1]); ans = 1/after; } if(n[i].equals("π")) { double after = Double.parseDouble(n[i - 1]); ans = 3.14*after; }if(n[i].equals("log")) { double after = Double.parseDouble(n[i + 1]); double Front = Double.parseDouble(n[i -1 ]); ans = Math.log(after)/Math.log(Front); } if(n[i].equals("n!")) { double after = Double.parseDouble(n[i - 1]); int e ; int c=1; for(e=1;e<=after;e++) {c=c*e; ans=c;} } } String result = String.valueOf(ans); return result; } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calculator_Science frame = new Calculator_Science(); frame.setVisible(true); frame.setResizable(false); } catch (Exception e) { e.printStackTrace(); } } }); } } ------------------------------------------------------------------------------ //標(biāo)準(zhǔn)計算器 import java.awt.BorderLayout; import java.awt.Button; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextArea; import javax.swing.JTextField; import java.awt.Color; import javax.swing.JButton; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.util.Stack; import java.awt.event.ActionEvent; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import java.awt.Font; import java.awt.GridLayout; public class Calculate_Ordinary extends JFrame implements ActionListener { private final String[] KEYS = { "7", "8", "9", "/", "√", "4", "5", "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" }; /** 計算器上的功能鍵的顯示名字 final p86*/ 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]; /** 計算結(jié)果文本框 */ private JTextField resultText = new JTextField("0"); // 標(biāo)志用戶按的是否是整個表達(dá)式的第一個數(shù)字,或者是運(yùn)算符后的第一個數(shù)字 private boolean firstDigit = true; // 計算的中間結(jié)果,。 private double resultNum = 0.0; // 當(dāng)前運(yùn)算的運(yùn)算符 private String operator = "="; // 操作是否合法 private boolean operateValidFlag = true; StringBuffer sb = new StringBuffer(); // 存儲輸入的數(shù)字 String vl = null; // 存儲事件點(diǎn)擊的字符 //標(biāo)準(zhǔn)型計算器 public Calculate_Ordinary() { setTitle("標(biāo)準(zhǔn)型"); // 初始化計算器 super p114 // 設(shè)置計算器的背景顏色 this.setBackground(Color.LIGHT_GRAY); this.setTitle("標(biāo)準(zhǔn)計算器"); // 在屏幕(500, 300)坐標(biāo)處顯示計算器 this.setLocation(500, 300); // 不許修改計算器的大小 this.setSize(500, 500); this.setResizable(false); // 使計算器中各組件大小合適 //使這個外部容器自動調(diào)整成剛好裝下這幾個按鈕的大小的尺寸,。 JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("功能"); menuBar.add(menu); JMenuItem menuItem = new JMenuItem("標(biāo)準(zhǔn)型"); menu.add(menuItem); JMenuItem menuItem_1 = new JMenuItem("科學(xué)型"); menu.add(menuItem_1); menuItem_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Calculator_Science science = new Calculator_Science(); science.setVisible(true); } }); JMenuItem menuItem_2 = new JMenuItem("時間換算"); menu.add(menuItem_2); menuItem_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Calculator_Time time = new Calculator_Time(); time.setVisible(true); } }); JMenu menu_1 = new JMenu("關(guān)于"); menuBar.add(menu_1); JMenuItem menuItem_4 = new JMenuItem("使用幫助"); menu_1.add(menuItem_4); JMenuItem menuItem_5 = new JMenuItem("關(guān)于作者"); menu_1.add(menuItem_5); menuItem_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Calculator_Author calculator_Author = new Calculator_Author(); calculator_Author.setVisible(true); } }); resultText.setHorizontalAlignment(JTextField.RIGHT); // 設(shè)置文本框背景顏色為白色 resultText.setBackground(Color.white); resultText.setFont(new Font("TimesRoman",Font.BOLD,46)); // 初始化計算器上鍵的按鈕,將鍵放在一個畫板內(nèi) JPanel calckeysPanel = new JPanel(); // 用網(wǎng)格布局器,,4行,,5列的網(wǎng)格,網(wǎng)格之間的水平方向間隔為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.white); keys[i].setBackground(Color.DARK_GRAY); keys[i].setFont(new Font("Courir",Font.ITALIC,20)); } // 運(yùn)算符鍵用紅色標(biāo)示,,其他鍵用黑色表示 keys[3].setForeground(Color.white); keys[8].setForeground(Color.white); keys[13].setForeground(Color.white); keys[18].setForeground(Color.white); keys[19].setForeground(Color.white); keys[3].setBackground(Color.getHSBColor(250, 155, 140)); keys[8].setBackground(Color.getHSBColor(250, 155, 140)); keys[13].setBackground(Color.getHSBColor(250, 155, 140)); keys[18].setBackground(Color.getHSBColor(250, 155, 140)); keys[19].setBackground(Color.getHSBColor(250, 155, 140)); // 初始化功能鍵,都用紅色標(biāo)示,。將功能鍵放在一個畫板內(nèi) JPanel commandsPanel = new JPanel(); // 用網(wǎng)格布局器,,1行,3列的網(wǎng)格,網(wǎng)格之間的水平方向間隔為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.DARK_GRAY); commands[i].setFont(new Font("Courir",Font.PLAIN,20)); } // 初始化M鍵,,用紅色標(biāo)示,將M鍵放在一個畫板內(nèi) JPanel calmsPanel = new JPanel(); // 用網(wǎng)格布局管理器,,5行,,1列的網(wǎng)格,網(wǎng)格之間的水平方向間隔為3個象素,,垂直方向間隔為3個象素 calmsPanel.setLayout(new GridLayout(4, 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); m[i].setFont(new Font("Courir",Font.PLAIN,20)); } // 下面進(jìn)行計算器的整體布局,,將calckeys和command畫板放在計算器的中部, // 將文本框放在北部,,將calms畫板放在計算器的西部,。 // 新建一個大的畫板,將上面建立的command和calckeys畫板放在該畫板內(nèi) JPanel panel1 = new JPanel(); // 畫板采用邊界布局管理器,,畫板里組件之間的水平和垂直方向上間隔都為3象素 panel1.setLayout(new BorderLayout(3, 3)); panel1.add("North", commandsPanel); panel1.add("Center", 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); } } //添加事件點(diǎn)擊 public void actionPerformed(ActionEvent e) { // 獲取事件源的標(biāo)簽 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) { // 用戶按了數(shù)字鍵或者小數(shù)點(diǎn)鍵 handleNumber(label); // handlezero(zero); } else { // 用戶按了運(yùn)算符鍵 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) { // 如果文本沒有了內(nèi)容,,則初始化計算器的各種值 resultText.setText("0"); firstDigit = true; operator = "="; } else { // 顯示新的文本 resultText.setText(text); } } } /** * 處理數(shù)字鍵被按下的事件 * * @param key */ private void handleNumber(String key) { if (firstDigit) { // 輸入的第一個數(shù)字 resultText.setText(key); } else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) { // 輸入的是小數(shù)點(diǎn),并且之前沒有小數(shù)點(diǎn),,則將小數(shù)點(diǎn)附在結(jié)果文本框的后面 resultText.setText(resultText.getText() + "."); } else if (!key.equals(".")) { // 如果輸入的不是小數(shù)點(diǎn),,則將數(shù)字附在結(jié)果文本框的后面 resultText.setText(resultText.getText() + key); } // 以后輸入的肯定不是第一個數(shù)字了 firstDigit = false; } /** * 處理C鍵被按下的事件 */ private void handleC() { // 初始化計算器的各種值 resultText.setText("0"); firstDigit = true; operator = "="; } /** * 處理運(yùn)算符鍵被按下的事件 * * @param key */ private void handleOperator(String key) { if (operator.equals("/")) { // 除法運(yùn)算 // 如果當(dāng)前結(jié)果文本框中的值等于0 if (getNumberFromText() == 0.0) { // 操作不合法 operateValidFlag = false; resultText.setText("除數(shù)不能為零"); } else { resultNum /= getNumberFromText(); } } else if (operator.equals("1/x")) { // 倒數(shù)運(yùn)算 if (resultNum == 0.0) { // 操作不合法 operateValidFlag = false; resultText.setText("零沒有倒數(shù)"); } else { resultNum = 1 / resultNum; } } else if (operator.equals("+")) { // 加法運(yùn)算 resultNum += getNumberFromText(); } else if (operator.equals("-")) { // 減法運(yùn)算 resultNum -= getNumberFromText(); } else if (operator.equals("*")) { // 乘法運(yùn)算 resultNum *= getNumberFromText(); } else if (operator.equals("√")) { // 平方根運(yùn)算 resultNum = Math.sqrt(resultNum); } else if (operator.equals("%")) { // 百分號運(yùn)算,除以100 resultNum = resultNum / 100; } else if (operator.equals("+/-")) { // 正數(shù)負(fù)數(shù)運(yùn)算 resultNum = resultNum * (-1); } else if (operator.equals("=")) { // 賦值運(yùn)算 resultNum = getNumberFromText(); } if (operateValidFlag) { // 雙精度浮點(diǎn)數(shù)的運(yùn)算 long t1; double t2; t1 = (long) resultNum; t2 = resultNum - t1; if (t2 == 0) { resultText.setText(String.valueOf(t1)); } else { resultText.setText(String.valueOf(resultNum)); } } // 運(yùn)算符等于用戶按的按鈕 operator = key; firstDigit = true; operateValidFlag = true; } /** * 從結(jié)果文本框中獲取數(shù)字 * * @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[]) { Calculate_Ordinary calculator1 = new Calculate_Ordinary(); calculator1.setVisible(true); calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } -------------------------------------------------------------------------------- //關(guān)于 import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import java.awt.Font; public class Calculator_Author extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calculator_Author frame = new Calculator_Author(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Calculator_Author() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 489, 335); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("小組成員:"); lblNewLabel.setFont(new Font("華文彩云", Font.BOLD, 15)); lblNewLabel.setBounds(20, 223, 530, 52); lblNewLabel.setForeground(Color.black); contentPane.add(lblNewLabel); JLabel label = new JLabel("軟件二班:第六組"); label.setFont(new Font("華文彩云", Font.BOLD, 30 )); label.setForeground(Color.BLUE); label.setBounds(120, 70, 300, 75); contentPane.add(label); JLabel lblqq = new JLabel("指導(dǎo)教師:"); lblqq.setFont(new Font("華文彩云", Font.BOLD, 20)); lblqq.setBounds(150, 150, 307, 73); contentPane.add(lblqq); lblqq.setForeground(Color.BLUE); } } |
|
來自: 昵稱57973720 > 《待分類》