From 9380a8916a6214122e24bcf2aa0d965f2a4f8ddf Mon Sep 17 00:00:00 2001 From: Lenovo Date: Sun, 6 Jun 2021 22:45:59 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 186 +++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 src/java2020spring/Calculator.java diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java new file mode 100644 index 0000000..6efe378 --- /dev/null +++ b/src/java2020spring/Calculator.java @@ -0,0 +1,186 @@ +package java2020spring; +import java.awt.Color; +import java.awt.Container; +import java.awt.EventQueue; +import java.awt.event.*; +import javax.swing.*; + +import javax.swing.border.EmptyBorder; +import java.awt.Font; +import java.awt.Window.Type; +import java.awt.GridLayout; +import java.awt.SystemColor; + +public class Calculator extends JFrame implements ActionListener { + JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20; + private JPanel contentPane; + StringBuffer str=new StringBuffer(); + private JTextField resultText =new JTextField("0.0"); + double x,y; + int z; + 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().setForeground(new Color(0, 0, 128)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(500, 200, 430, 400); + setBackground(Color.WHITE); + setSize(434,530); + setTitle("球球的计算器"); + getContentPane().setLayout(new GridLayout(6, 4, 12, 16)); + resultText.setBackground(Color.WHITE); + resultText.setEditable(false); + resultText.setFont(new Font("Microsoft Tai Le", Font.BOLD, 35)); + resultText.setHorizontalAlignment(JTextField.RIGHT); + resultText.setBounds(10,5,255,40); + getContentPane().add(resultText); + + Container c=getContentPane(); + + JPanel p1 = new JPanel(); + getContentPane().add(p1); + p1.setLayout(new GridLayout(1, 0, 0, 0)); + + b1 = new JButton("\uFF0B"); + p1.add(b1); + b1.setForeground(new Color(0, 0, 0)); + b1.setBackground(new Color(240, 255, 240)); + b1.setFont(new Font("幼圆", Font.BOLD, 35)); + + b1.addActionListener(this); + + b2 = new JButton("-"); + p1.add(b2); + b2.setFont(new Font("幼圆", Font.BOLD, 48)); + + b3 = new JButton("C\r\n"); + p1.add(b3); + b3.setBackground(new Color(255, 255, 255)); + b3.setFont(new Font("幼圆", Font.BOLD, 27)); + b3.setForeground(new Color(0, 0, 0)); + + b4 = new JButton("\u2190"); + p1.add(b4); + b4.setFont(new Font("幼圆", Font.BOLD, 33)); + b4.setBackground(new Color(255, 255, 255)); + + JPanel p3 = new JPanel(); + getContentPane().add(p3); + p3.setLayout(new GridLayout(1, 0, 0, 0)); + + b5 = new JButton("7"); + b5.setFont(new Font("幼圆", Font.BOLD, 33)); + p3.add(b5); + + b6 = new JButton("8"); + b6.setFont(new Font("幼圆", Font.BOLD, 33)); + p3.add(b6); + + b7 = new JButton("9"); + b7.setFont(new Font("幼圆", Font.BOLD, 33)); + p3.add(b7); + + b8 = new JButton("\u00D7"); + b8.setFont(new Font("幼圆", Font.BOLD, 33)); + p3.add(b8); + + JPanel p2 = new JPanel(); + getContentPane().add(p2); + p2.setLayout(new GridLayout(1, 0, 0, 0)); + + b9 = new JButton("4"); + b9.setFont(new Font("幼圆", Font.BOLD, 33)); + p2.add(b9); + + b10 = new JButton("5"); + b10.setFont(new Font("幼圆", Font.BOLD, 33)); + p2.add(b10); + + b11 = new JButton("6"); + b11.setFont(new Font("幼圆", Font.BOLD, 33)); + p2.add(b11); + + b12 = new JButton("\u00F7"); + b12.setFont(new Font("幼圆", Font.BOLD, 33)); + p2.add(b12); + + JPanel p4 = new JPanel(); + getContentPane().add(p4); + p4.setLayout(new GridLayout(1, 0, 0, 0)); + + b13 = new JButton("1"); + b13.setFont(new Font("幼圆", Font.BOLD, 33)); + p4.add(b13); + + b14 = new JButton("2"); + b14.setFont(new Font("幼圆", Font.BOLD, 33)); + p4.add(b14); + + b15 = new JButton("3"); + b15.setFont(new Font("幼圆", Font.BOLD, 33)); + p4.add(b15); + + b16 = new JButton("\u221A"); + b16.setFont(new Font("幼圆", Font.BOLD, 29)); + p4.add(b16); + + JPanel p5 = new JPanel(); + getContentPane().add(p5); + p5.setLayout(new GridLayout(1, 0, 0, 0)); + + b17 = new JButton("-/+"); + b17.setFont(new Font("幼圆", Font.BOLD, 33)); + p5.add(b17); + + b18 = new JButton("0"); + b18.setFont(new Font("幼圆", Font.BOLD, 33)); + p5.add(b18); + + b19 = new JButton("."); + b19.setFont(new Font("幼圆", Font.BOLD, 33)); + p5.add(b19); + + b20 = new JButton("="); + b20.setBackground(new Color(255, 255, 255)); + b20.setForeground(new Color(0, 0, 0)); + b20.setFont(new Font("幼圆", Font.BOLD, 33)); + p5.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); + + } + + /*实现按钮*/ + public void actionPerformed(ActionEvent e) { + try { + + + } + } + +} -- Gitee From 3808555885d4f34f527dd078aa24fc3ea4efafc2 Mon Sep 17 00:00:00 2001 From: Lenovo Date: Mon, 7 Jun 2021 23:06:18 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=BE=85=E5=AE=8C=E5=96=84=E7=9A=84?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 120 ++++++++++++++++++++++++----- 1 file changed, 100 insertions(+), 20 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 6efe378..8ed1102 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -1,23 +1,18 @@ package java2020spring; import java.awt.Color; -import java.awt.Container; import java.awt.EventQueue; import java.awt.event.*; import javax.swing.*; - -import javax.swing.border.EmptyBorder; import java.awt.Font; -import java.awt.Window.Type; import java.awt.GridLayout; -import java.awt.SystemColor; +@SuppressWarnings("serial") public class Calculator extends JFrame implements ActionListener { JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20; - private JPanel contentPane; - StringBuffer str=new StringBuffer(); - private JTextField resultText =new JTextField("0.0"); double x,y; int z; + StringBuffer str=new StringBuffer(); + private JTextField resultText=new JTextField("0") ; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @@ -31,7 +26,6 @@ public class Calculator extends JFrame implements ActionListener { } }); } - /** * Create the frame. @@ -41,7 +35,7 @@ public class Calculator extends JFrame implements ActionListener { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(500, 200, 430, 400); setBackground(Color.WHITE); - setSize(434,530); + setSize(434,474); setTitle("球球的计算器"); getContentPane().setLayout(new GridLayout(6, 4, 12, 16)); resultText.setBackground(Color.WHITE); @@ -50,9 +44,8 @@ public class Calculator extends JFrame implements ActionListener { resultText.setHorizontalAlignment(JTextField.RIGHT); resultText.setBounds(10,5,255,40); getContentPane().add(resultText); - - Container c=getContentPane(); - + + JPanel p1 = new JPanel(); getContentPane().add(p1); p1.setLayout(new GridLayout(1, 0, 0, 0)); @@ -63,8 +56,6 @@ public class Calculator extends JFrame implements ActionListener { b1.setBackground(new Color(240, 255, 240)); b1.setFont(new Font("幼圆", Font.BOLD, 35)); - b1.addActionListener(this); - b2 = new JButton("-"); p1.add(b2); b2.setFont(new Font("幼圆", Font.BOLD, 48)); @@ -178,9 +169,98 @@ public class Calculator extends JFrame implements ActionListener { /*实现按钮*/ public void actionPerformed(ActionEvent e) { try { - - - } - } - + if(e.getSource()==b3) { + resultText.setText("0"); + str.setLength(0); + } + else if(e.getSource()==b17) { + x=Double.parseDouble(resultText.getText().trim());//trim函数作用是去掉字符串中的空格 + resultText.setText(""+(-x)); + resultText.setHorizontalAlignment(JTextField.RIGHT); + } + else if(e.getSource()==b1) { + x=Double.parseDouble(resultText.getText().trim()); + str.setLength(0); + y=0d; + z=0; + } + else if(e.getSource()==b2) { + x=Double.parseDouble(resultText.getText().trim()); + str.setLength(0); + y=0d; + z=1; + } + else if(e.getSource()==b8)//单击乘号按钮获得x的值和z的值并清空y的值 + { + x=Double.parseDouble(resultText.getText().trim()); + str.setLength(0); + y=0d; + z=2; + } + else if(e.getSource()==b12) { + x=Double.parseDouble(resultText.getText().trim()); + str.setLength(0); + y=0d; + z=3; + } + else if(e.getSource()==b20) { + str.setLength(0); + switch(z) { + case 0: resultText.setText(""+(x+y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 1:resultText.setText(""+(x-y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 2:resultText.setText(""+(x*y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 3:resultText.setText(""+(x/y)); resultText.setHorizontalAlignment(JTextField.RIGHT);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()); + } + else if(resultText.getText().trim().equals("")) /*如果初时显示为空则不做任何操作*/ + { } + else + { + resultText.setText(str.append(e.getActionCommand()).toString()); + } + } + y=0d; + } + else if(e.getSource()==b16) /*开根号运算*/ + { + x=Double.parseDouble(resultText.getText().trim()); + resultText.setText(""+Math.sqrt(x)); + str.setLength(0); + 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"); + str.setLength(0); + } + } + y=Double.parseDouble(resultText.getText().trim()); + } + else + { + resultText.setText(str.append(e.getActionCommand()).toString()); + y=Double.parseDouble(resultText.getText().trim()); + } + } + catch(StringIndexOutOfBoundsException e1){resultText.setText("字符串索引越界"); + + } + } } -- Gitee From 77bbaa5ba3f04b784d37ff9112890177481db5e2 Mon Sep 17 00:00:00 2001 From: Lenovo Date: Tue, 8 Jun 2021 22:28:14 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=BE=85=E5=AE=8C?= =?UTF-8?q?=E5=96=84=EF=BC=8C=E8=A6=81=E4=BF=AE=E6=94=B9=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E5=A4=84=E7=90=86=EF=BC=88=E5=A6=82=E6=9E=9C=E5=A4=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 317 ++++++++++++++++++----------- 1 file changed, 196 insertions(+), 121 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 8ed1102..4657e9a 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -8,13 +8,14 @@ import java.awt.GridLayout; @SuppressWarnings("serial") public class Calculator extends JFrame implements ActionListener { - JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20; - double x,y; + 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; - StringBuffer str=new StringBuffer(); - private JTextField resultText=new JTextField("0") ; + double sum;/*阶乘结果数*/ + double s; + StringBuffer str=new StringBuffer(); /*建立字符缓冲区*/ + private JTextField resultText=new JTextField("0") ; /*显示结果文本框*/ public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { public void run() { try { @@ -26,132 +27,126 @@ public class Calculator extends JFrame implements ActionListener { } }); } - /** * Create the frame. */ - public Calculator() { - getContentPane().setForeground(new Color(0, 0, 128)); + public Calculator() { /*初始化计算器页面,结果文本框颜色字体以及按钮初始化*/ + getContentPane().setForeground(new Color(255, 250, 205)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(500, 200, 430, 400); setBackground(Color.WHITE); - setSize(434,474); + setSize(466,484); setTitle("球球的计算器"); - getContentPane().setLayout(new GridLayout(6, 4, 12, 16)); + + getContentPane().setLayout(new GridLayout(0, 1, 0, 0)); + resultText.setBackground(Color.WHITE); resultText.setEditable(false); - resultText.setFont(new Font("Microsoft Tai Le", Font.BOLD, 35)); - resultText.setHorizontalAlignment(JTextField.RIGHT); - resultText.setBounds(10,5,255,40); + resultText.setFont(new Font("华文楷体", Font.BOLD, 40)); + resultText.setHorizontalAlignment(SwingConstants.RIGHT); + resultText.setBounds(5,4,200,40); getContentPane().add(resultText); - - JPanel p1 = new JPanel(); + JPanel p1 = new JPanel(); getContentPane().add(p1); - p1.setLayout(new GridLayout(1, 0, 0, 0)); + p1.setLayout(new GridLayout(0, 5, 0, 0)); b1 = new JButton("\uFF0B"); - p1.add(b1); b1.setForeground(new Color(0, 0, 0)); - b1.setBackground(new Color(240, 255, 240)); - b1.setFont(new Font("幼圆", Font.BOLD, 35)); - - b2 = new JButton("-"); - p1.add(b2); - b2.setFont(new Font("幼圆", Font.BOLD, 48)); + b1.setBackground(Color.WHITE); + b1.setFont(new Font("华文楷体", Font.BOLD, 26)); - b3 = new JButton("C\r\n"); - p1.add(b3); - b3.setBackground(new Color(255, 255, 255)); - b3.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"); - p1.add(b4); + b4 = new JButton("\u2190"); b4.setFont(new Font("幼圆", Font.BOLD, 33)); - b4.setBackground(new Color(255, 255, 255)); + b4.setBackground(Color.WHITE); - JPanel p3 = new JPanel(); - getContentPane().add(p3); - p3.setLayout(new GridLayout(1, 0, 0, 0)); - - b5 = new JButton("7"); + b5 = new JButton("7"); b5.setFont(new Font("幼圆", Font.BOLD, 33)); - p3.add(b5); b6 = new JButton("8"); b6.setFont(new Font("幼圆", Font.BOLD, 33)); - p3.add(b6); b7 = new JButton("9"); b7.setFont(new Font("幼圆", Font.BOLD, 33)); - p3.add(b7); - - b8 = new JButton("\u00D7"); - b8.setFont(new Font("幼圆", Font.BOLD, 33)); - p3.add(b8); - JPanel p2 = new JPanel(); - getContentPane().add(p2); - p2.setLayout(new GridLayout(1, 0, 0, 0)); + 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)); - p2.add(b9); b10 = new JButton("5"); b10.setFont(new Font("幼圆", Font.BOLD, 33)); - p2.add(b10); b11 = new JButton("6"); b11.setFont(new Font("幼圆", Font.BOLD, 33)); - p2.add(b11); b12 = new JButton("\u00F7"); + b12.setBackground(Color.WHITE); b12.setFont(new Font("幼圆", Font.BOLD, 33)); - p2.add(b12); - - JPanel p4 = new JPanel(); - getContentPane().add(p4); - p4.setLayout(new GridLayout(1, 0, 0, 0)); b13 = new JButton("1"); b13.setFont(new Font("幼圆", Font.BOLD, 33)); - p4.add(b13); b14 = new JButton("2"); b14.setFont(new Font("幼圆", Font.BOLD, 33)); - p4.add(b14); - b15 = new JButton("3"); + b15 = new JButton("3"); b15.setFont(new Font("幼圆", Font.BOLD, 33)); - p4.add(b15); b16 = new JButton("\u221A"); + b16.setBackground(Color.WHITE); b16.setFont(new Font("幼圆", Font.BOLD, 29)); - p4.add(b16); - - JPanel p5 = new JPanel(); - getContentPane().add(p5); - p5.setLayout(new GridLayout(1, 0, 0, 0)); b17 = new JButton("-/+"); - b17.setFont(new Font("幼圆", Font.BOLD, 33)); - p5.add(b17); + b17.setFont(new Font("华文宋体", Font.BOLD, 30)); b18 = new JButton("0"); b18.setFont(new Font("幼圆", Font.BOLD, 33)); - p5.add(b18); b19 = new JButton("."); b19.setFont(new Font("幼圆", Font.BOLD, 33)); - p5.add(b19); b20 = new JButton("="); - b20.setBackground(new Color(255, 255, 255)); + b20.setBackground(Color.WHITE); b20.setForeground(new Color(0, 0, 0)); - b20.setFont(new Font("幼圆", Font.BOLD, 33)); - p5.add(b20); + b20.setFont(new Font("华文楷体", Font.BOLD, 36)); + + b21 = new JButton("%"); + b21.setBackground(Color.WHITE); + b21.setFont(new Font("华文楷体", Font.BOLD, 23)); + + b22 = new JButton("!"); + b22.setBackground(Color.WHITE); + b22.setFont(new Font("华文楷体", Font.BOLD, 29)); + + 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, 28)); + + 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); @@ -163,104 +158,184 @@ public class Calculator extends JFrame implements ActionListener { 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 { - if(e.getSource()==b3) { + /*清除键C操作*/ + if(e.getSource()==b3) { resultText.setText("0"); str.setLength(0); } - else if(e.getSource()==b17) { - x=Double.parseDouble(resultText.getText().trim());//trim函数作用是去掉字符串中的空格 + /*加负号操作*/ + 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)); resultText.setHorizontalAlignment(JTextField.RIGHT); } - else if(e.getSource()==b1) { + } + /*x+z的值并清空y的值*/ + else if(e.getSource()==b1) { x=Double.parseDouble(resultText.getText().trim()); str.setLength(0); y=0d; z=0; } - else if(e.getSource()==b2) { + /*x-z的值并清空y的值*/ + else if(e.getSource()==b2) { x=Double.parseDouble(resultText.getText().trim()); str.setLength(0); y=0d; z=1; } - else if(e.getSource()==b8)//单击乘号按钮获得x的值和z的值并清空y的值 - { + /*x*z的值并清空y的值*/ + else if(e.getSource()==b8) { x=Double.parseDouble(resultText.getText().trim()); str.setLength(0); y=0d; z=2; } - else if(e.getSource()==b12) { + /*x÷z的值并清空y的值*/ + else if(e.getSource()==b12) { x=Double.parseDouble(resultText.getText().trim()); str.setLength(0); y=0d; z=3; } - else if(e.getSource()==b20) { + /*平方运算*/ + 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()); + sum=1.0; + while(num>1.0) { + sum=sum*num; + num=num-1.0; + } + resultText.setText(""+sum); + z=6; + str.setLength(0); + y=0d; + } + /*开根号运算*/ + 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"); + } + else { + resultText.setText(""+(1/x)); + z=8; + } + str.setLength(0); + y=0d; + } + /*乘Π运算*/ + else if(e.getSource()==b25) { + s=Double.parseDouble(resultText.getText().trim()); + s=s*Math.PI; + resultText.setText(""+s) ; + z=9; + str.setLength(0); + y=0d; + } + /*单击等号输出结果*/ + else if(e.getSource()==b20) { str.setLength(0); switch(z) { - case 0: resultText.setText(""+(x+y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 0: resultText.setText(""+(x+y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 1:resultText.setText(""+(x-y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 2:resultText.setText(""+(x*y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 3:resultText.setText(""+(x/y)); 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: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; } } + /*小数点运算*/ else if(e.getSource()==b19) { - if(resultText.getText().trim().indexOf('.')!=-1) { } /*判断字符串中是否已经包含了小数点*/ - else /*如果没有小数点*/ - { - if(resultText.getText().trim().equals("0")) /*如果初时显示为0*/ - { + 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 if(resultText.getText().trim().equals("")) /*如果初时显示为空则不做任何操作*/ - { } - else - { + else if(resultText.getText().trim().equals("")) { } /*如果初时显示为空则不做任何操作*/ + else { resultText.setText(str.append(e.getActionCommand()).toString()); + resultText.setHorizontalAlignment(JTextField.RIGHT); } } y=0d; } - else if(e.getSource()==b16) /*开根号运算*/ - { - x=Double.parseDouble(resultText.getText().trim()); - resultText.setText(""+Math.sqrt(x)); - str.setLength(0); - 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"); - str.setLength(0); - } - } - y=Double.parseDouble(resultText.getText().trim()); - } - else - { - resultText.setText(str.append(e.getActionCommand()).toString()); + /*回退键操作*/ + 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()); - } - } - catch(StringIndexOutOfBoundsException e1){resultText.setText("字符串索引越界"); - - } + } + /*按其他数字键直接输出结果框就好了*/ + else + { + resultText.setText(str.append(e.getActionCommand()).toString()); + resultText.setHorizontalAlignment(JTextField.RIGHT); + y=Double.parseDouble(resultText.getText().trim()); + } + } + catch(StringIndexOutOfBoundsException e1) { + resultText.setText(""+resultText.getText()); /*若出现字符索引过界直接输出原结果*/ + } } } -- Gitee From 103daff56e80e91b3d3d580d9d9f0458287d5809 Mon Sep 17 00:00:00 2001 From: Lenovo Date: Wed, 9 Jun 2021 17:00:02 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E9=94=99=E8=AF=AF=E6=8F=90=E9=86=92=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=EF=BC=8C=E6=83=B3?= =?UTF-8?q?=E5=8A=9E=E6=B3=95=E5=86=8D=E5=BC=84=E8=BF=9E=E7=BB=AD=E5=8A=A0?= =?UTF-8?q?=E4=B8=A4=E4=B8=89=E4=B8=AA=E6=95=B0=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 42 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 4657e9a..462d93e 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -3,6 +3,7 @@ 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; @@ -15,6 +16,7 @@ public class Calculator extends JFrame implements ActionListener { double s; StringBuffer str=new StringBuffer(); /*建立字符缓冲区*/ private JTextField resultText=new JTextField("0") ; /*显示结果文本框*/ + public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { @@ -30,31 +32,36 @@ public class Calculator extends JFrame implements ActionListener { /** * Create the frame. */ - public Calculator() { /*初始化计算器页面,结果文本框颜色字体以及按钮初始化*/ + public Calculator() { + getContentPane().setBackground(Color.WHITE); /*初始化计算器页面,结果文本框颜色字体以及按钮初始化*/ getContentPane().setForeground(new Color(255, 250, 205)); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setBounds(500, 200, 430, 400); + getContentPane().setLayout(new GridLayout(2, 0, 25, 20)); + + setBounds(400, 200, 250, 200); setBackground(Color.WHITE); - setSize(466,484); + setSize(466,486); setTitle("球球的计算器"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - getContentPane().setLayout(new GridLayout(0, 1, 0, 0)); - - resultText.setBackground(Color.WHITE); resultText.setEditable(false); - resultText.setFont(new Font("华文楷体", Font.BOLD, 40)); + resultText.setBackground(Color.WHITE); + resultText.setFont(new Font("华文细黑", Font.BOLD, 42)); resultText.setHorizontalAlignment(SwingConstants.RIGHT); - resultText.setBounds(5,4,200,40); + resultText.setSize(46,86); + resultText.setBorder(new EmptyBorder(5, 0, 2, 2)); getContentPane().add(resultText); JPanel p1 = new JPanel(); + p1.setBackground(Color.WHITE); getContentPane().add(p1); - p1.setLayout(new GridLayout(0, 5, 0, 0)); - + + p1.setSize(400,400); + p1.setLayout(new GridLayout(5, 5, 2, 3)); + p1.setBorder(new EmptyBorder(0, 0, 0, 0)); b1 = new JButton("\uFF0B"); b1.setForeground(new Color(0, 0, 0)); b1.setBackground(Color.WHITE); - b1.setFont(new Font("华文楷体", Font.BOLD, 26)); + b1.setFont(new Font("华文楷体", Font.BOLD, 27)); b2 = new JButton("\uFE63"); b2.setForeground(new Color(0, 0, 0)); @@ -116,7 +123,7 @@ public class Calculator extends JFrame implements ActionListener { b18.setFont(new Font("幼圆", Font.BOLD, 33)); b19 = new JButton("."); - b19.setFont(new Font("幼圆", Font.BOLD, 33)); + b19.setFont(new Font("幼圆", Font.BOLD, 35)); b20 = new JButton("="); b20.setBackground(Color.WHITE); @@ -125,11 +132,11 @@ public class Calculator extends JFrame implements ActionListener { b21 = new JButton("%"); b21.setBackground(Color.WHITE); - b21.setFont(new Font("华文楷体", Font.BOLD, 23)); + b21.setFont(new Font("华文楷体", Font.BOLD, 25)); b22 = new JButton("!"); b22.setBackground(Color.WHITE); - b22.setFont(new Font("华文楷体", Font.BOLD, 29)); + b22.setFont(new Font("华文楷体", Font.BOLD, 31)); b23 = new JButton("x\u00B2"); b23.setBackground(Color.WHITE); @@ -141,7 +148,7 @@ public class Calculator extends JFrame implements ActionListener { b25 = new JButton("\u03C0"); b25.setBackground(Color.WHITE); - b25.setFont(new Font("华文楷体", Font.BOLD, 28)); + 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); @@ -169,6 +176,8 @@ public class Calculator extends JFrame implements ActionListener { if(e.getSource()==b3) { resultText.setText("0"); str.setLength(0); + y=0d; + z=10; } /*加负号操作*/ else if(e.getSource()==b17) { @@ -294,6 +303,7 @@ public class Calculator extends JFrame implements ActionListener { 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; + case 10:resultText.setText("0");resultText.setHorizontalAlignment(JTextField.RIGHT);break; } } /*小数点运算*/ -- Gitee From 008c6b04e5fba9ef379bce91b411940d927b117c Mon Sep 17 00:00:00 2001 From: Lenovo Date: Wed, 9 Jun 2021 20:13:54 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=B0=8F=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 462d93e..b51b6f4 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -176,7 +176,7 @@ public class Calculator extends JFrame implements ActionListener { if(e.getSource()==b3) { resultText.setText("0"); str.setLength(0); - y=0d; + y=0d; z=10; } /*加负号操作*/ @@ -192,11 +192,10 @@ public class Calculator extends JFrame implements ActionListener { } } /*x+z的值并清空y的值*/ - else if(e.getSource()==b1) { - x=Double.parseDouble(resultText.getText().trim()); - str.setLength(0); - y=0d; - z=0; + else if(e.getSource()==b1) { + x=Double.parseDouble(resultText.getText().trim()); + z=0; + str.setLength(0); } /*x-z的值并清空y的值*/ else if(e.getSource()==b2) { @@ -290,10 +289,10 @@ public class Calculator extends JFrame implements ActionListener { y=0d; } /*单击等号输出结果*/ - else if(e.getSource()==b20) { + else if(e.getSource()==b20) { str.setLength(0); switch(z) { - case 0: resultText.setText(""+(x+y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 0: resultText.setText(""+(x+y));resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 1:resultText.setText(""+(x-y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 2:resultText.setText(""+(x*y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 3:resultText.setText(""+(x/y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; @@ -304,7 +303,8 @@ public class Calculator extends JFrame implements ActionListener { case 8:resultText.setText(""+(1/x));resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 9:resultText.setText(""+s);resultText.setHorizontalAlignment(JTextField.RIGHT);break; case 10:resultText.setText("0");resultText.setHorizontalAlignment(JTextField.RIGHT);break; - } + default:resultText.setText(resultText.getText());break; + } } /*小数点运算*/ else if(e.getSource()==b19) { @@ -314,7 +314,6 @@ public class Calculator extends JFrame implements ActionListener { resultText.setText(str.append(e.getActionCommand()).toString()); resultText.setHorizontalAlignment(JTextField.RIGHT); } - else if(resultText.getText().trim().equals("")) { } /*如果初时显示为空则不做任何操作*/ else { resultText.setText(str.append(e.getActionCommand()).toString()); resultText.setHorizontalAlignment(JTextField.RIGHT); -- Gitee From 10e53e856d0db6cc280fde0040b7530a12163e67 Mon Sep 17 00:00:00 2001 From: Lenovo Date: Thu, 10 Jun 2021 09:37:14 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E4=BA=86=E8=BF=9E?= =?UTF-8?q?=E5=8A=A0=E8=BF=9E=E5=87=8F=E8=BF=9E=E9=99=A4=E8=BF=9E=E4=B9=98?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E4=BB=A5=E5=8F=8A=E9=98=B6=E4=B9=98=E7=9A=84?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 88 ++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 29 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index b51b6f4..e72592b 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -13,7 +13,8 @@ public class Calculator extends JFrame implements ActionListener { double x,y; /*运算数x,y*/ int z; double sum;/*阶乘结果数*/ - double s; + double s,j; + int flag=0; StringBuffer str=new StringBuffer(); /*建立字符缓冲区*/ private JTextField resultText=new JTextField("0") ; /*显示结果文本框*/ @@ -173,10 +174,11 @@ public class Calculator extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { try { /*清除键C操作*/ - if(e.getSource()==b3) { - resultText.setText("0"); - str.setLength(0); + if(e.getSource()==b3) { y=0d; + flag=0; + resultText.setText("0"); + str.setLength(0); z=10; } /*加负号操作*/ @@ -191,32 +193,51 @@ public class Calculator extends JFrame implements ActionListener { resultText.setHorizontalAlignment(JTextField.RIGHT); } } - /*x+z的值并清空y的值*/ - else if(e.getSource()==b1) { - x=Double.parseDouble(resultText.getText().trim()); - z=0; - str.setLength(0); + /*连加操作*/ + 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); } /*x-z的值并清空y的值*/ else if(e.getSource()==b2) { - x=Double.parseDouble(resultText.getText().trim()); - str.setLength(0); - y=0d; - z=1; + if(flag==0) { + x=Double.parseDouble(resultText.getText().trim());flag=1;; + } + else { + x=x-y; + y=0d;} + z=1; + str.setLength(0); } /*x*z的值并清空y的值*/ else if(e.getSource()==b8) { - x=Double.parseDouble(resultText.getText().trim()); - str.setLength(0); - y=0d; - z=2; + if(flag==0) { + x=Double.parseDouble(resultText.getText().trim());flag=1; + } + else { x=x*y; + y=0d; + } + z=2; + str.setLength(0); } /*x÷z的值并清空y的值*/ else if(e.getSource()==b12) { - x=Double.parseDouble(resultText.getText().trim()); + if(flag==0) { + x=Double.parseDouble(resultText.getText().trim());flag=1; + } + else{ + x=x/y; + y=0d; + } + z=3; str.setLength(0); - y=0d; - z=3; } /*平方运算*/ else if(e.getSource()==b23) { @@ -239,15 +260,23 @@ public class Calculator extends JFrame implements ActionListener { /*阶乘运算*/ else if(e.getSource()==b22) { double num=Double.parseDouble(resultText.getText()); - sum=1.0; + if(num>0) { + sum=1.0; while(num>1.0) { sum=sum*num; num=num-1.0; + } + } + else if(num==0) { + sum=1; + } + else { + sum=0;resultText.setText("负数不可以阶乘"); } resultText.setText(""+sum); z=6; + y=0d; str.setLength(0); - y=0d; } /*开根号运算*/ else if(e.getSource()==b16) { @@ -276,8 +305,8 @@ public class Calculator extends JFrame implements ActionListener { resultText.setText(""+(1/x)); z=8; } - str.setLength(0); - y=0d; + y=0d; + str.setLength(0); } /*乘Π运算*/ else if(e.getSource()==b25) { @@ -285,17 +314,17 @@ public class Calculator extends JFrame implements ActionListener { s=s*Math.PI; resultText.setText(""+s) ; z=9; + y=0d; str.setLength(0); - y=0d; } /*单击等号输出结果*/ else if(e.getSource()==b20) { str.setLength(0); switch(z) { - case 0: resultText.setText(""+(x+y));resultText.setHorizontalAlignment(JTextField.RIGHT);break; - case 1:resultText.setText(""+(x-y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; - case 2:resultText.setText(""+(x*y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; - case 3:resultText.setText(""+(x/y)); resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 0: resultText.setText(""+(x+y));x=0d;y=0d;flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 1:resultText.setText(""+(x-y));x=0d;y=0d;flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 2:resultText.setText(""+(x*y));x=0d;y=0d;flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + case 3:resultText.setText(""+(x/y));x=0d;y=0d;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:resultText.setText(""+sum);resultText.setHorizontalAlignment(JTextField.RIGHT);break; @@ -319,6 +348,7 @@ public class Calculator extends JFrame implements ActionListener { resultText.setHorizontalAlignment(JTextField.RIGHT); } } + z=11; y=0d; } /*回退键操作*/ -- Gitee From 6123daab250544f1d628ee49ebe70c094e2e74ed Mon Sep 17 00:00:00 2001 From: Lenovo Date: Thu, 10 Jun 2021 21:24:52 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E6=9C=80=E7=BB=88=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 90 ++++++++++++++++++------------ 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index e72592b..28d609d 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -12,9 +12,9 @@ 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,j; - int flag=0; + double sum; /*阶乘结果数*/ + double s; /*乘Π运算结果数*/ + int flag=0; /*标志变量*/ StringBuffer str=new StringBuffer(); /*建立字符缓冲区*/ private JTextField resultText=new JTextField("0") ; /*显示结果文本框*/ @@ -37,7 +37,6 @@ public class Calculator extends JFrame implements ActionListener { 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); @@ -54,11 +53,11 @@ public class Calculator extends JFrame implements ActionListener { JPanel p1 = new JPanel(); p1.setBackground(Color.WHITE); - getContentPane().add(p1); - 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); @@ -169,31 +168,34 @@ public class Calculator extends JFrame implements ActionListener { 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); - z=10; } /*加负号操作*/ else if(e.getSource()==b17) { x=Double.parseDouble(resultText.getText().trim()); - if(x==0.0) { /*若显示屏为零则不用给0加负号*/ + 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; @@ -201,43 +203,45 @@ public class Calculator extends JFrame implements ActionListener { else { x=x+y; y=0d; - } - z=0; + } + z=0; str.setLength(0); } - /*x-z的值并清空y的值*/ + /*连减和减法操作*/ else if(e.getSource()==b2) { if(flag==0) { - x=Double.parseDouble(resultText.getText().trim());flag=1;; + x=Double.parseDouble(resultText.getText().trim());flag=1; } else { x=x-y; - y=0d;} - z=1; - str.setLength(0); + y=0d; + } + z=1; + str.setLength(0); } - /*x*z的值并清空y的值*/ + /*连乘和乘法操作*/ else if(e.getSource()==b8) { if(flag==0) { x=Double.parseDouble(resultText.getText().trim());flag=1; } - else { x=x*y; + else { + x=x*y; y=0d; - } + } z=2; str.setLength(0); } - /*x÷z的值并清空y的值*/ + /*连除和除法操作*/ else if(e.getSource()==b12) { if(flag==0) { x=Double.parseDouble(resultText.getText().trim());flag=1; } else{ - x=x/y; - y=0d; - } + x=x/y; + y=0d; + } z=3; - str.setLength(0); + str.setLength(0); } /*平方运算*/ else if(e.getSource()==b23) { @@ -266,14 +270,16 @@ public class Calculator extends JFrame implements ActionListener { sum=sum*num; num=num-1.0; } + resultText.setText(""+sum); } - else if(num==0) { + else {if(num==0) { sum=1; + resultText.setText(""+sum); } else { - sum=0;resultText.setText("负数不可以阶乘"); + resultText.setText("负数不可以阶乘"); + } } - resultText.setText(""+sum); z=6; y=0d; str.setLength(0); @@ -300,6 +306,7 @@ public class Calculator extends JFrame implements ActionListener { x=Double.parseDouble(resultText.getText().trim()); if(x==0) { resultText.setText("分母不能为0"); + z=15; } else { resultText.setText(""+(1/x)); @@ -321,17 +328,23 @@ public class Calculator extends JFrame implements ActionListener { else if(e.getSource()==b20) { str.setLength(0); switch(z) { - case 0: resultText.setText(""+(x+y));x=0d;y=0d;flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; - case 1:resultText.setText(""+(x-y));x=0d;y=0d;flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; - case 2:resultText.setText(""+(x*y));x=0d;y=0d;flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; - case 3:resultText.setText(""+(x/y));x=0d;y=0d;flag=0;resultText.setHorizontalAlignment(JTextField.RIGHT);break; + 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:resultText.setText(""+sum);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; - case 10:resultText.setText("0");resultText.setHorizontalAlignment(JTextField.RIGHT);break; default:resultText.setText(resultText.getText());break; } } @@ -348,8 +361,7 @@ public class Calculator extends JFrame implements ActionListener { resultText.setHorizontalAlignment(JTextField.RIGHT); } } - z=11; - y=0d; + y=0d; } /*回退键操作*/ else if (e.getSource()==b4) { @@ -373,8 +385,12 @@ public class Calculator extends JFrame implements ActionListener { y=Double.parseDouble(resultText.getText().trim()); } } + /*若出现数字格式异常则提醒用户*/ + catch(NumberFormatException e1){ resultText.setText("数字格式异常"); + resultText.setHorizontalAlignment(JTextField.RIGHT); } + /*若出现字符索引过界直接输出原结果*/ catch(StringIndexOutOfBoundsException e1) { - resultText.setText(""+resultText.getText()); /*若出现字符索引过界直接输出原结果*/ + resultText.setText(""+resultText.getText()); } } } -- Gitee