diff --git a/src/java2020spring/BiaoZhun.java b/src/java2020spring/BiaoZhun.java new file mode 100644 index 0000000000000000000000000000000000000000..711403f73aee07166840881ed6fe135429a9f21e --- /dev/null +++ b/src/java2020spring/BiaoZhun.java @@ -0,0 +1,217 @@ +package JiSuanQi; + import java.awt.Color; + import java.awt.*; + import java.awt.event.*; + import javax.swing.*; + 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; + @SuppressWarnings("serial") + public class BiaoZhun extends JFrame implements ActionListener { + private final String[] KEYS = {"*", "/", "MC","MR","MS","M+","7", "8", "9","+", "4", "5", "6", "-", "1", "2", "3", "%","0",".","=", "1/x" }; + private final String[] COMMAND = { "Back", "C" }; + private JButton keys[] = new JButton[KEYS.length]; + private JButton commands[] = new JButton[COMMAND.length]; + private JTextField resultText = new JTextField("0"); + private boolean firstDigit = true; + private double resultNum = 0.0; + private String operator = "="; + private boolean operateValidFlag = true; + StringBuffer sb = new StringBuffer(); + String vl = null; + public BiaoZhun() { + setTitle("标准型"); + this.setBackground(Color.LIGHT_GRAY); + this.setTitle("计算器"); + this.setLocation(500, 300); + this.setSize(500, 500); + this.setResizable(false); + JMenuBar menuBar = new JMenuBar(); + setJMenuBar(menuBar); + JMenu menu = new JMenu("功能"); + menuBar.add(menu); + JMenuItem menuItem = new JMenuItem("标准型"); + menu.add(menuItem); + JMenuItem menuItem_1 = new JMenuItem("科学型"); + menu.add(menuItem_1); + menuItem_1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + KeXue science = new KeXue(); + science.setVisible(true); + } + } + ); + resultText.setHorizontalAlignment(JTextField.RIGHT); + resultText.setBackground(Color.white); + resultText.setFont(new Font("TimesRoman",Font.BOLD,46)); + JPanel PPanel = new JPanel(); + PPanel.setLayout(new GridLayout(6, 4, 3, 3)); + for (int i = 0; i < COMMAND.length; i++) { + commands[i] = new JButton(COMMAND[i]); + PPanel.add(commands[i]); + commands[i].setFont(new Font("Courir",Font.PLAIN,20)); + } + commands[0].setForeground(Color.red); + commands[1].setForeground(Color.red); + for (int i = 0; i < KEYS.length; i++) { + keys[i] = new JButton(KEYS[i]); + PPanel.add(keys[i]); + keys[i].setFont(new Font("Courir",Font.ITALIC,20)); + } + keys[0].setForeground(Color.red); + keys[1].setForeground(Color.red); + keys[2].setForeground(Color.red); + keys[3].setForeground(Color.red); + keys[4].setForeground(Color.red); + keys[5].setForeground(Color.red); + keys[9].setForeground(Color.red); + keys[13].setForeground(Color.red); + keys[17].setForeground(Color.red); + keys[21].setForeground(Color.red); + keys[6].setForeground(Color.blue); + keys[7].setForeground(Color.blue); + keys[8].setForeground(Color.blue); + keys[10].setForeground(Color.blue); + keys[11].setForeground(Color.blue); + keys[12].setForeground(Color.blue); + keys[14].setForeground(Color.blue); + keys[15].setForeground(Color.blue); + keys[16].setForeground(Color.blue); + keys[18].setForeground(Color.blue); + keys[19].setForeground(Color.blue); + keys[20].setForeground(Color.blue); + JPanel panel1 = new JPanel(); + panel1.setLayout(new BorderLayout(3,3)); + panel1.add("Center", PPanel); + 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); + for (int i = 0; i < KEYS.length; i++) { + keys[i].addActionListener(this); + } + for (int i = 0; i < COMMAND.length; i++) { + commands[i].addActionListener(this); + } + } + JPanel calmsPanel = new JPanel(); + public void actionPerformed(ActionEvent e) { + String label = e.getActionCommand(); + if (label.equals(COMMAND[0])) { + handleBack(); + } + else if (label.equals(COMMAND[1])) { + handleC(); + } + else if ("0123456789.".indexOf(label) >= 0) { + handleNumber(label); + } + else + handleOperator(label); + } + private void handleBack() { + 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); + } + } + + 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; + } + + private void handleC() { + resultText.setText("0"); + firstDigit = true; + operator = "="; + } + + private void handleOperator(String key) { + if (operator.equals("/")) { + 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("%")) { + resultNum %= getNumberFromText(); + } + 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; + } + + private double getNumberFromText() { + double result = 0; + try { + result = Double.valueOf(resultText.getText()).doubleValue(); + } catch (NumberFormatException e) {} + return result; + } + + public static void main(String args[]) { + BiaoZhun application = new BiaoZhun(); + application.setVisible(true); + application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + } diff --git a/src/java2020spring/KeXue.java b/src/java2020spring/KeXue.java new file mode 100644 index 0000000000000000000000000000000000000000..4d648afcd3aca74497a12696330522a98f9ef577 --- /dev/null +++ b/src/java2020spring/KeXue.java @@ -0,0 +1,303 @@ +package JiSuanQi; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +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; +@SuppressWarnings("serial") +public class KeXue extends JFrame implements ActionListener { + private final String[] KEYS = { "*", "/","MC","MR","MS","M+", "7", "8", "9","+", "4", "5", "6", "-", "1", "2", "3", "%","0",".","=", "1/x" }; + private final String[] COMMAND = { "Back", "C" }; + private JButton keys[] = new JButton[KEYS.length]; + private JButton commands[] = new JButton[COMMAND.length]; + private JTextField resultText = new JTextField("0"); + + private boolean firstDigit = true; + private double resultNum = 0.0; + private String operator = "="; + private boolean operateValidFlag = true; + private final String[] KEYS2= {"X^2","X^3","X^Y","Sqrt","rt","X!","ln","lg","%","sin","cos","tan","PI","e","e^X"}; + private JButton keys2[]=new JButton[KEYS2.length]; + StringBuffer sb = new StringBuffer(); + String vl = null; + private JPanel contentPane,contentpane2; + + private FlowLayout layout; + + public KeXue() + { + super("科学型"); + this.setBackground(Color.LIGHT_GRAY); + this.setTitle("计算器"); + this.setSize(800, 500); + JMenuBar menuBar = new JMenuBar(); + setJMenuBar(menuBar); + JMenu menu = new JMenu("功能"); + menuBar.add(menu); + JMenuItem menuItem = new JMenuItem("标准型"); + menu.add(menuItem); + JMenuItem menuItem_1 = new JMenuItem("科学型"); + menu.add(menuItem_1); + menuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + BiaoZhun ordinary = new BiaoZhun(); + ordinary.setVisible(true); + } + } + ); + + resultText.setHorizontalAlignment(JTextField.RIGHT); + resultText.setBackground(Color.white); + resultText.setFont(new Font("TimesRoman",Font.BOLD,46)); + + JPanel PPanel=new JPanel(); + PPanel.setLayout(new GridLayout(6, 4, 3, 3)); + for (int i = 0; i < COMMAND.length; i++) { + commands[i] = new JButton(COMMAND[i]); + PPanel.add(commands[i]); + commands[i].setFont(new Font("Courir",Font.PLAIN,20)); + } + commands[0].setForeground(Color.red); + commands[1].setForeground(Color.red); + for (int i = 0; i < KEYS.length; i++) { + keys[i] = new JButton(KEYS[i]); + PPanel.add(keys[i]); + keys[i].setFont(new Font("Courir",Font.ITALIC,20)); + } + keys[0].setForeground(Color.red); + keys[1].setForeground(Color.red); + keys[2].setForeground(Color.red); + keys[3].setForeground(Color.red); + keys[4].setForeground(Color.red); + keys[5].setForeground(Color.red); + keys[9].setForeground(Color.red); + keys[13].setForeground(Color.red); + keys[17].setForeground(Color.red); + keys[21].setForeground(Color.red); + keys[6].setForeground(Color.blue); + keys[7].setForeground(Color.blue); + keys[8].setForeground(Color.blue); + keys[10].setForeground(Color.blue); + keys[11].setForeground(Color.blue); + keys[12].setForeground(Color.blue); + keys[14].setForeground(Color.blue); + keys[15].setForeground(Color.blue); + keys[16].setForeground(Color.blue); + keys[18].setForeground(Color.blue); + keys[19].setForeground(Color.blue); + keys[20].setForeground(Color.blue); + JPanel panel1 = new JPanel(); + panel1.setLayout(new BorderLayout(3,3)); + panel1.add("Center", PPanel); + JPanel top = new JPanel(); + top.setLayout(new BorderLayout()); + top.add("Center", resultText); + contentPane=new JPanel(); + contentPane.setLayout(new BorderLayout(3, 5)); + contentPane.add("North", top); + contentPane.add("Center", panel1); + JPanel ppanel=new JPanel(); + ppanel.setLayout(new GridLayout(5, 3, 3, 3)); + for (int i = 0; i < KEYS2.length; i++) { + keys2[i] = new JButton(KEYS2[i]); + ppanel.add(keys2[i]); + keys2[i].setFont(new Font("Courir",Font.ITALIC,20)); + } + contentpane2 = new JPanel(); + contentpane2.setLayout(new BorderLayout(3, 5)); + contentpane2.add("Center", ppanel); + + layout=new FlowLayout(); + JPanel ane= new JPanel(); + ane.setLayout(layout); + getContentPane().setLayout(new BorderLayout(3,5)); + getContentPane().add("Center", contentPane); + getContentPane().add("East", contentpane2); + 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 < KEYS2.length; i++) + keys2[i].addActionListener(this); + setVisible(true); + } + public void actionPerformed(ActionEvent e) + { + String label = e.getActionCommand(); + if (label.equals(COMMAND[0])) { + handleBack(); + } + else if (label.equals(COMMAND[1])) { + handleC(); + } + else if ("0123456789.".indexOf(label) >= 0) { + handleNumber(label); + } + else if(label.equals("PI")) + resultText.setText(String.valueOf(3.14159265)); + else if(label.equals("e")) + resultText.setText(String.valueOf(2.17828)); + else + handleOperator(label); + } + private void handleBack() { + 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); + } + } + + 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; + } + + private void handleC() { + resultText.setText("0"); + firstDigit = true; + operator = "="; + } + + private void handleOperator(String key) { + if (operator.equals("/")) { + 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("%")) { + resultNum = getNumberFromText(); + } + else if (operator.equals("X^2")) { + resultNum = resultNum*resultNum; + } + else if (operator.equals("X^3")) { + resultNum = resultNum*resultNum*resultNum; + } + else if (operator.equals("X^Y")) { + resultNum = Math.pow(resultNum,getNumberFromText()); + } + else if (operator.equals("Sqrt")) { + resultNum = Math.sqrt(resultNum); + } + else if (operator.equals("rt")) { + resultNum = resultNum*57.3; + } + else if (operator.equals("X!")) { + int ee; + double re=resultNum ; + int c=1; + for(ee=1;ee<=re;ee++) + { + c=c*ee; + resultNum=c;} + } + else if (operator.equals("ln")) { + resultNum = Math.log(resultNum); + } + else if (operator.equals("lg")) { + resultNum = Math.log10(resultNum); + } + else if (operator.equals("%")) { + resultNum %= getNumberFromText(); + }else if (operator.equals("sin")) { + resultNum = Math.sin(resultNum); + } + else if (operator.equals("cos")) { + resultNum = Math.cos(resultNum); + } + else if (operator.equals("tan")) { + resultNum = Math.tan(resultNum); + } + else if (operator.equals("e^X")) { + resultNum = Math.exp(resultNum); + } + 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; + } + + private double getNumberFromText() { + double result = 0; + try { + result = Double.valueOf(resultText.getText()).doubleValue(); + } catch (NumberFormatException e) {} + return result; + } + + public static void main(String args[]) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + KeXue application = new KeXue(); + application.setVisible(true); + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + ); + } + +}