From 6cecfdc15699ae72663ab6c5e3742ccc58713a1d Mon Sep 17 00:00:00 2001 From: Eilien <1415399624@qq.com> Date: Wed, 9 Jun 2021 22:49:55 +0800 Subject: [PATCH 1/3] add src/java2020spring/Caculator. --- src/java2020spring/Caculator | 196 +++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 src/java2020spring/Caculator diff --git a/src/java2020spring/Caculator b/src/java2020spring/Caculator new file mode 100644 index 0000000..5c1587f --- /dev/null +++ b/src/java2020spring/Caculator @@ -0,0 +1,196 @@ + +import java.awt.color.*; +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Set; +public class Caculator extends JFrame{ + //标签 + JLabel show= new JLabel();//数值框框 + JPanel main= new JPanel();//主面板 + JPanel shuziPanel= new JPanel();//数字面板 + boolean start;//判断是否重新开始 + int count;//小数点计数 + double result;//接受结果 + String fuhao;//接受符号 + public static void main(String[] args) { + new Caculator(); + + } + public Caculator() { + init(); + } + +//布局 + private void init() { + //窗体大小 + this.setSize(350,200); + //显示窗体 + this.setVisible(true); + //窗体居中 + this.setLocationRelativeTo(null); + //设置点击退出则关闭程序 + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + //设置标题 + this.setTitle("Caculator"); + //显示框默认值为0 + show.setText("0"); + //默认开始 + start=true; + //默认小数点次数为0 + count=0; + //结果默认为0 + result=0; + //默认fuhao="=" + fuhao="="; + + //按钮设置布局 + shuziPanel.setLayout(new GridLayout(6,4)); + //准备监听对象 + Action1 action1=new Action1();//数字按钮 + Action2 action2=new Action2();//fuhao按钮 + Action3 action3=new Action3();//功能按钮 + //添加按钮 + this.addButton("TIME",action3); + this.addButton("M+",action3); + this.addButton("Backspace",action3); + this.addButton("C",action3); + this.addButton("7",action1); + this.addButton("8",action1); + this.addButton("9",action1); + this.addButton("+",action2); + this.addButton("4",action1); + this.addButton("5",action1); + this.addButton("6",action1); + this.addButton("-",action2); + this.addButton("1",action1); + this.addButton("2",action1); + this.addButton("3",action1); + this.addButton("*",action2); + this.addButton(".",action1); + this.addButton("0",action1); + this.addButton("=",action2); + this.addButton("/",action2); + this.addButton("%",action2); + this.addButton("sqrt",action2); + this.addButton("Mess",action3); + this.addButton("1/x",action2); + //主面板添加数字按键 + main.setLayout(new BorderLayout());; + main.add(show,"North");//数字面板显示在北方 + main.add(shuziPanel);//添加数字面板 + + //添加窗体 + this.add(main); + } + public void addButton(String shuzi,ActionListener a1 ) { + JButton button=new JButton(shuzi); + button.addActionListener(a1); + shuziPanel.add(button); + + } + + class Action1 implements ActionListener{ + + public void actionPerformed(ActionEvent e) { + String input=e.getActionCommand();//显示按钮的数字 + if(show.getText().equals("0")) { + start=true;} + if(start) { + count=0; + show.setText(""); + start=false; + } + if(input.equals(".")) { + if(count==0) { + show.setText("0"+show.getText()+input);//显示数值 + } + else { + show.setText(show.getText()+"");//显示数值 + } + count++; + } + if(!input.equals(".")) { + show.setText(show.getText()+input); + } + } + + + + } + //fuhao监听方法 + class Action2 implements ActionListener{ + public void actionPerformed(ActionEvent e) { + String input=e.getActionCommand(); + if(start) { + fuhao=input;}//开始新计算,接受符号 + else { + if(fuhao.equals("+")) { + result +=Double.parseDouble(show.getText()); + }else if(fuhao.equals("-")) { + result -=Double.parseDouble(show.getText()); + }else if(fuhao.equals("*")) { + result *=Double.parseDouble(show.getText()); + }else if(fuhao.equals("/")) { + result /=Double.parseDouble(show.getText()); + }else if(fuhao.equals("%")) { + result =Double.parseDouble(show.getText())/100; + }else if(fuhao.equals("sqrt")) { + result =Math.sqrt(Double.parseDouble(show.getText())); + }else if(input.equals("1/x")) { + if(Double.parseDouble(show.getText())==0) { + show.setText("0无倒数"); + } + else { + result=1/Double.parseDouble(show.getText()); + + } + } + else { + result =Double.parseDouble(show.getText()); + } + show.setText(result+""); + fuhao=input; + start=true; + + } + } + } + class Action3 implements ActionListener{ + public void actionPerformed(ActionEvent e) { + String input=e.getActionCommand(); + if(input.equals("TIME")) { + show.setText(new SimpleDateFormat("yyyy-MM-HH:mm:ss").format(new Date())); + start=true; + } + if(input.equals("C")) { + //清0 + show.setText("0");result=0; + } + if(input.equals("M+")) { + show.setText("暂无此功能"); + start=true; + } + + + + if(input.equals("Mess")) { + show.setText("20信管4周浩特"); + } + if(input.equals("Backspace")) { + //后退一步 + if(show.getText().length()>1) { + show.setText(show.getText().substring(0, show.getText().length()-1)); + }else { + show.setText("0"); + } + } + } + } + + + } + + -- Gitee From 2f1380423a64f780835b4b8258c1c0a7c21fe88f Mon Sep 17 00:00:00 2001 From: Eilien <1415399624@qq.com> Date: Wed, 9 Jun 2021 22:50:27 +0800 Subject: [PATCH 2/3] update src/java2020spring/Caculator. --- src/java2020spring/Caculator | 1 + 1 file changed, 1 insertion(+) diff --git a/src/java2020spring/Caculator b/src/java2020spring/Caculator index 5c1587f..b8afd09 100644 --- a/src/java2020spring/Caculator +++ b/src/java2020spring/Caculator @@ -1,3 +1,4 @@ +package java2020spring; import java.awt.color.*; import java.awt.*; -- Gitee From 40c9b8f94829229b61f7035303a1653e98cfc765 Mon Sep 17 00:00:00 2001 From: Eilien <1415399624@qq.com> Date: Thu, 10 Jun 2021 18:26:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?update=20src/java2020spring/Caculator.=2020?= =?UTF-8?q?21.6.10=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=B9=B3=E6=96=B9=E3=80=81?= =?UTF-8?q?=E7=AB=8B=E6=96=B9=E3=80=81=E5=BC=80=E7=AB=8B=E6=96=B9=E3=80=81?= =?UTF-8?q?=E5=BC=80=E5=B9=B3=E6=94=BE=E3=80=81=E5=8F=96=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=80=BC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Caculator | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/java2020spring/Caculator b/src/java2020spring/Caculator index b8afd09..a7ed580 100644 --- a/src/java2020spring/Caculator +++ b/src/java2020spring/Caculator @@ -48,14 +48,14 @@ public class Caculator extends JFrame{ fuhao="="; //按钮设置布局 - shuziPanel.setLayout(new GridLayout(6,4)); + shuziPanel.setLayout(new GridLayout(7,4)); //准备监听对象 Action1 action1=new Action1();//数字按钮 Action2 action2=new Action2();//fuhao按钮 Action3 action3=new Action3();//功能按钮 //添加按钮 this.addButton("TIME",action3); - this.addButton("M+",action3); + this.addButton("Mess",action3); this.addButton("Backspace",action3); this.addButton("C",action3); this.addButton("7",action1); @@ -76,8 +76,12 @@ public class Caculator extends JFrame{ this.addButton("/",action2); this.addButton("%",action2); this.addButton("sqrt",action2); - this.addButton("Mess",action3); + this.addButton("M+",action3); this.addButton("1/x",action2); + this.addButton("Square",action2); + this.addButton("Cube",action2); + this.addButton("CubeRoot",action2); + this.addButton("|X|",action2); //主面板添加数字按键 main.setLayout(new BorderLayout());; main.add(show,"North");//数字面板显示在北方 @@ -130,7 +134,25 @@ public class Caculator extends JFrame{ else { if(fuhao.equals("+")) { result +=Double.parseDouble(show.getText()); - }else if(fuhao.equals("-")) { + } + + else if(fuhao.equals("CubeRoot")) { + result=Math.pow(Double.parseDouble(show.getText()), 1.0/3); + } + else if(fuhao.equals("|X|")) { + if(Double.parseDouble(show.getText())<0) { + result=-Double.parseDouble(show.getText()); + } + else { + result=Double.parseDouble(show.getText()); + } + } + else if(fuhao.equals("Square")) { + result =Double.parseDouble(show.getText())*Double.parseDouble(show.getText()); + }else if(fuhao.equals("Cube")){ + result =Double.parseDouble(show.getText())*Double.parseDouble(show.getText())*Double.parseDouble(show.getText()); + } + else if(fuhao.equals("-")) { result -=Double.parseDouble(show.getText()); }else if(fuhao.equals("*")) { result *=Double.parseDouble(show.getText()); @@ -140,7 +162,9 @@ public class Caculator extends JFrame{ result =Double.parseDouble(show.getText())/100; }else if(fuhao.equals("sqrt")) { result =Math.sqrt(Double.parseDouble(show.getText())); - }else if(input.equals("1/x")) { + + } + else if(input.equals("1/x")) { if(Double.parseDouble(show.getText())==0) { show.setText("0无倒数"); } -- Gitee