From 97719089391153361431ef0f8fabb3b78bb875bc Mon Sep 17 00:00:00 2001 From: Administrator Date: Sat, 14 May 2022 10:58:28 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E5=B0=9D=E8=AF=95push?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/CL.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/java2022spring/CL.java diff --git a/src/java2022spring/CL.java b/src/java2022spring/CL.java new file mode 100644 index 0000000..ab91c27 --- /dev/null +++ b/src/java2022spring/CL.java @@ -0,0 +1,11 @@ +package java2022spring; + +public class CL { + + public static void main(String[] args) { + // TODO 自动生成的方法存根 + System.out.println("make it"); + + } + +} -- Gitee From c2dc96a2a1d0913edf1f504a82e65ef5f33858b1 Mon Sep 17 00:00:00 2001 From: Administrator Date: Sun, 15 May 2022 22:27:53 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E5=B8=83=E5=B1=80=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/DEMO0302.java | 16 +++++ src/java2022spring/ONE302.java | 117 +++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/java2022spring/DEMO0302.java create mode 100644 src/java2022spring/ONE302.java diff --git a/src/java2022spring/DEMO0302.java b/src/java2022spring/DEMO0302.java new file mode 100644 index 0000000..3efc0be --- /dev/null +++ b/src/java2022spring/DEMO0302.java @@ -0,0 +1,16 @@ +package java2022spring; + +import javax.swing.JFrame; + +public class DEMO0302{ + + public static void main(String[] args) { + // TODO 自动生成的方法存根 + JFrame frame=new ONE302("计算器"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setBounds(100,100,500,700); + frame.setResizable(true); + frame.setVisible(true); + } + +} diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java new file mode 100644 index 0000000..1d7be98 --- /dev/null +++ b/src/java2022spring/ONE302.java @@ -0,0 +1,117 @@ +package java2022spring; + +import java.awt.BorderLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.*; +import javax.swing.JPanel; +import javax.swing.JSplitPane; +import javax.swing.JTextField; + +public class ONE302 extends JFrame implements ActionListener{ + + + JButton [] Numbutton; + JTextField textfield; + JMenuBar menubar; + JMenu menu1,menu2,menu3; + + public ONE302(String title){ + super(title); + + menubar=new JMenuBar(); + menu1=new JMenu("编辑"); + menu2=new JMenu("查看"); + menu3=new JMenu("帮助"); + menubar.add(menu1); + menubar.add(menu2); + menubar.add(menu3); + setJMenuBar(menubar); + + + textfield=new JTextField(10); + textfield.setHorizontalAlignment(JTextField.RIGHT); + //add(textfield); + + JButton[] Numbutton=new JButton[10]; + for(int i=0;i<=9;i++) + Numbutton[i]=new JButton(i+""); + + JButton button1=new JButton("+"); + JButton button2=new JButton("-"); + JButton button3=new JButton("*"); + JButton button4=new JButton("/"); + + JButton sqrtButton=new JButton("sqrt"); + JButton yuButton=new JButton("%"); + JButton daoButton=new JButton("1/x"); + JButton dengButton=new JButton("="); + JButton pointButton=new JButton("."); + JButton orButton=new JButton("+/-"); + JButton backButton=new JButton("Backspace"); + JButton ceButton=new JButton("CE"); + JButton cButton=new JButton("C"); + + JPanel root1 =new JPanel();////????????????????? + Box boxOne,boxTwo,boxthree; + boxOne=Box.createHorizontalBox(); + boxTwo=Box.createHorizontalBox(); + boxthree=Box.createVerticalBox(); + boxOne.add(backButton); + boxOne.add(ceButton); + boxOne.add(cButton); + + + boxTwo.add(textfield); + + boxthree.add(boxTwo); + boxthree.add(Box.createVerticalStrut(5)); + boxthree.add(boxOne); + root1.add(boxthree); + + //root1.add(textfield,BorderLayout.NORTH); + //root1.add(backButton,BorderLayout.SOUTH); + + JPanel root2=new JPanel(); + root2.setLayout(new GridLayout(4,5,5,5)); + + root2.add(Numbutton[7]); + root2.add(Numbutton[8]); + root2.add(Numbutton[9]); + root2.add(button4); + root2.add(sqrtButton); + root2.add(Numbutton[4]); + root2.add(Numbutton[5]); + root2.add(Numbutton[6]); + root2.add(button3); + root2.add(yuButton); + root2.add(Numbutton[1]); + root2.add(Numbutton[2]); + root2.add(Numbutton[3]); + root2.add(button2); + root2.add(daoButton); + root2.add(Numbutton[0]); + root2.add(orButton); + root2.add(pointButton); + root2.add(button1); + root2.add(dengButton); + + this.getContentPane().setLayout(new BorderLayout()); + this.getContentPane().add(root2,BorderLayout.SOUTH); + this.getContentPane().add(root1,BorderLayout.CENTER); + + + + + } + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + } + +} -- Gitee From 3c609a8d39999567d5e7185fffa2e9adc1a56ad2 Mon Sep 17 00:00:00 2001 From: Administrator Date: Tue, 17 May 2022 17:20:11 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E5=8F=88=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/DEMO0302.java | 2 ++ src/java2022spring/ONE302.java | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/java2022spring/DEMO0302.java b/src/java2022spring/DEMO0302.java index 3efc0be..45af9d6 100644 --- a/src/java2022spring/DEMO0302.java +++ b/src/java2022spring/DEMO0302.java @@ -11,6 +11,8 @@ public class DEMO0302{ frame.setBounds(100,100,500,700); frame.setResizable(true); frame.setVisible(true); + frame.pack(); + } } diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index 1d7be98..ce58d16 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -4,6 +4,7 @@ import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; @@ -19,10 +20,11 @@ public class ONE302 extends JFrame implements ActionListener{ JTextField textfield; JMenuBar menubar; JMenu menu1,menu2,menu3; + Vector vt; public ONE302(String title){ super(title); - + //菜单 menubar=new JMenuBar(); menu1=new JMenu("编辑"); menu2=new JMenu("查看"); @@ -32,11 +34,11 @@ public class ONE302 extends JFrame implements ActionListener{ menubar.add(menu3); setJMenuBar(menubar); - - textfield=new JTextField(10); + //文本框 + textfield=new JTextField(40); textfield.setHorizontalAlignment(JTextField.RIGHT); - //add(textfield); + //按钮 JButton[] Numbutton=new JButton[10]; for(int i=0;i<=9;i++) Numbutton[i]=new JButton(i+""); @@ -56,6 +58,15 @@ public class ONE302 extends JFrame implements ActionListener{ JButton ceButton=new JButton("CE"); JButton cButton=new JButton("C"); + JPanel root3=new JPanel(); + Box boxFor; + boxFor=Box.createVerticalBox(); + boxFor.add(new JButton("MC")); + boxFor.add(new JButton("MR")); + boxFor.add(new JButton("MS")); + boxFor.add(new JButton("M+")); + root3.add(boxFor); + JPanel root1 =new JPanel();////????????????????? Box boxOne,boxTwo,boxthree; boxOne=Box.createHorizontalBox(); @@ -65,7 +76,6 @@ public class ONE302 extends JFrame implements ActionListener{ boxOne.add(ceButton); boxOne.add(cButton); - boxTwo.add(textfield); boxthree.add(boxTwo); @@ -73,9 +83,8 @@ public class ONE302 extends JFrame implements ActionListener{ boxthree.add(boxOne); root1.add(boxthree); - //root1.add(textfield,BorderLayout.NORTH); - //root1.add(backButton,BorderLayout.SOUTH); + JPanel root2=new JPanel(); root2.setLayout(new GridLayout(4,5,5,5)); @@ -100,9 +109,11 @@ public class ONE302 extends JFrame implements ActionListener{ root2.add(button1); root2.add(dengButton); + this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(root2,BorderLayout.SOUTH); this.getContentPane().add(root1,BorderLayout.CENTER); + this.getContentPane().add(root3,BorderLayout.WEST); @@ -111,6 +122,7 @@ public class ONE302 extends JFrame implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 + vt=new Vector(20,10); } -- Gitee From 98ba6bd59e94baa2661dbb5753de40370aff794f Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 25 May 2022 21:35:56 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 111 ++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 17 deletions(-) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index ce58d16..56bd7df 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -4,6 +4,8 @@ import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.JButton; @@ -13,14 +15,37 @@ import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JTextField; -public class ONE302 extends JFrame implements ActionListener{ +public class ONE302 extends JFrame { JButton [] Numbutton; JTextField textfield; JMenuBar menubar; JMenu menu1,menu2,menu3; - Vector vt; + Vector vt=new Vector(20,10); + //程序的安全 + String str1="0"; + //操作数2 + String str2="0"; + //运算符 + String oper="+"; + //运算结果 + String result=""; + //数字标志位 + boolean isNum; + //为1时写入str1 为2时写入str2 + int k1=1; + //记录符号键的次数 K2>2 多符号运算 + int k2=1; + //str1可否被清零 为1可以 不为1不可以 + int k3=1; + //str2可否被清0 为1可以 + int k4=1; + //小数点可否被清零 等于1可以 + int k5=1; + //寄存器 + JButton store; + public ONE302(String title){ super(title); @@ -42,11 +67,11 @@ public class ONE302 extends JFrame implements ActionListener{ JButton[] Numbutton=new JButton[10]; for(int i=0;i<=9;i++) Numbutton[i]=new JButton(i+""); - - JButton button1=new JButton("+"); - JButton button2=new JButton("-"); - JButton button3=new JButton("*"); - JButton button4=new JButton("/"); + JButton[] btoper=new JButton[4]; + btoper[0]=new JButton("+"); + btoper[1]=new JButton("-"); + btoper[2]=new JButton("*"); + btoper[3]=new JButton("/"); JButton sqrtButton=new JButton("sqrt"); JButton yuButton=new JButton("%"); @@ -91,39 +116,91 @@ public class ONE302 extends JFrame implements ActionListener{ root2.add(Numbutton[7]); root2.add(Numbutton[8]); root2.add(Numbutton[9]); - root2.add(button4); + root2.add(btoper[3]); root2.add(sqrtButton); root2.add(Numbutton[4]); root2.add(Numbutton[5]); root2.add(Numbutton[6]); - root2.add(button3); + root2.add(btoper[2]); root2.add(yuButton); root2.add(Numbutton[1]); root2.add(Numbutton[2]); root2.add(Numbutton[3]); - root2.add(button2); + root2.add(btoper[1]); root2.add(daoButton); root2.add(Numbutton[0]); root2.add(orButton); root2.add(pointButton); - root2.add(button1); + root2.add(btoper[0]); root2.add(dengButton); + this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(root2,BorderLayout.SOUTH); this.getContentPane().add(root1,BorderLayout.CENTER); this.getContentPane().add(root3,BorderLayout.WEST); + this.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + + for(int i=0;i<=9;i++) + Numbutton[i].addActionListener(new Listener_num()); + + for(int i=0;i<=3;i++) + btoper[i].addActionListener(new Listener_oper()); + + } - @Override - public void actionPerformed(ActionEvent e) { - // TODO 自动生成的方法存根 - vt=new Vector(20,10); - + private class Listener_num implements ActionListener { + + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + String ss=((JButton)e.getSource()).getText(); + store=(JButton)e.getSource(); + vt.add(store); + if(k1==1) { + if(k3==1) { + str1=""; + k5=1; + } + str1=str1+ss; + k3=k3+1; + textfield.setText(str1); + } + else if(k1==2) { + if(k4==1) { + str2=""; + k5=1; + + } + str2=str2+ss; + k4=k4+1; + textfield.setText(str2); + } + } + + } + private class Listener_oper implements ActionListener { -} + @Override + public void actionPerformed(ActionEvent e) { + + + } + + } + + + + + + } -- Gitee From 50a8b8942eb6b5a47ae4bf2c99779eac7c16d0a2 Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 27 May 2022 19:44:32 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E6=96=B9=E6=B3=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 141 ++++++++++++++++++++++++++++++++- 1 file changed, 138 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index 56bd7df..f491251 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -6,6 +6,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.math.BigDecimal; import java.util.Vector; import javax.swing.JButton; @@ -74,7 +75,9 @@ public class ONE302 extends JFrame { btoper[3]=new JButton("/"); JButton sqrtButton=new JButton("sqrt"); + sqrtButton.addActionListener(new Listener_oper()); JButton yuButton=new JButton("%"); + yuButton.addActionListener(new Listener_oper()); JButton daoButton=new JButton("1/x"); JButton dengButton=new JButton("="); JButton pointButton=new JButton("."); @@ -157,6 +160,109 @@ public class ONE302 extends JFrame { + } + public void cal() + + { + + double a2; + + double b2; + + String c = oper; + + double result2 = 0; + + if (c.equals("")) { + + textfield.setText("Please input operator"); + + } else { + + if (str1.equals(".")) + + str1 = "0.0"; + + if (str2.equals(".")) + + str2 = "0.0"; + + a2 = Double.valueOf(str1).doubleValue(); + + b2 = Double.valueOf(str2).doubleValue(); + + if (c.equals("+")) { + + result2 = a2 + b2; + + } + + if (c.equals("-")) { + + result2 = a2 - b2; + + } + + if (c.equals("*")) { + + BigDecimal m1 = new BigDecimal(Double.toString(a2)); + BigDecimal m2 = new BigDecimal(Double.toString(b2)); + result2 = m1.multiply(m2).doubleValue(); + + } + + if (c.equals("/")) { + + if (b2 == 0) { + + result2 = 0; + + } else { + + result2 = a2 / b2; + + } + + } + + if (c.equals("Sqrt")) { + + result2 = Math.sqrt(a2); + + } + + if (c.equals("^2")) { + + result2 = a2*a2; + + } + + if (c.equals("^3")) { + + result2 = a2*a2*a2; + + } + + if (c.equals("%")) { + + if (b2 == 0) { + + result2 = 0; + + } else { + + result2 = a2 % b2; + + } + + } + + result = ((new Double(result2)).toString()); + + textfield.setText(result); + + } + } private class Listener_num implements ActionListener { @@ -193,9 +299,38 @@ public class ONE302 extends JFrame { @Override public void actionPerformed(ActionEvent e) { + String ss2=((JButton)e.getSource()).getText(); + store =(JButton)e.getSource(); + vt.add(store); + if(k2==1) { + k1 = 2; + k5 = 1; + oper = ss2; + k2 = k2 + 1; + } + else { + int a = vt.size(); + JButton c = (JButton) vt.get(a - 2); + if (!(c.getText().equals("+")) + && !(c.getText().equals("-")) + && !(c.getText().equals("*")) + && !(c.getText().equals("/")) + && !(c.getText().equals("Sqrt"))&& !(c.getText().equals("^2")) + && !(c.getText().equals("^3"))&& !(c.getText().equals("%"))) + { + cal(); + str1 = result; + k1 = 2; + k5 = 1; + k4 = 1; + oper = ss2; + } + k2 = k2 + 1; + } + } + } - - } + } @@ -203,4 +338,4 @@ public class ONE302 extends JFrame { - } + -- Gitee From 5badbf078f18ed796b06c5b37bfad1cf82e47b97 Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 27 May 2022 19:44:45 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=96=B9=E6=B3=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index f491251..baf33aa 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -312,11 +312,11 @@ public class ONE302 extends JFrame { int a = vt.size(); JButton c = (JButton) vt.get(a - 2); if (!(c.getText().equals("+")) - && !(c.getText().equals("-")) - && !(c.getText().equals("*")) - && !(c.getText().equals("/")) - && !(c.getText().equals("Sqrt"))&& !(c.getText().equals("^2")) - && !(c.getText().equals("^3"))&& !(c.getText().equals("%"))) + || !(c.getText().equals("-")) + || !(c.getText().equals("*")) + || !(c.getText().equals("/")) + || !(c.getText().equals("Sqrt"))|| !(c.getText().equals("^2")) + || !(c.getText().equals("^3"))|| !(c.getText().equals("%")))//???&&?? { cal(); str1 = result; -- Gitee From 5afb9c0fcce191cfb593804f2e8e7baa717aa92e Mon Sep 17 00:00:00 2001 From: Administrator Date: Sun, 29 May 2022 21:34:34 +0800 Subject: [PATCH 07/13] 3 --- src/java2022spring/DEMO0302.java | 4 +- src/java2022spring/ONE302.java | 430 +++++++++++++++++++++---------- 2 files changed, 290 insertions(+), 144 deletions(-) diff --git a/src/java2022spring/DEMO0302.java b/src/java2022spring/DEMO0302.java index 45af9d6..9b069ed 100644 --- a/src/java2022spring/DEMO0302.java +++ b/src/java2022spring/DEMO0302.java @@ -8,8 +8,8 @@ public class DEMO0302{ // TODO 自动生成的方法存根 JFrame frame=new ONE302("计算器"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setBounds(100,100,500,700); - frame.setResizable(true); + frame.setBounds(100,100,700,700); + frame.setResizable(false); frame.setVisible(true); frame.pack(); diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index baf33aa..bc1fec6 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -1,6 +1,8 @@ package java2022spring; import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -79,42 +81,52 @@ public class ONE302 extends JFrame { JButton yuButton=new JButton("%"); yuButton.addActionListener(new Listener_oper()); JButton daoButton=new JButton("1/x"); + daoButton.addActionListener(new Listener_oper()); JButton dengButton=new JButton("="); + dengButton.addActionListener(new Listener_equal()); JButton pointButton=new JButton("."); + pointButton.addActionListener(new Listener_point()); JButton orButton=new JButton("+/-"); - JButton backButton=new JButton("Backspace"); - JButton ceButton=new JButton("CE"); - JButton cButton=new JButton("C"); + JButton backButton=new JButton("C"); + backButton.addActionListener(new Listener_backspace()); + JButton pfButton=new JButton("^2"); + JButton scfButton=new JButton("^3"); - JPanel root3=new JPanel(); - Box boxFor; - boxFor=Box.createVerticalBox(); - boxFor.add(new JButton("MC")); - boxFor.add(new JButton("MR")); - boxFor.add(new JButton("MS")); - boxFor.add(new JButton("M+")); - root3.add(boxFor); + JButton ceButton=new JButton("CE"); + JButton cButton=new JButton("Backspace"); - JPanel root1 =new JPanel();////????????????????? - Box boxOne,boxTwo,boxthree; - boxOne=Box.createHorizontalBox(); - boxTwo=Box.createHorizontalBox(); - boxthree=Box.createVerticalBox(); - boxOne.add(backButton); - boxOne.add(ceButton); - boxOne.add(cButton); + //JPanel root3=new JPanel(); +// Box boxFor; +// boxFor=Box.createVerticalBox(); +// boxFor.add(new JButton("MC")); +// boxFor.add(new JButton("MR")); +// boxFor.add(new JButton("MS")); +// boxFor.add(new JButton("M+")); +// root3.add(boxFor); - boxTwo.add(textfield); + //JPanel root1 =new JPanel();////????????????????? +// Box boxOne,boxTwo,boxthree; +// boxOne=Box.createHorizontalBox(); +// boxTwo=Box.createHorizontalBox(); +// boxthree=Box.createVerticalBox(); +// boxOne.add(backButton); +// boxOne.add(ceButton); +// boxOne.add(cButton); +// +// boxTwo.add(textfield); +// +// boxthree.add(boxTwo); +// boxthree.add(Box.createVerticalStrut(5)); +// boxthree.add(boxOne); +// root1.add(boxthree); - boxthree.add(boxTwo); - boxthree.add(Box.createVerticalStrut(5)); - boxthree.add(boxOne); - root1.add(boxthree); + JPanel root1=new JPanel(); + root1.add(textfield); + root1.add(backButton); - JPanel root2=new JPanel(); - root2.setLayout(new GridLayout(4,5,5,5)); + root2.setLayout(new GridLayout(5,5,5,5)); root2.add(Numbutton[7]); root2.add(Numbutton[8]); @@ -135,14 +147,21 @@ public class ONE302 extends JFrame { root2.add(orButton); root2.add(pointButton); root2.add(btoper[0]); + root2.add(new JButton("tanx")); + root2.add(pfButton); + pfButton.addActionListener(new Listener_oper()); + root2.add(scfButton); + scfButton.addActionListener(new Listener_oper()); + root2.add(new JButton("sinx")); + root2.add(new JButton("cosx")); root2.add(dengButton); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(root2,BorderLayout.SOUTH); - this.getContentPane().add(root1,BorderLayout.CENTER); - this.getContentPane().add(root3,BorderLayout.WEST); + this.getContentPane().add(root1,BorderLayout.NORTH); + //this.getContentPane().add(root3,BorderLayout.WEST); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { @@ -153,182 +172,309 @@ public class ONE302 extends JFrame { for(int i=0;i<=9;i++) Numbutton[i].addActionListener(new Listener_num()); - for(int i=0;i<=3;i++) + for(int i=0;i<=3;i++) { btoper[i].addActionListener(new Listener_oper()); + btoper[i].setForeground(Color.RED); } - - - + } + + + private class Listener_num implements ActionListener { + + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + String ss=((JButton)e.getSource()).getText(); + store=(JButton)e.getSource(); + vt.add(store); + + if(k1==1) {//写入str1 + if(k3==1) {//str1可以被清零 + str1=""; + k5=1;//小数点被清零 + } + str1=str1+ss; + k3=k3+1;//下次处理str1不可以被清零 + textfield.setText(str1); + } + + else if(k1==2) {//写入str2 + if(k4==1) {//str2可以清零 + str2=""; + k5=1;//小数点清零 + + } + str2=str2+ss; + k4=k4+1;//下次处理str2不可以被清零 + textfield.setText(str2); + } + } + } + + private class Listener_oper implements ActionListener { + + @Override + public void actionPerformed(ActionEvent e) { + String ss2=((JButton)e.getSource()).getText(); + store =(JButton)e.getSource(); + vt.add(store); + if(k2==1) {//单符号运算 K3=2 + k1 = 2;//写入str2 + k5 = 1; + oper = ss2; + k2 = k2 + 1;//k2=2 再有就多符号运算 + } + else {//多符号运算 + int a = vt.size(); + JButton c = (JButton) vt.get(a - 2);//?????? + if (!(c.getText().equals("+")) + || !(c.getText().equals("-")) + || !(c.getText().equals("*")) + || !(c.getText().equals("/")) + || !(c.getText().equals("Sqrt"))|| !(c.getText().equals("^2")) + || !(c.getText().equals("^3"))|| !(c.getText().equals("%"))|| !(c.getText().equals("1/x")))//???&&?? + { + cal(); + str1 = result; + k1 = 2;//写入str2 + k5 = 1; + k4 = 1; + oper = ss2; + } + k2 = k2 + 1; + } + } + } + + //Listener_equal类中编写的是等于号键的响应逻辑 + + class Listener_equal implements ActionListener { + + public void actionPerformed(ActionEvent e) { + + store = (JButton) e.getSource(); + + vt.add(store); + + cal(); + + k1 = 1; + + k2 = 1; + + k3 = 1; + + k4 = 1; + + str1 = result; + + } + + } + public void cal() - { + { - double a2; + double a2; - double b2; + double b2; - String c = oper; + String c = oper; - double result2 = 0; + double result2 = 0; - if (c.equals("")) { + if (c.equals("")) { - textfield.setText("Please input operator"); + textfield.setText("Please input operator"); - } else { + } + else { - if (str1.equals(".")) + if (str1.equals(".")) - str1 = "0.0"; + str1 = "0.0"; - if (str2.equals(".")) + if (str2.equals(".")) - str2 = "0.0"; + str2 = "0.0"; - a2 = Double.valueOf(str1).doubleValue(); + a2 = Double.valueOf(str1).doubleValue(); + + b2 = Double.valueOf(str2).doubleValue(); + + if (c.equals("+")) { + + result2 = a2 + b2; + + } + + if (c.equals("-")) { + + result2 = a2 - b2; + + } - b2 = Double.valueOf(str2).doubleValue(); + if (c.equals("*")) { + + BigDecimal m1 = new BigDecimal(Double.toString(a2)); + BigDecimal m2 = new BigDecimal(Double.toString(b2)); + result2 = m1.multiply(m2).doubleValue(); + + } + + if (c.equals("/")) { + + if (b2 == 0) { + + result2 = 0; + + } else { + + result2 = a2 / b2; + + } + + } + + if (c.equals("sqrt")) { + + result2 = Math.sqrt(a2); + + } + + if (c.equals("^2")) { + + result2 = a2*a2; + + } + + if (c.equals("^3")) { + + result2 = a2*a2*a2; + + } + if(c.equals("1/x")) + result2=1/a2; + + if (c.equals("%")) { + + if (b2 == 0) { + + result2 = 0; + + } else { + + result2 = a2 % b2; + + } - if (c.equals("+")) { + } - result2 = a2 + b2; + result = ((new Double(result2)).toString()); - } + textfield.setText(result); - if (c.equals("-")) { + } - result2 = a2 - b2; + } + //Listener_point类中编写的是小数点键的相应逻辑 - } + class Listener_point implements ActionListener { - if (c.equals("*")) { + public void actionPerformed(ActionEvent e) { - BigDecimal m1 = new BigDecimal(Double.toString(a2)); - BigDecimal m2 = new BigDecimal(Double.toString(b2)); - result2 = m1.multiply(m2).doubleValue(); + store = (JButton) e.getSource(); - } + vt.add(store); - if (c.equals("/")) { + if (k5 == 1) {//小数点被清零 - if (b2 == 0) { + String ss2=((JButton)e.getSource()).getText(); - result2 = 0; + if (k1 == 1) {//写入str1 - } else { + if (k3 == 1) {//str1可以被清零 - result2 = a2 / b2; + str1 = ""; - } + k5 = 1; - } + } - if (c.equals("Sqrt")) { + str1 = str1 + ss2; + + k3 = k3 + 1; + + textfield.setText(str1); - result2 = Math.sqrt(a2); + } + else if (k1 == 2) {//写入str2 - } + if (k4 == 1) {//可以清零 + + str2 = ""; + + k5 = 1; + + } - if (c.equals("^2")) { + str2 = str2 + ss2; + + k4 = k4 + 1; + + textfield.setText(str2); - result2 = a2*a2; + } } - if (c.equals("^3")) { + k5 = k5 + 1;//若不可以清零,k5加一 - result2 = a2*a2*a2; + } } + //Listener_backspace类中编写了清除键的响应逻辑 - if (c.equals("%")) { + private class Listener_backspace implements ActionListener { - if (b2 == 0) { + public void actionPerformed(ActionEvent e) { - result2 = 0; + store = (JButton) e.getSource(); - } else { + vt.add(store); + //全部初始化 + k5 = 1; - result2 = a2 % b2; + k2 = 1; - } + k1 = 1; - } + k3 = 1; + + k4 = 1; + + str1 = "0"; + + str2 = "0"; - result = ((new Double(result2)).toString()); + oper = ""; + + result = ""; textfield.setText(result); - } + vt.clear(); } - private class Listener_num implements ActionListener { - - @Override - public void actionPerformed(ActionEvent e) { - // TODO 自动生成的方法存根 - String ss=((JButton)e.getSource()).getText(); - store=(JButton)e.getSource(); - vt.add(store); - if(k1==1) { - if(k3==1) { - str1=""; - k5=1; - } - str1=str1+ss; - k3=k3+1; - textfield.setText(str1); - } - else if(k1==2) { - if(k4==1) { - str2=""; - k5=1; - - } - str2=str2+ss; - k4=k4+1; - textfield.setText(str2); - } - } - } - private class Listener_oper implements ActionListener { + - @Override - public void actionPerformed(ActionEvent e) { - String ss2=((JButton)e.getSource()).getText(); - store =(JButton)e.getSource(); - vt.add(store); - if(k2==1) { - k1 = 2; - k5 = 1; - oper = ss2; - k2 = k2 + 1; - } - else { - int a = vt.size(); - JButton c = (JButton) vt.get(a - 2); - if (!(c.getText().equals("+")) - || !(c.getText().equals("-")) - || !(c.getText().equals("*")) - || !(c.getText().equals("/")) - || !(c.getText().equals("Sqrt"))|| !(c.getText().equals("^2")) - || !(c.getText().equals("^3"))|| !(c.getText().equals("%")))//???&&?? - { - cal(); - str1 = result; - k1 = 2; - k5 = 1; - k4 = 1; - oper = ss2; - } - k2 = k2 + 1; - } - } - } + + -- Gitee From 3e7214f78bc5e5445d720e3564e5f1ac41206f60 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 30 May 2022 16:22:35 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E5=B7=AE=E4=B8=8D=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/DEMO0302.java | 2 +- src/java2022spring/ONE302.java | 37 +++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/java2022spring/DEMO0302.java b/src/java2022spring/DEMO0302.java index 9b069ed..a411a79 100644 --- a/src/java2022spring/DEMO0302.java +++ b/src/java2022spring/DEMO0302.java @@ -8,7 +8,7 @@ public class DEMO0302{ // TODO 自动生成的方法存根 JFrame frame=new ONE302("计算器"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setBounds(100,100,700,700); + frame.setBounds(100,100,700,500); frame.setResizable(false); frame.setVisible(true); frame.pack(); diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index bc1fec6..1e46bb9 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -72,7 +72,7 @@ public class ONE302 extends JFrame { Numbutton[i]=new JButton(i+""); JButton[] btoper=new JButton[4]; btoper[0]=new JButton("+"); - btoper[1]=new JButton("-"); + btoper[1]=new JButton("——"); btoper[2]=new JButton("*"); btoper[3]=new JButton("/"); @@ -86,12 +86,19 @@ public class ONE302 extends JFrame { dengButton.addActionListener(new Listener_equal()); JButton pointButton=new JButton("."); pointButton.addActionListener(new Listener_point()); - JButton orButton=new JButton("+/-"); + JButton fuButton=new JButton("-"); + fuButton.addActionListener(new Listener_num()); + JButton backButton=new JButton("C"); backButton.addActionListener(new Listener_backspace()); JButton pfButton=new JButton("^2"); JButton scfButton=new JButton("^3"); - + JButton sinButton=new JButton("sinx"); + sinButton.addActionListener(new Listener_oper()); + JButton cosButton=new JButton("cosx"); + cosButton.addActionListener(new Listener_oper()); + JButton tanButton=new JButton("tanx"); + tanButton.addActionListener(new Listener_oper()); JButton ceButton=new JButton("CE"); JButton cButton=new JButton("Backspace"); @@ -144,16 +151,17 @@ public class ONE302 extends JFrame { root2.add(btoper[1]); root2.add(daoButton); root2.add(Numbutton[0]); - root2.add(orButton); + root2.add(fuButton); root2.add(pointButton); root2.add(btoper[0]); - root2.add(new JButton("tanx")); + root2.add(tanButton); root2.add(pfButton); pfButton.addActionListener(new Listener_oper()); root2.add(scfButton); scfButton.addActionListener(new Listener_oper()); - root2.add(new JButton("sinx")); - root2.add(new JButton("cosx")); + root2.add(sinButton); + + root2.add(cosButton); root2.add(dengButton); @@ -234,7 +242,8 @@ public class ONE302 extends JFrame { || !(c.getText().equals("*")) || !(c.getText().equals("/")) || !(c.getText().equals("Sqrt"))|| !(c.getText().equals("^2")) - || !(c.getText().equals("^3"))|| !(c.getText().equals("%"))|| !(c.getText().equals("1/x")))//???&&?? + || !(c.getText().equals("^3"))|| !(c.getText().equals("%"))|| !(c.getText().equals("1/x")) + || !(c.getText().equals("sinx"))|| !(c.getText().equals("cosx"))|| !(c.getText().equals("tanx")))//???&&?? { cal(); str1 = result; @@ -372,6 +381,18 @@ public class ONE302 extends JFrame { } } + if(c.equals("sinx")) { + double b=Math.toRadians(a2); + result2=Math.sin(b); + + } + if(c.equals("cosx")) { + double b=Math.toRadians(a2); + result2=Math.cos(b);} + + if(c.equals("tanx")) { + double b=Math.toRadians(a2); + result2=Math.tan(b);} result = ((new Double(result2)).toString()); -- Gitee From fe9d3b2d9dd800c14f753be64ee6af56be3cd441 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 30 May 2022 16:44:49 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index 1e46bb9..2e524a6 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -101,6 +101,7 @@ public class ONE302 extends JFrame { tanButton.addActionListener(new Listener_oper()); JButton ceButton=new JButton("CE"); JButton cButton=new JButton("Backspace"); + cButton.addActionListener(new Listener_C()); //JPanel root3=new JPanel(); // Box boxFor; @@ -131,6 +132,7 @@ public class ONE302 extends JFrame { JPanel root1=new JPanel(); root1.add(textfield); root1.add(backButton); + root1.add(cButton); JPanel root2=new JPanel(); root2.setLayout(new GridLayout(5,5,5,5)); @@ -492,6 +494,38 @@ public class ONE302 extends JFrame { } } + private class Listener_C implements ActionListener{ + + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + String DisplayString=textfield.getText(); + DisplayString=DisplayString.substring(0,(DisplayString.length()-1)); + textfield.setText(DisplayString); + if(k1==1) {//写入str1 + if(k3==1) {//str1可以被清零 + str1=""; + k5=1;//小数点被清零 + } + str1=DisplayString; + k3=k3+1;//下次处理str1不可以被清零 + textfield.setText(str1); + } + + else if(k1==2) {//写入str2 + if(k4==1) {//str2可以清零 + str2=""; + k5=1;//小数点清零 + + } + str2=DisplayString; + k4=k4+1;//下次处理str2不可以被清零 + textfield.setText(str2); + } + + } + + } -- Gitee From c463bca15e3c4b5975d3c498d99a80c0f700b12a Mon Sep 17 00:00:00 2001 From: Administrator Date: Sat, 4 Jun 2022 23:40:46 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E5=9F=BA=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 209 +++++++-------------------------- 1 file changed, 40 insertions(+), 169 deletions(-) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index 2e524a6..1009681 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -25,6 +25,7 @@ public class ONE302 extends JFrame { JTextField textfield; JMenuBar menubar; JMenu menu1,menu2,menu3; + //创建指定大小的向量 Vector vt=new Vector(20,10); //程序的安全 String str1="0"; @@ -52,7 +53,7 @@ public class ONE302 extends JFrame { public ONE302(String title){ super(title); - //菜单 + //菜单(未实现) menubar=new JMenuBar(); menu1=new JMenu("编辑"); menu2=new JMenu("查看"); @@ -69,7 +70,8 @@ public class ONE302 extends JFrame { //按钮 JButton[] Numbutton=new JButton[10]; for(int i=0;i<=9;i++) - Numbutton[i]=new JButton(i+""); + {Numbutton[i]=new JButton(i+""); + } JButton[] btoper=new JButton[4]; btoper[0]=new JButton("+"); btoper[1]=new JButton("——"); @@ -90,53 +92,29 @@ public class ONE302 extends JFrame { fuButton.addActionListener(new Listener_num()); JButton backButton=new JButton("C"); - backButton.addActionListener(new Listener_backspace()); + backButton.addActionListener(new Listener_C()); JButton pfButton=new JButton("^2"); + pfButton.addActionListener(new Listener_oper()); JButton scfButton=new JButton("^3"); + scfButton.addActionListener(new Listener_oper()); JButton sinButton=new JButton("sinx"); sinButton.addActionListener(new Listener_oper()); JButton cosButton=new JButton("cosx"); cosButton.addActionListener(new Listener_oper()); JButton tanButton=new JButton("tanx"); tanButton.addActionListener(new Listener_oper()); - JButton ceButton=new JButton("CE"); JButton cButton=new JButton("Backspace"); - cButton.addActionListener(new Listener_C()); - - //JPanel root3=new JPanel(); -// Box boxFor; -// boxFor=Box.createVerticalBox(); -// boxFor.add(new JButton("MC")); -// boxFor.add(new JButton("MR")); -// boxFor.add(new JButton("MS")); -// boxFor.add(new JButton("M+")); -// root3.add(boxFor); - - //JPanel root1 =new JPanel();////????????????????? -// Box boxOne,boxTwo,boxthree; -// boxOne=Box.createHorizontalBox(); -// boxTwo=Box.createHorizontalBox(); -// boxthree=Box.createVerticalBox(); -// boxOne.add(backButton); -// boxOne.add(ceButton); -// boxOne.add(cButton); -// -// boxTwo.add(textfield); -// -// boxthree.add(boxTwo); -// boxthree.add(Box.createVerticalStrut(5)); -// boxthree.add(boxOne); -// root1.add(boxthree); - + cButton.addActionListener(new Listener_backspace()); + + //面板1 JPanel root1=new JPanel(); root1.add(textfield); root1.add(backButton); root1.add(cButton); - + //面板2 网格布局 JPanel root2=new JPanel(); root2.setLayout(new GridLayout(5,5,5,5)); - root2.add(Numbutton[7]); root2.add(Numbutton[8]); root2.add(Numbutton[9]); @@ -158,43 +136,39 @@ public class ONE302 extends JFrame { root2.add(btoper[0]); root2.add(tanButton); root2.add(pfButton); - pfButton.addActionListener(new Listener_oper()); root2.add(scfButton); - scfButton.addActionListener(new Listener_oper()); root2.add(sinButton); - root2.add(cosButton); root2.add(dengButton); - - - + //设置BorderLayOut布局 this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(root2,BorderLayout.SOUTH); this.getContentPane().add(root1,BorderLayout.NORTH); - //this.getContentPane().add(root3,BorderLayout.WEST); - + + //窗口关闭事件 this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); - + //数字按钮添加监听器 for(int i=0;i<=9;i++) Numbutton[i].addActionListener(new Listener_num()); - + //四则运算按钮设置文本颜色 for(int i=0;i<=3;i++) { btoper[i].addActionListener(new Listener_oper()); btoper[i].setForeground(Color.RED); } - } - + //数字按钮的监听器Listener_num. private class Listener_num implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 + //按钮一旦被点击,上面的文本内容将会被计入ss字符串中 String ss=((JButton)e.getSource()).getText(); + //将被触发的按钮添加到向量的末尾 store=(JButton)e.getSource(); vt.add(store); @@ -205,9 +179,9 @@ public class ONE302 extends JFrame { } str1=str1+ss; k3=k3+1;//下次处理str1不可以被清零 - textfield.setText(str1); + textfield.setText(str1);//文本框显示出来 } - + //双目运算 else if(k1==2) {//写入str2 if(k4==1) {//str2可以清零 str2=""; @@ -216,17 +190,16 @@ public class ONE302 extends JFrame { } str2=str2+ss; k4=k4+1;//下次处理str2不可以被清零 - textfield.setText(str2); + textfield.setText(str2);//文本框显示 } } - - } - + //运算符监听器Listener_oper private class Listener_oper implements ActionListener { @Override public void actionPerformed(ActionEvent e) { + //运算符写入ss2 String ss2=((JButton)e.getSource()).getText(); store =(JButton)e.getSource(); vt.add(store); @@ -237,10 +210,10 @@ public class ONE302 extends JFrame { k2 = k2 + 1;//k2=2 再有就多符号运算 } else {//多符号运算 - int a = vt.size(); - JButton c = (JButton) vt.get(a - 2);//?????? + int a = vt.size();//向量的元素个数 + JButton c = (JButton) vt.get(a - 2);//返回向量中指定的位置的元素(按钮) if (!(c.getText().equals("+")) - || !(c.getText().equals("-")) + || !(c.getText().equals("——")) || !(c.getText().equals("*")) || !(c.getText().equals("/")) || !(c.getText().equals("Sqrt"))|| !(c.getText().equals("^2")) @@ -262,231 +235,135 @@ public class ONE302 extends JFrame { //Listener_equal类中编写的是等于号键的响应逻辑 class Listener_equal implements ActionListener { - public void actionPerformed(ActionEvent e) { - store = (JButton) e.getSource(); - vt.add(store); - cal(); - + //清零 k1 = 1; - k2 = 1; - k3 = 1; - k4 = 1; - str1 = result; - - } - } - + } + //底层的方法 public void cal() - { - double a2; - double b2; - String c = oper; - double result2 = 0; - + //错误处理机制 if (c.equals("")) { - textfield.setText("Please input operator"); - } else { - if (str1.equals(".")) - str1 = "0.0"; - if (str2.equals(".")) - str2 = "0.0"; - + //str转为double类型的数 a2 = Double.valueOf(str1).doubleValue(); - b2 = Double.valueOf(str2).doubleValue(); - if (c.equals("+")) { - result2 = a2 + b2; - } - - if (c.equals("-")) { - + if (c.equals("——")) { result2 = a2 - b2; - } - if (c.equals("*")) { - + //大数值对象 BigDecimal m1 = new BigDecimal(Double.toString(a2)); BigDecimal m2 = new BigDecimal(Double.toString(b2)); result2 = m1.multiply(m2).doubleValue(); - } - if (c.equals("/")) { - if (b2 == 0) { - result2 = 0; - } else { - result2 = a2 / b2; - } - } - if (c.equals("sqrt")) { - result2 = Math.sqrt(a2); - } - if (c.equals("^2")) { - result2 = a2*a2; - } - if (c.equals("^3")) { - result2 = a2*a2*a2; - } if(c.equals("1/x")) result2=1/a2; - if (c.equals("%")) { - if (b2 == 0) { - result2 = 0; - } else { - result2 = a2 % b2; - } - } if(c.equals("sinx")) { double b=Math.toRadians(a2); - result2=Math.sin(b); - + result2=Math.sin(b); } if(c.equals("cosx")) { double b=Math.toRadians(a2); result2=Math.cos(b);} - if(c.equals("tanx")) { double b=Math.toRadians(a2); result2=Math.tan(b);} - - result = ((new Double(result2)).toString()); - + result = (new Double(result2).toString()); textfield.setText(result); - } - } //Listener_point类中编写的是小数点键的相应逻辑 - class Listener_point implements ActionListener { - public void actionPerformed(ActionEvent e) { - store = (JButton) e.getSource(); - vt.add(store); - if (k5 == 1) {//小数点被清零 - String ss2=((JButton)e.getSource()).getText(); - if (k1 == 1) {//写入str1 - if (k3 == 1) {//str1可以被清零 - str1 = ""; - k5 = 1; - } - str1 = str1 + ss2; - k3 = k3 + 1; - textfield.setText(str1); - } else if (k1 == 2) {//写入str2 - if (k4 == 1) {//可以清零 - str2 = ""; - k5 = 1; - } - str2 = str2 + ss2; - k4 = k4 + 1; - textfield.setText(str2); - } - } - k5 = k5 + 1;//若不可以清零,k5加一 } } - //Listener_backspace类中编写了清除键的响应逻辑 - - private class Listener_backspace implements ActionListener { + //Listener_C类中编写了清除键的响应逻辑 + private class Listener_C implements ActionListener { public void actionPerformed(ActionEvent e) { - store = (JButton) e.getSource(); - vt.add(store); //全部初始化 k5 = 1; - k2 = 1; - k1 = 1; - k3 = 1; - k4 = 1; - str1 = "0"; - str2 = "0"; - oper = ""; - result = ""; - textfield.setText(result); vt.clear(); @@ -494,11 +371,11 @@ public class ONE302 extends JFrame { } } - private class Listener_C implements ActionListener{ - + private class Listener_backspace implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 + //显示的字符减1 String DisplayString=textfield.getText(); DisplayString=DisplayString.substring(0,(DisplayString.length()-1)); textfield.setText(DisplayString); @@ -527,12 +404,6 @@ public class ONE302 extends JFrame { } - - - - - - } -- Gitee From e7fbbcbf2df8cc8b40ba07c484a30a10a71977aa Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 8 Jun 2022 22:03:34 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 129 ++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 20 deletions(-) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index 1009681..d6efa7b 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -74,7 +74,7 @@ public class ONE302 extends JFrame { } JButton[] btoper=new JButton[4]; btoper[0]=new JButton("+"); - btoper[1]=new JButton("——"); + btoper[1]=new JButton("—"); btoper[2]=new JButton("*"); btoper[3]=new JButton("/"); @@ -103,6 +103,16 @@ public class ONE302 extends JFrame { cosButton.addActionListener(new Listener_oper()); JButton tanButton=new JButton("tanx"); tanButton.addActionListener(new Listener_oper()); + JButton logButton=new JButton("log"); + logButton.addActionListener(new Listener_oper()); + JButton nButton=new JButton("n!"); + nButton.addActionListener(new Listener_oper()); + JButton xyButton=new JButton("x^y"); + xyButton.addActionListener(new Listener_oper()); + JButton lnButton=new JButton("ln"); + lnButton.addActionListener(new Listener_oper()); + JButton pButton=new JButton(".."); + pButton.addActionListener(new Listener_oper()); JButton cButton=new JButton("Backspace"); cButton.addActionListener(new Listener_backspace()); @@ -114,7 +124,7 @@ public class ONE302 extends JFrame { root1.add(cButton); //面板2 网格布局 JPanel root2=new JPanel(); - root2.setLayout(new GridLayout(5,5,5,5)); + root2.setLayout(new GridLayout(6,5,5,5)); root2.add(Numbutton[7]); root2.add(Numbutton[8]); root2.add(Numbutton[9]); @@ -139,6 +149,13 @@ public class ONE302 extends JFrame { root2.add(scfButton); root2.add(sinButton); root2.add(cosButton); + root2.add(logButton); + root2.add(nButton); + root2.add(xyButton); + root2.add(lnButton); + root2.add(pButton); + + root2.add(dengButton); //设置BorderLayOut布局 this.getContentPane().setLayout(new BorderLayout()); @@ -213,12 +230,13 @@ public class ONE302 extends JFrame { int a = vt.size();//向量的元素个数 JButton c = (JButton) vt.get(a - 2);//返回向量中指定的位置的元素(按钮) if (!(c.getText().equals("+")) - || !(c.getText().equals("——")) - || !(c.getText().equals("*")) - || !(c.getText().equals("/")) - || !(c.getText().equals("Sqrt"))|| !(c.getText().equals("^2")) - || !(c.getText().equals("^3"))|| !(c.getText().equals("%"))|| !(c.getText().equals("1/x")) - || !(c.getText().equals("sinx"))|| !(c.getText().equals("cosx"))|| !(c.getText().equals("tanx")))//???&&?? + || !(c.getText().equals("—")) + || !(c.getText().equals("*")) + || !(c.getText().equals("/")) + || !(c.getText().equals("Sqrt"))|| !(c.getText().equals("^2")) + || !(c.getText().equals("^3"))|| !(c.getText().equals("%"))|| !(c.getText().equals("1/x")) + || !(c.getText().equals("sinx"))|| !(c.getText().equals("x^y")) + || !(c.getText().equals("ln"))|| !(c.getText().equals("cosx"))|| !(c.getText().equals("n!"))|| !(c.getText().equals("log"))|| !(c.getText().equals("tanx"))) { cal(); str1 = result; @@ -268,55 +286,124 @@ public class ONE302 extends JFrame { b2 = Double.valueOf(str2).doubleValue(); if (c.equals("+")) { result2 = a2 + b2; + result = (new Double(result2).toString()); + textfield.setText(result); } - if (c.equals("——")) { + if (c.equals("—")) { result2 = a2 - b2; + result = (new Double(result2).toString()); + textfield.setText(result); } if (c.equals("*")) { //大数值对象 BigDecimal m1 = new BigDecimal(Double.toString(a2)); BigDecimal m2 = new BigDecimal(Double.toString(b2)); result2 = m1.multiply(m2).doubleValue(); + result = (new Double(result2).toString()); + textfield.setText(result); } if (c.equals("/")) { if (b2 == 0) { result2 = 0; } else { result2 = a2 / b2; + result = (new Double(result2).toString()); + textfield.setText(result); + } + } + if (c.equals("x^y")) { + if (a2 == 0) { + textfield.setText("请输入非0的数字"); + } else {double b=1; + for(double a=b2;a>0;a--) + b*=a2; + result2 =b; + result = (new Double(result2).toString()); + textfield.setText(result); } } if (c.equals("sqrt")) { - result2 = Math.sqrt(a2); + if (a2<0) + textfield.setText("请输入正数"); + else {result2 = Math.sqrt(a2); + result = (new Double(result2).toString()); + textfield.setText(result);} + } + if (c.equals("log")) { + if ((a2<0)||(a2==1)) + textfield.setText("log的底数不可以小于零或者等于1"); + else {result2 = Math.log10(b2)/Math.log10(a2); + result = (new Double(result2).toString()); + textfield.setText(result);} + + } + if (c.equals("ln")) { + + result2 = Math.log(a2); + result = (new Double(result2).toString()); + textfield.setText(result);} + + + if (c.equals("n!")) { + if (a2<0) + textfield.setText("输入的数字不可以小于零"); + else {double b=1; + for(double a=a2;a>0;a--) + b=b*a; + result2 =b ;} + result = (new Double(result2).toString()); + textfield.setText(result);} + + if (c.equals("^2")) { result2 = a2*a2; + result = (new Double(result2).toString()); + textfield.setText(result); } if (c.equals("^3")) { result2 = a2*a2*a2; + result = (new Double(result2).toString()); + textfield.setText(result); } if(c.equals("1/x")) - result2=1/a2; + { + if(a2==0) + textfield.setText("请输入非零的数字"); + + else + {result2=1/a2; + result = (new Double(result2).toString()); + textfield.setText(result);}} if (c.equals("%")) { if (b2 == 0) { result2 = 0; } else { result2 = a2 % b2; + result = (new Double(result2).toString()); + textfield.setText(result); } } if(c.equals("sinx")) { double b=Math.toRadians(a2); - result2=Math.sin(b); + result2=Math.sin(b); + result = (new Double(result2).toString()); + textfield.setText(result); } if(c.equals("cosx")) { double b=Math.toRadians(a2); - result2=Math.cos(b);} + result2=Math.cos(b); + result = (new Double(result2).toString()); + textfield.setText(result);} if(c.equals("tanx")) { double b=Math.toRadians(a2); - result2=Math.tan(b);} - result = (new Double(result2).toString()); - textfield.setText(result); + result2=Math.tan(b); + result = (new Double(result2).toString()); + textfield.setText(result);} + } } + //Listener_point类中编写的是小数点键的相应逻辑 class Listener_point implements ActionListener { public void actionPerformed(ActionEvent e) { @@ -350,7 +437,7 @@ public class ONE302 extends JFrame { } //Listener_C类中编写了清除键的响应逻辑 - private class Listener_C implements ActionListener { + class Listener_C implements ActionListener { public void actionPerformed(ActionEvent e) { store = (JButton) e.getSource(); vt.add(store); @@ -371,7 +458,7 @@ public class ONE302 extends JFrame { } } - private class Listener_backspace implements ActionListener{ + class Listener_backspace implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 @@ -402,9 +489,11 @@ public class ONE302 extends JFrame { } - } + } + } + + - } -- Gitee From 4fc55d378ad67d10b690417dddc44b7753d5f46e Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 8 Jun 2022 22:16:41 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E6=9C=80=E7=BB=88=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index d6efa7b..1e8eba2 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -140,8 +140,8 @@ public class ONE302 extends JFrame { root2.add(Numbutton[3]); root2.add(btoper[1]); root2.add(daoButton); - root2.add(Numbutton[0]); root2.add(fuButton); + root2.add(Numbutton[0]); root2.add(pointButton); root2.add(btoper[0]); root2.add(tanButton); @@ -169,12 +169,15 @@ public class ONE302 extends JFrame { } }); //数字按钮添加监听器 - for(int i=0;i<=9;i++) + for(int i=0;i<=9;i++) { Numbutton[i].addActionListener(new Listener_num()); + Numbutton[i].setBackground(Color.GRAY); + Numbutton[i].setForeground(Color.WHITE); + } //四则运算按钮设置文本颜色 for(int i=0;i<=3;i++) { btoper[i].addActionListener(new Listener_oper()); - btoper[i].setForeground(Color.RED); } + } } //数字按钮的监听器Listener_num. -- Gitee From bbca27bbbcac1679853415dbb81ab4b5458d14a4 Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 9 Jun 2022 12:14:55 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E6=9C=80=E7=BB=88=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ONE302.java | 39 ++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/ONE302.java b/src/java2022spring/ONE302.java index 1e8eba2..2a88da0 100644 --- a/src/java2022spring/ONE302.java +++ b/src/java2022spring/ONE302.java @@ -307,7 +307,7 @@ public class ONE302 extends JFrame { } if (c.equals("/")) { if (b2 == 0) { - result2 = 0; + textfield.setText("请输入非零的除数"); } else { result2 = a2 / b2; result = (new Double(result2).toString()); @@ -336,16 +336,23 @@ public class ONE302 extends JFrame { if (c.equals("log")) { if ((a2<0)||(a2==1)) textfield.setText("log的底数不可以小于零或者等于1"); - else {result2 = Math.log10(b2)/Math.log10(a2); - result = (new Double(result2).toString()); - textfield.setText(result);} + + else { + if (b2<0) + textfield.setText("log的真数不可以小于零"); + else { + result2 = Math.log10(b2)/Math.log10(a2); + result = (new Double(result2).toString()); + textfield.setText(result);}} } if (c.equals("ln")) { - + if ((a2<0)||(a2==0)) + textfield.setText("输入的数字不可以小于等于零"); + else { result2 = Math.log(a2); result = (new Double(result2).toString()); - textfield.setText(result);} + textfield.setText(result);}} if (c.equals("n!")) { @@ -354,9 +361,9 @@ public class ONE302 extends JFrame { else {double b=1; for(double a=a2;a>0;a--) b=b*a; - result2 =b ;} - result = (new Double(result2).toString()); - textfield.setText(result);} + result2 =b ;result = (new Double(result2).toString()); + textfield.setText(result);}} + if (c.equals("^2")) { @@ -379,12 +386,15 @@ public class ONE302 extends JFrame { result = (new Double(result2).toString()); textfield.setText(result);}} if (c.equals("%")) { - if (b2 == 0) { - result2 = 0; + if (a2 < 0) { + textfield.setText("负数不可以取余数"); } else { + if (b2<0) { + textfield.setText("第二个数请输入正数");} + else { result2 = a2 % b2; result = (new Double(result2).toString()); - textfield.setText(result); + textfield.setText(result);} } } if(c.equals("sinx")) { @@ -399,10 +409,13 @@ public class ONE302 extends JFrame { result = (new Double(result2).toString()); textfield.setText(result);} if(c.equals("tanx")) { + if (a2==90) + textfield.setText("不存在"); + else { double b=Math.toRadians(a2); result2=Math.tan(b); result = (new Double(result2).toString()); - textfield.setText(result);} + textfield.setText(result);}} } } -- Gitee