From f39cc3231b057e41ac31389f3b4318f80cc9e829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=92=BF=E9=92=BF?= <11106093+tiantian_chen@user.noreply.gitee.com> Date: Wed, 8 Jun 2022 20:36:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..3316af1 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,10 @@ package java2022spring; + public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); + public static void main (String [] args) { + Calculator c=new Calculator(); + c.setVisible(true); } + } -- Gitee From 8d56afbaa91b3e53c0c511ae3564895110231685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=92=BF=E9=92=BF?= <11106093+tiantian_chen@user.noreply.gitee.com> Date: Wed, 8 Jun 2022 20:53:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=9A=E5=88=9A=E6=B2=A1=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 137 +++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/java2022spring/Calculator.java diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000..8b04254 --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,137 @@ +package java2022spring; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.awt.GridLayout; +import java.awt.HeadlessException; +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; + +public class Calculator extends JFrame implements ActionListener { + private JPanel jp_north=new JPanel(); + private JTextField input_text=new JTextField(20); + private JButton c_btn=new JButton("C"); + + private JPanel jp_center=new JPanel(); + + public Calculator() throws HeadlessException { + this.init(); + this.addNorthComponent(); + this.addCenterButton(); + } + + +//初始化窗体 + public void init() { + this.setTitle("计算器"); + this.setSize(300, 400); + this.setLayout(new BorderLayout()); + this.setResizable(false); + this.setLocationRelativeTo(null); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + } + +//添加北面的控件 + public void addNorthComponent() { + jp_north.add(input_text); + this.c_btn.setForeground(new Color(225, 155, 0)); + jp_north.add(c_btn); + + c_btn.addActionListener(new ActionListener() { + + + @Override + public void actionPerformed(ActionEvent e) { + input_text.setText(""); + + } + }); + + input_text.setEditable(false);//禁止键盘录入 + + this.add(jp_north,BorderLayout.NORTH); + + } + +//添加中间的按钮 + public void addCenterButton() { + String btn_text ="123+456-789*0.=/"; + String regex="[\\+\\-*/=]"; + this.jp_center.setLayout(new GridLayout(4,4)); + for(int i=0;i<16;i++) { + String temp=btn_text.substring(i,i+1); + JButton btn=new JButton(); + btn.setText(temp); + btn.setFont(new Font("华文细黑",Font.BOLD,21)); + btn.setBackground(Color.WHITE); + if(temp.matches(regex)) { + btn.setForeground(new Color(225, 155, 0)); + } + btn.addActionListener(this); + jp_center.add(btn); + } + + this.add(jp_center,BorderLayout.CENTER); + } + + + +//运算实现 + private String str1=null; + private String str2=null; + private String operator="!"; + @Override + public void actionPerformed(ActionEvent e) { + String getStr=e.getActionCommand(); + if(".0123456789".indexOf(getStr)!=-1) { + if (getStr.equals(".")) { + if(input_text.getText().length()>0) //第一位不能是"." + this.input_text.setText(input_text.getText()+getStr); + }else { + this.input_text.setText(input_text.getText()+getStr); + } + + + }else if(getStr.matches("[\\+\\-*/]{1}")) { + operator=getStr; + str1=this.input_text.getText(); + this.input_text.setText(""); + }else if(getStr.equals("=")) { + Double a=Double.valueOf(str1); + str2=this.input_text.getText(); + Double b=Double.valueOf(str2); + Double result = null; + switch(operator) { + case "+": + result=a+b; + break; + case "-": + result=a-b; + break; + case "*": + result=a*b; + break; + case "/": + if(b!=0) { + result=a/b; + } + break; + } + this.input_text.setText(result.toString()); + +//一次运算后临时变量清零 + str1="0"; + str2="0"; + operator=null; + } + + } + +} \ No newline at end of file -- Gitee From 51073153c4c95adf6c094c239a0853e583fa4f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=92=BF=E9=92=BF?= <11106093+tiantian_chen@user.noreply.gitee.com> Date: Fri, 10 Jun 2022 12:50:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=B8=85?= =?UTF-8?q?=E9=99=A4=E5=8A=9F=E8=83=BD=EF=BC=8C=E9=99=A4=E6=95=B0=E4=B8=BA?= =?UTF-8?q?=E9=9B=B6=E6=97=B6=E6=9C=89=E6=96=87=E5=AD=97=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 8b04254..809b29b 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -17,7 +17,6 @@ public class Calculator extends JFrame implements ActionListener { private JPanel jp_north=new JPanel(); private JTextField input_text=new JTextField(20); private JButton c_btn=new JButton("C"); - private JPanel jp_center=new JPanel(); public Calculator() throws HeadlessException { @@ -49,11 +48,13 @@ public class Calculator extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { + str1="0"; + str2="0"; + operator=null; input_text.setText(""); - } }); - + input_text.setEditable(false);//禁止键盘录入 this.add(jp_north,BorderLayout.NORTH); @@ -119,7 +120,10 @@ public class Calculator extends JFrame implements ActionListener { result=a*b; break; case "/": - if(b!=0) { + if(b==0) { + this.input_text.setText("除数不能为零"); + } + else{ result=a/b; } break; -- Gitee