From 088b841b3d73a7f5cb2e58333b372c937d2f3301 Mon Sep 17 00:00:00 2001 From: 11789 <11789@LAPTOP-U57U68U6> Date: Mon, 6 Jun 2022 20:07:41 +0800 Subject: [PATCH 1/6] good --- src/java2022spring/Calculator.java | 108 +++++++++++++++++++++++++++++ 1 file changed, 108 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..daeaf78 --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,108 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class Calculator extends JFrame implements ActionListener + { + JTextArea memoryArea=new JTextArea(" ",1,5); + JTextArea process=new JTextArea(" ",1,20); + JTextArea dispresult=new JTextArea("0. ",1,30); + Font font1=new Font("Vani",Font.BOLD,30 ); + Font font2=new Font("Arial Rounded MT Bold",Font.PLAIN,10 ); + Font font3=new Font("Arial Rounded MT Bold",Font.PLAIN,20 ); + Font font4=new Font("Vani",Font.PLAIN,15); + + JButton[]jbuttons=new JButton[32]; + + double result=0,first=0,second=0; + double memery=0; + char firstsymbol='\0',secondsymbol='\0'; + boolean prev=true,repeat=true,dot=true; + Calculator() + { + super("Calculator"); + this.setTitle("计算器"); + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch(Exception e) + { + System.out.println(e); + } + JPanel resultField=new JPanel(); + resultField.setLayout(new GridLayout(3,1)); + + JPanel buttonField=new JPanel(); + Container all=getContentPane(); + + GridLayout grid1; + grid1=new GridLayout(8,4,0,0); + + memoryArea.setFont(font3); + dispresult.setFont(font3); + process.setFont(font4); + memoryArea.setEditable(false); + process.setEditable(false); + dispresult.setEditable(false); + + + resultField.add(memoryArea); + resultField.add(process); + resultField.add(dispresult); + + all.add(resultField,"North"); + + String[]buttonname={"MS","MC","MR","M+","1/x","x2","√","㏒","%","sin","cos","tan","CE","C","back","÷","7","8","9","×","4","5","6","-","1","2","3","+","±","0",".","="}; + + buttonField.setLayout(grid1); + buttonField.setBackground(Color.gray); + for(int i=0;i<4;i++) + { + + for(int j=0;j<8;j++) + { + jbuttons[i*8+j]=new JButton(buttonname[i*8+j]); + jbuttons[i*8+j].addActionListener(this); + jbuttons[i*8+j].setBackground(Color.LIGHT_GRAY ); + buttonField.add(jbuttons[i*8+j]); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + + } + } + for(int j=0;j<4;j++){ + jbuttons[j].setFont(font2); + jbuttons[j].setBackground(new Color(250,250,250)); + jbuttons[j].setPreferredSize(new Dimension(40,40)); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + } + for(int j=4;j<16;j++){ + jbuttons[j].setFont(font4); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + + } + for(int j=16;j<32;j++){ + jbuttons[j].setFont(font1); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + + } + jbuttons[15].setFont(font4); + jbuttons[19].setFont(font4); + + all.add(buttonField,"Center"); + setSize(422,405); + setResizable(true); + setVisible(true); + //this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + + + public static void main(String args[]) + { + Calculator mc=new Calculator(); + } +} + -- Gitee From 11c51ee3f5653a424b363d715b33d5076bc2c0f1 Mon Sep 17 00:00:00 2001 From: 11789 <11789@LAPTOP-U57U68U6> Date: Tue, 7 Jun 2022 20:56:28 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=8C=89=E5=8E=8B=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index daeaf78..f9abb3a 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -97,7 +97,29 @@ public class Calculator extends JFrame implements ActionListener //this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } - + public void pressNumber(String n) + { + process.setText(process.getText()+n); + if(prev) + { + dispresult.setText(n); + prev=false; + } + else dispresult.append(n); + } + public boolean divide(double d) + { + if(d==0) + { + dispresult.setText("除数不能为0!"); + prev=true; + repeat=true; + firstsymbol='\0'; + secondsymbol='\0'; + return true; + } + return false; + } public static void main(String args[]) -- Gitee From 0d6c74790ded8e9331240de3c0f6dc4c556c9819 Mon Sep 17 00:00:00 2001 From: 11789 <11789@LAPTOP-U57U68U6> Date: Tue, 7 Jun 2022 22:35:09 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=8C=89=E9=94=AE?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 106 ++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index f9abb3a..c8fd6a5 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -120,7 +120,111 @@ public class Calculator extends JFrame implements ActionListener } return false; } - + public void actionPerformed(ActionEvent e) + { + Object source=e.getSource(); + if(source==jbuttons[12]) + { + dispresult.setText("0."); + dot=true; + prev=true; + repeat=true; + return; + } + + if(source==jbuttons[13]) + { + process.setText(null); + dispresult.setText("0."); + firstsymbol='\0'; + secondsymbol='\0'; + prev=true; + repeat=true; + dot=true; + return; + } + if(source==jbuttons[14]){ + String text=process.getText(); + int i=text.length(); + if(i>0){ + text=text.substring(0,i-1); + if(text.length()==0){ + dispresult.setText("0."); + process.setText(""); + firstsymbol='\0'; + secondsymbol='\0'; + first=second=0; + prev=true; + repeat=true; + dot=true; + } + else{ + dispresult.setText(text); + process.setText(text); + } + + } + } + if(source==jbuttons[1]) + { + memery=0; + memoryArea.setText(" "); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[29]) + { + pressNumber("0"); + repeat=false; + return; + } + if(source==jbuttons[24]) + { + pressNumber("1"); + repeat=false; + return; + } + if(source==jbuttons[25]) + { + pressNumber("2"); + repeat=false; + return; + } + if(source==jbuttons[4]) + { + double temp=1/Double.parseDouble(dispresult.getText()); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[5]) + { + double temp=Double.parseDouble(dispresult.getText())*Double.parseDouble(dispresult.getText()); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[6]) + { + + if(-Double.parseDouble(dispresult.getText())<0){ + double temp=Math.sqrt(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + }else{ + dispresult.setText("请输入有效数字"); + } + + } public static void main(String args[]) { -- Gitee From 89b56df67752d05c762a5a4eecb2feba192b8ec9 Mon Sep 17 00:00:00 2001 From: 11789 <11789@LAPTOP-U57U68U6> Date: Wed, 8 Jun 2022 09:56:27 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E4=B8=8E=E8=B0=83=E6=95=B4=E5=89=8D=E9=9D=A2?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 130 +++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index c8fd6a5..1a7aa7c 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -225,7 +225,137 @@ public class Calculator extends JFrame implements ActionListener } } + if(source==jbuttons[7]) + { + + if(-Double.parseDouble(dispresult.getText())<0){ + double temp=Math.log10(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + }else{ + dispresult.setText("请输入有效数字"); + } + + } + if(source==jbuttons[8]) + { + double temp=Double.parseDouble(dispresult.getText())/100; + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[9]) + { + double temp=Math.sin(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[10]) + { + double temp=Math.cos(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[2]) + { + if(memoryArea.getText().equals(" M ")) + dispresult.setText(String.valueOf(memery)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[26]) + { + pressNumber("3"); + repeat=false; + return; + } + if(source==jbuttons[20]) + { + pressNumber("4"); + repeat=false; + return; + } + if(source==jbuttons[21]) + { + pressNumber("5"); + repeat=false; + return; + } + if(source==jbuttons[11]) + { + double temp=Math.tan(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[0]) + { + memery=Double.parseDouble(dispresult.getText()); + if(memery!=0)memoryArea.setText("M"); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[22]) + { + pressNumber("6"); + repeat=false; + return; + } + + if(source==jbuttons[16]) + { pressNumber("7"); + repeat=false; + return; + } + if(source==jbuttons[17]) + { pressNumber("8"); + repeat=false; + return;} + if(source==jbuttons[28]) + { double temp=-Double.parseDouble(dispresult.getText()); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return;} + if(source==jbuttons[3]) + { memery+=-Double.parseDouble(dispresult.getText()); + if(memery!=0) memoryArea.setText("M"); + prev=true; + repeat=false; + dot=true; + return;} + if(source==jbuttons[18]) + { pressNumber("9"); + repeat=false; + return; + } + if(source==jbuttons[30]) + { if(dot){ + pressNumber("."); + dot=false; + repeat=false; + } + return; + } public static void main(String args[]) { Calculator mc=new Calculator(); -- Gitee From 34a198e93aa57b84432605115036e48275978bdc Mon Sep 17 00:00:00 2001 From: 11789 <11789@LAPTOP-U57U68U6> Date: Wed, 8 Jun 2022 12:14:34 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E4=BD=9C=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 135 +++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 1a7aa7c..832622c 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -356,6 +356,141 @@ public class Calculator extends JFrame implements ActionListener } return; } + + if(source==jbuttons[31]) + { + second=Double.parseDouble(dispresult.getText()); + dot=true; + switch(secondsymbol) + { + case'*': + second*=first; + break; + case'/': + if(divide(second)) return; + second=first/second; + }//end of switch(secondsymbol) + secondsymbol='\0'; + switch(firstsymbol) + { + case'+': + result+=second; + break; + case'-': + result-=second; + break; + case'*': + result*=second; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second)) return; + result/=second; + } + if(firstsymbol!='\0') dispresult.setText(String.valueOf(result)); + firstsymbol='\0'; + prev=true; + repeat=false; + return; + } + if(source==jbuttons[27]) + { + process.setText(process.getText()+"+"); + dot=true; + if(repeat){ + firstsymbol='+'; + return; + + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'*': + second*=first; + break; + case'/': + if(divide(second))return; + second=first/second; + } + secondsymbol='\0'; + switch(firstsymbol){ + case'\0': + result=second; + firstsymbol='+'; + break; + case'+': + result+=second; + dispresult.setText(String.valueOf(result)); + break; + case'-': + result-=second; + firstsymbol='+'; + dispresult.setText(String.valueOf(result)); + break; + case'*': + result*=second; + firstsymbol='+'; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + firstsymbol='+'; + dispresult.setText(String.valueOf(result)); + + } + prev=true; + repeat=true; + return; + } + if(source==jbuttons[23]) + { + process.setText(process.getText()+"-"); + dot=true; + if(repeat){ + firstsymbol='-'; + return; + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'*': + second*=first; + break; + case'/': + if(divide(second))return; + second=first/second; + + } + secondsymbol='\0'; + switch(firstsymbol){ + case'\0': + result=second; + firstsymbol='-'; + break; + case'+': + result=second; + firstsymbol='-'; + dispresult.setText(String.valueOf(result)); + break; + case'-': + result-=second; + dispresult.setText(String.valueOf(result)); + break; + case'*': + result*=second; + firstsymbol='-'; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + firstsymbol='-'; + dispresult.setText(String.valueOf(result)); + + } + prev=true; + repeat=true; + return; + } public static void main(String args[]) { Calculator mc=new Calculator(); -- Gitee From ca14ac0164fe604d175dc46609b21b544f9f44b1 Mon Sep 17 00:00:00 2001 From: 11789 <11789@LAPTOP-U57U68U6> Date: Wed, 8 Jun 2022 21:15:28 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 96 ++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 832622c..7653635 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -491,6 +491,102 @@ public class Calculator extends JFrame implements ActionListener repeat=true; return; } + if(source==jbuttons[19]) + { + process.setText(process.getText()+"*"); + dot=true; + if(repeat){ + if(secondsymbol=='\0')firstsymbol='*'; + else secondsymbol='*'; + return; + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'\0': + switch(firstsymbol){ + case'\0': + firstsymbol='*'; + result=second; + break; + case'+': + case'-': + first=second; + secondsymbol='*'; + break; + case'*': + result*=second; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + dispresult.setText(String.valueOf(result)); + firstsymbol='*'; + } + break; + case'*': + first*=second; + dispresult.setText(String.valueOf(first)); + break; + case'/': + if(divide(second))return; + first/=second; + secondsymbol='*'; + dispresult.setText(String.valueOf(first)); + } + prev=true; + repeat=true; + return; + } + if(source==jbuttons[15]) + { + process.setText(process.getText()+"/"); + dot=true; + if(repeat){ + if(secondsymbol=='\0')firstsymbol='/'; + else secondsymbol='/'; + return; + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'\0': + switch(firstsymbol){ + case'\0': + firstsymbol='/'; + result=second; + break; + case'+': + case'-': + first=second; + secondsymbol='/'; + break; + case'*': + result*=second; + firstsymbol='/'; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + dispresult.setText(String.valueOf(result)); + } + break; + case'*': + first*=second; + secondsymbol='/'; + dispresult.setText(String.valueOf(first)); + break; + case'/': + if(divide(second))return; + first/=second; + dispresult.setText(String.valueOf(first)); + } + prev=true; + repeat=true; + return; + } + } + public static void main(String args[]) { Calculator mc=new Calculator(); -- Gitee