diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..28d609d721243a4d72da8efeea954e1699e4a1fc --- /dev/null +++ b/src/java2020spring/Calculator.java @@ -0,0 +1,396 @@ +package java2020spring; +import java.awt.Color; +import java.awt.EventQueue; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.Font; +import java.awt.GridLayout; + +@SuppressWarnings("serial") +public class Calculator extends JFrame implements ActionListener { + private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25; + double x,y; /*运算数x,y*/ + int z; + double sum; /*阶乘结果数*/ + double s; /*乘Π运算结果数*/ + int flag=0; /*标志变量*/ + StringBuffer str=new StringBuffer(); /*建立字符缓冲区*/ + private JTextField resultText=new JTextField("0") ; /*显示结果文本框*/ + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + Calculator frame = new Calculator(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + /** + * Create the frame. + */ + public Calculator() { + getContentPane().setBackground(Color.WHITE); /*初始化计算器页面,结果文本框颜色字体以及按钮初始化*/ + getContentPane().setForeground(new Color(255, 250, 205)); + getContentPane().setLayout(new GridLayout(2, 0, 25, 20)); + setBounds(400, 200, 250, 200); + setBackground(Color.WHITE); + setSize(466,486); + setTitle("球球的计算器"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + resultText.setEditable(false); + resultText.setBackground(Color.WHITE); + resultText.setFont(new Font("华文细黑", Font.BOLD, 42)); + resultText.setHorizontalAlignment(SwingConstants.RIGHT); + resultText.setSize(46,86); + resultText.setBorder(new EmptyBorder(5, 0, 2, 2)); + getContentPane().add(resultText); + + JPanel p1 = new JPanel(); + p1.setBackground(Color.WHITE); + p1.setSize(400,400); + p1.setLayout(new GridLayout(5, 5, 2, 3)); + p1.setBorder(new EmptyBorder(0, 0, 0, 0)); + getContentPane().add(p1); + + b1 = new JButton("\uFF0B"); + b1.setForeground(new Color(0, 0, 0)); + b1.setBackground(Color.WHITE); + b1.setFont(new Font("华文楷体", Font.BOLD, 27)); + + b2 = new JButton("\uFE63"); + b2.setForeground(new Color(0, 0, 0)); + b2.setBackground(Color.WHITE); + b2.setFont(new Font("楷体", Font.BOLD, 42)); + + b3 = new JButton("C"); + b3.setBackground(Color.WHITE); + b3.setFont(new Font("华文细黑", Font.BOLD, 27)); + b3.setForeground(new Color(0, 0, 0)); + + b4 = new JButton("\u2190"); + b4.setFont(new Font("幼圆", Font.BOLD, 33)); + b4.setBackground(Color.WHITE); + + b5 = new JButton("7"); + b5.setFont(new Font("幼圆", Font.BOLD, 33)); + + b6 = new JButton("8"); + b6.setFont(new Font("幼圆", Font.BOLD, 33)); + + b7 = new JButton("9"); + b7.setFont(new Font("幼圆", Font.BOLD, 33)); + + b8 = new JButton("×"); + b8.setBackground(Color.WHITE); + b8.setFont(new Font("华文楷体", Font.BOLD, 29)); + + b9 = new JButton("4"); + b9.setFont(new Font("幼圆", Font.BOLD, 33)); + + b10 = new JButton("5"); + b10.setFont(new Font("幼圆", Font.BOLD, 33)); + + b11 = new JButton("6"); + b11.setFont(new Font("幼圆", Font.BOLD, 33)); + + b12 = new JButton("\u00F7"); + b12.setBackground(Color.WHITE); + b12.setFont(new Font("幼圆", Font.BOLD, 33)); + + b13 = new JButton("1"); + b13.setFont(new Font("幼圆", Font.BOLD, 33)); + + b14 = new JButton("2"); + b14.setFont(new Font("幼圆", Font.BOLD, 33)); + + b15 = new JButton("3"); + b15.setFont(new Font("幼圆", Font.BOLD, 33)); + + b16 = new JButton("\u221A"); + b16.setBackground(Color.WHITE); + b16.setFont(new Font("幼圆", Font.BOLD, 29)); + + b17 = new JButton("-/+"); + b17.setFont(new Font("华文宋体", Font.BOLD, 30)); + + b18 = new JButton("0"); + b18.setFont(new Font("幼圆", Font.BOLD, 33)); + + b19 = new JButton("."); + b19.setFont(new Font("幼圆", Font.BOLD, 35)); + + b20 = new JButton("="); + b20.setBackground(Color.WHITE); + b20.setForeground(new Color(0, 0, 0)); + b20.setFont(new Font("华文楷体", Font.BOLD, 36)); + + b21 = new JButton("%"); + b21.setBackground(Color.WHITE); + b21.setFont(new Font("华文楷体", Font.BOLD, 25)); + + b22 = new JButton("!"); + b22.setBackground(Color.WHITE); + b22.setFont(new Font("华文楷体", Font.BOLD, 31)); + + b23 = new JButton("x\u00B2"); + b23.setBackground(Color.WHITE); + b23.setFont(new Font("华文楷体", Font.BOLD, 28)); + + b24 = new JButton("1/x"); + b24.setBackground(Color.WHITE); + b24.setFont(new Font("华文楷体", Font.BOLD, 26)); + + b25 = new JButton("\u03C0"); + b25.setBackground(Color.WHITE); + b25.setFont(new Font("华文楷体", Font.BOLD, 30)); + + p1.add(b21);p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p1.add(b22);p1.add(b5); + p1.add(b6);p1.add(b7);p1.add(b8);p1.add(b23);p1.add(b9);p1.add(b10);p1.add(b11);p1.add(b12);p1.add(b24); + p1.add(b13);p1.add(b14);p1.add(b15);p1.add(b16);p1.add(b25);p1.add(b17);p1.add(b18);p1.add(b19);p1.add(b20); + + /*注册监听器*/ + b1.addActionListener(this);b2.addActionListener(this); + b3.addActionListener(this); b4.addActionListener(this); + b5.addActionListener(this); b6.addActionListener(this); + b7.addActionListener(this); b8.addActionListener(this); + b9.addActionListener(this); b10.addActionListener(this); + b11.addActionListener(this); b12.addActionListener(this); + b13.addActionListener(this); b14.addActionListener(this); + b15.addActionListener(this); b16.addActionListener(this); + b17.addActionListener(this); b18.addActionListener(this); + b19.addActionListener(this); b20.addActionListener(this); + b21.addActionListener(this); b22.addActionListener(this); + b23.addActionListener(this); b24.addActionListener(this); b25.addActionListener(this); + } + /*实现按钮*/ + public void actionPerformed(ActionEvent e) { + try { + /*清除键C操作*/ + if(e.getSource()==b3) { + y=0d; + x=0d; + s=0d; + sum=0d; + flag=0; + z=13; + resultText.setText("0"); + str.setLength(0); + } + /*加负号操作*/ + else if(e.getSource()==b17) { + x=Double.parseDouble(resultText.getText().trim()); + if(x==0.0) { /*若显示屏为零则不用给0加负号*/ + resultText.setText("0"); + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + else { + resultText.setText(""+(-x)); + z=12; + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + } + /*连加与加法操作*/ + else if(e.getSource()==b1) { + if(flag==0) { + x=Double.parseDouble(resultText.getText().trim());flag=1; + } + else { + x=x+y; + y=0d; + } + z=0; + str.setLength(0); + } + /*连减和减法操作*/ + else if(e.getSource()==b2) { + if(flag==0) { + x=Double.parseDouble(resultText.getText().trim());flag=1; + } + else { + x=x-y; + y=0d; + } + z=1; + str.setLength(0); + } + /*连乘和乘法操作*/ + else if(e.getSource()==b8) { + if(flag==0) { + x=Double.parseDouble(resultText.getText().trim());flag=1; + } + else { + x=x*y; + y=0d; + } + z=2; + str.setLength(0); + } + /*连除和除法操作*/ + else if(e.getSource()==b12) { + if(flag==0) { + x=Double.parseDouble(resultText.getText().trim());flag=1; + } + else{ + x=x/y; + y=0d; + } + z=3; + str.setLength(0); + } + /*平方运算*/ + else if(e.getSource()==b23) { + x=Double.parseDouble(resultText.getText().trim()); + x=x*x; + resultText.setText(""+x); + str.setLength(0); + y=0d; + z=4; + } + /*百分号运算*/ + else if(e.getSource()==b21) { + x=Double.parseDouble(resultText.getText().trim()); + x=x/100; + resultText.setText(""+x); + str.setLength(0); + y=0d; + z=5; + } + /*阶乘运算*/ + else if(e.getSource()==b22) { + double num=Double.parseDouble(resultText.getText()); + if(num>0) { + sum=1.0; + while(num>1.0) { + sum=sum*num; + num=num-1.0; + } + resultText.setText(""+sum); + } + else {if(num==0) { + sum=1; + resultText.setText(""+sum); + } + else { + resultText.setText("负数不可以阶乘"); + } + } + z=6; + y=0d; + str.setLength(0); + } + /*开根号运算*/ + else if(e.getSource()==b16) { + x=Double.parseDouble(resultText.getText().trim()); + if(x==0) { + resultText.setText("0"); + } + else if(x<0) { + resultText.setText("根号下不能为负数"); + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + else { + resultText.setText(""+Math.sqrt(x)); + z=7; + } + str.setLength(0); + y=0d; + } + /*1/x运算*/ + else if(e.getSource()==b24) { + x=Double.parseDouble(resultText.getText().trim()); + if(x==0) { + resultText.setText("分母不能为0"); + z=15; + } + else { + resultText.setText(""+(1/x)); + z=8; + } + y=0d; + str.setLength(0); + } + /*乘Π运算*/ + else if(e.getSource()==b25) { + s=Double.parseDouble(resultText.getText().trim()); + s=s*Math.PI; + resultText.setText(""+s) ; + z=9; + y=0d; + str.setLength(0); + } + /*单击等号输出结果*/ + else if(e.getSource()==b20) { + str.setLength(0); + switch(z) { + case 0: resultText.setText(""+(x+y));flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 1:resultText.setText(""+(x-y));flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 2:resultText.setText(""+(x*y));flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 3:resultText.setText(""+(x/y));flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 4:resultText.setText(""+x);resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 5:resultText.setText(""+x);resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 6:if(sum==0) { + resultText.setText("负数不可以阶乘"); + } + else{ + resultText.setText(""+sum); + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + break; + case 7:resultText.setText(""+Math.sqrt(x));resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 8:resultText.setText(""+(1/x));resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 9:resultText.setText(""+s);resultText.setHorizontalAlignment(JTextField.RIGHT);break; + default:resultText.setText(resultText.getText());break; + } + } + /*小数点运算*/ + else if(e.getSource()==b19) { + if(resultText.getText().trim().indexOf('.')!=-1) { } /*判断字符串中是否已经包含了小数点,有即不操作*/ + else { /*如果没有小数点*/ + if(resultText.getText().trim().equals("0")) { /*如果初时显示为0*/ + resultText.setText(str.append(e.getActionCommand()).toString()); + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + else { + resultText.setText(str.append(e.getActionCommand()).toString()); + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + } + y=0d; + } + /*回退键操作*/ + else if (e.getSource()==b4) { + if(!resultText.getText().trim().equals("0")) { /*如果显示屏显示的不是零*/ + if(str.length()!=1) { + resultText.setText(str.delete(str.length()-1,str.length()).toString()); + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + else { + resultText.setText("0"); resultText.setHorizontalAlignment(JTextField.RIGHT); + str.setLength(0); + } + } + y=Double.parseDouble(resultText.getText().trim()); + } + /*按其他数字键直接输出结果框就好了*/ + else + { + resultText.setText(str.append(e.getActionCommand()).toString()); + resultText.setHorizontalAlignment(JTextField.RIGHT); + y=Double.parseDouble(resultText.getText().trim()); + } + } + /*若出现数字格式异常则提醒用户*/ + catch(NumberFormatException e1){ resultText.setText("数字格式异常"); + resultText.setHorizontalAlignment(JTextField.RIGHT); } + /*若出现字符索引过界直接输出原结果*/ + catch(StringIndexOutOfBoundsException e1) { + resultText.setText(""+resultText.getText()); + } + } +}