From e1f85b59e35cb278bc34d4cfe06c36fe2c16e074 Mon Sep 17 00:00:00 2001 From: czldl Date: Sat, 19 Nov 2022 07:13:13 +0000 Subject: [PATCH 1/6] v0.0 Signed-off-by: czldl --- GuiCalculator.java | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 GuiCalculator.java diff --git a/GuiCalculator.java b/GuiCalculator.java new file mode 100644 index 0000000..9aba783 --- /dev/null +++ b/GuiCalculator.java @@ -0,0 +1,74 @@ +//GuiCalculator.java + +package gitops.idegitplugin; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.GridLayout; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JTextField; + +public class GuiCalculator { + + public static void main(String[] args) { + CalculatorUI ui=new CalculatorUI("VSCode GitPlugin GuiCalculator"); + ui.setVisible(true); + ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + +} + +class CalculatorUI extends JFrame +{ + private JTextField textField=new JTextField("0"); + + private JPanel panel=new JPanel(new GridLayout(4,4)); + private JButton bt11=new JButton("7"); + private JButton bt12=new JButton("8"); + private JButton bt13=new JButton("9"); + private JButton bt14=new JButton("/"); + private JButton bt21=new JButton("4"); + private JButton bt22=new JButton("5"); + private JButton bt23=new JButton("6"); + private JButton bt24=new JButton("*"); + private JButton bt31=new JButton("1"); + private JButton bt32=new JButton("2"); + private JButton bt33=new JButton("3"); + private JButton bt34=new JButton("-"); + private JButton bt41=new JButton("0"); + private JButton bt42=new JButton("."); + private JButton bt43=new JButton("="); + private JButton bt44=new JButton("+"); + JButton jb[]={bt11,bt12,bt13,bt14,bt21,bt22,bt23,bt24, + bt31,bt32,bt33,bt34,bt41,bt42,bt43,bt44}; + + public CalculatorUI(String title) + { + super(title); + this.ComponentInit(); + this.setLocation(300, 200); + this.setSize(600, 450); + } + + private void ComponentInit() + { + this.add(textField,BorderLayout.NORTH); + textField.setPreferredSize(new Dimension(300,80)); + textField.setHorizontalAlignment(JTextField.RIGHT); + textField.setFont(new Font("楷体",Font.PLAIN,32)); + textField.setEditable(false); + + this.add(panel,BorderLayout.CENTER); + panel.setPreferredSize(new Dimension(300,350)); + for(int i=0;i<16;i++){ + jb[i].setFont(new Font("楷体",Font.BOLD,32)); + panel.add(jb[i]); + } + + } + +} \ No newline at end of file -- Gitee From 0e3ec82c64839a332d9bb233347e002cb362fe2e Mon Sep 17 00:00:00 2001 From: chzcgitops Date: Sat, 19 Nov 2022 08:33:35 +0000 Subject: [PATCH 2/6] update GuiCalculator.java. Signed-off-by: chzcgitops --- GuiCalculator.java | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/GuiCalculator.java b/GuiCalculator.java index 9aba783..5761aa6 100644 --- a/GuiCalculator.java +++ b/GuiCalculator.java @@ -6,9 +6,11 @@ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; 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.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; @@ -46,10 +48,43 @@ class CalculatorUI extends JFrame JButton jb[]={bt11,bt12,bt13,bt14,bt21,bt22,bt23,bt24, bt31,bt32,bt33,bt34,bt41,bt42,bt43,bt44}; + class MyHandler implements ActionListener + { + String str=""; + + @Override + public void actionPerformed(ActionEvent e) { + JButton source=(JButton) e.getSource(); + + if(source==bt43) + { + //str=str+"= "+CalcString.getRes(str); + str=str+3; + textField.setText(str); + + int res=JOptionPane.showConfirmDialog(panel, "Calculated, Goon ?", "",JOptionPane.YES_NO_OPTION); + if(res==JOptionPane.YES_OPTION) + { + str=""; + textField.setText("0"); + } + else + System.exit(0); + + } + else + { + str=str+source.getText(); + textField.setText(str); + } + } + } public CalculatorUI(String title) { super(title); this.ComponentInit(); + MyHandler myHandler=new MyHandler(); + this.ListenerAdd(myHandler); this.setLocation(300, 200); this.setSize(600, 450); } @@ -70,5 +105,12 @@ class CalculatorUI extends JFrame } } + + private void ListenerAdd(MyHandler myHandler) + { + for(int i=0;i<16;i++){ + jb[i].addActionListener(myHandler); + } + } } \ No newline at end of file -- Gitee From 58a8588cbea56f2565fe1b4a0f44ae056cdbbf8f Mon Sep 17 00:00:00 2001 From: chzcgitops Date: Sat, 19 Nov 2022 08:34:34 +0000 Subject: [PATCH 3/6] update GuiCalculator.java. v0.2 Signed-off-by: chzcgitops --- GuiCalculator.java | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/GuiCalculator.java b/GuiCalculator.java index 5761aa6..54986ae 100644 --- a/GuiCalculator.java +++ b/GuiCalculator.java @@ -58,8 +58,7 @@ class CalculatorUI extends JFrame if(source==bt43) { - //str=str+"= "+CalcString.getRes(str); - str=str+3; + str=str+"= "+CalcString.getRes(str); textField.setText(str); int res=JOptionPane.showConfirmDialog(panel, "Calculated, Goon ?", "",JOptionPane.YES_NO_OPTION); @@ -113,4 +112,46 @@ class CalculatorUI extends JFrame } } +} +class CalcString +{ + public static double getRes(String str) + { + String s=""; + double n1 = 0,n2,res = 0; + char op = 0; + + for(int i=0;i Date: Sat, 19 Nov 2022 08:53:03 +0000 Subject: [PATCH 4/6] update GuiCalculator.java. v0.3 Signed-off-by: czldl --- GuiCalculator.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/GuiCalculator.java b/GuiCalculator.java index 54986ae..13b41b3 100644 --- a/GuiCalculator.java +++ b/GuiCalculator.java @@ -28,7 +28,7 @@ class CalculatorUI extends JFrame { private JTextField textField=new JTextField("0"); - private JPanel panel=new JPanel(new GridLayout(4,4)); + private JPanel panel=new JPanel(new GridLayout(5,4)); private JButton bt11=new JButton("7"); private JButton bt12=new JButton("8"); private JButton bt13=new JButton("9"); @@ -45,8 +45,12 @@ class CalculatorUI extends JFrame private JButton bt42=new JButton("."); private JButton bt43=new JButton("="); private JButton bt44=new JButton("+"); + private JButton bt51=new JButton("%"); + private JButton bt52=new JButton("!"); + private JButton bt53=new JButton("~"); + private JButton bt54=new JButton("^"); JButton jb[]={bt11,bt12,bt13,bt14,bt21,bt22,bt23,bt24, - bt31,bt32,bt33,bt34,bt41,bt42,bt43,bt44}; + bt31,bt32,bt33,bt34,bt41,bt42,bt43,bt44,bt51,bt52,bt53,bt54}; class MyHandler implements ActionListener { @@ -98,7 +102,7 @@ class CalculatorUI extends JFrame this.add(panel,BorderLayout.CENTER); panel.setPreferredSize(new Dimension(300,350)); - for(int i=0;i<16;i++){ + for(int i=0;i<20;i++){ jb[i].setFont(new Font("楷体",Font.BOLD,32)); panel.add(jb[i]); } @@ -107,7 +111,7 @@ class CalculatorUI extends JFrame private void ListenerAdd(MyHandler myHandler) { - for(int i=0;i<16;i++){ + for(int i=0;i<20;i++){ jb[i].addActionListener(myHandler); } } @@ -123,7 +127,7 @@ class CalcString for(int i=0;i Date: Sat, 19 Nov 2022 08:56:00 +0000 Subject: [PATCH 5/6] update GuiCalculator.java. v0.4 Signed-off-by: chzcgitops --- GuiCalculator.java | 304 ++++++++++++++++++++++++--------------------- 1 file changed, 162 insertions(+), 142 deletions(-) diff --git a/GuiCalculator.java b/GuiCalculator.java index 13b41b3..294dd93 100644 --- a/GuiCalculator.java +++ b/GuiCalculator.java @@ -16,149 +16,169 @@ import javax.swing.JTextField; public class GuiCalculator { - public static void main(String[] args) { - CalculatorUI ui=new CalculatorUI("VSCode GitPlugin GuiCalculator"); - ui.setVisible(true); - ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - } + public static void main(String[] args) { + CalculatorUI ui = new CalculatorUI("VSCode GitPlugin GuiCalculator"); + ui.setVisible(true); + ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +} + +class CalculatorUI extends JFrame { + + private JTextField textField = new JTextField("0"); + + private JPanel panel = new JPanel(new GridLayout(5, 4)); + private JButton bt11 = new JButton("7"); + private JButton bt12 = new JButton("8"); + private JButton bt13 = new JButton("9"); + private JButton bt14 = new JButton("/"); + private JButton bt21 = new JButton("4"); + private JButton bt22 = new JButton("5"); + private JButton bt23 = new JButton("6"); + private JButton bt24 = new JButton("*"); + private JButton bt31 = new JButton("1"); + private JButton bt32 = new JButton("2"); + private JButton bt33 = new JButton("3"); + private JButton bt34 = new JButton("-"); + private JButton bt41 = new JButton("0"); + private JButton bt42 = new JButton("."); + private JButton bt43 = new JButton("="); + private JButton bt44 = new JButton("+"); + private JButton bt51 = new JButton("%"); + private JButton bt52 = new JButton("!"); + private JButton bt53 = new JButton("~"); + private JButton bt54 = new JButton("^"); + JButton jb[] = { + bt11, + bt12, + bt13, + bt14, + bt21, + bt22, + bt23, + bt24, + bt31, + bt32, + bt33, + bt34, + bt41, + bt42, + bt43, + bt44, + bt51, + bt52, + bt53, + bt54, + }; + + class MyHandler implements ActionListener { + + String str = ""; + + @Override + public void actionPerformed(ActionEvent e) { + JButton source = (JButton) e.getSource(); + + if (source == bt43) { + str = str + "= " + CalcString.getRes(str); + textField.setText(str); + int res = JOptionPane.showConfirmDialog( + panel, + "Calculated, Goon ?", + "", + JOptionPane.YES_NO_OPTION + ); + if (res == JOptionPane.YES_OPTION) { + str = ""; + textField.setText("0"); + } else System.exit(0); + } else { + str = str + source.getText(); + textField.setText(str); + } + } + } + + public CalculatorUI(String title) { + super(title); + this.ComponentInit(); + MyHandler myHandler = new MyHandler(); + this.ListenerAdd(myHandler); + this.setLocation(300, 200); + this.setSize(600, 450); + } + + private void ComponentInit() { + this.add(textField, BorderLayout.NORTH); + textField.setPreferredSize(new Dimension(300, 80)); + textField.setHorizontalAlignment(JTextField.RIGHT); + textField.setFont(new Font("楷体", Font.PLAIN, 32)); + textField.setEditable(false); + + this.add(panel, BorderLayout.CENTER); + panel.setPreferredSize(new Dimension(300, 350)); + for (int i = 0; i < 20; i++) { + jb[i].setFont(new Font("楷体", Font.BOLD, 32)); + panel.add(jb[i]); + } + } + + private void ListenerAdd(MyHandler myHandler) { + for (int i = 0; i < 20; i++) { + jb[i].addActionListener(myHandler); + } + } } -class CalculatorUI extends JFrame -{ - private JTextField textField=new JTextField("0"); - - private JPanel panel=new JPanel(new GridLayout(5,4)); - private JButton bt11=new JButton("7"); - private JButton bt12=new JButton("8"); - private JButton bt13=new JButton("9"); - private JButton bt14=new JButton("/"); - private JButton bt21=new JButton("4"); - private JButton bt22=new JButton("5"); - private JButton bt23=new JButton("6"); - private JButton bt24=new JButton("*"); - private JButton bt31=new JButton("1"); - private JButton bt32=new JButton("2"); - private JButton bt33=new JButton("3"); - private JButton bt34=new JButton("-"); - private JButton bt41=new JButton("0"); - private JButton bt42=new JButton("."); - private JButton bt43=new JButton("="); - private JButton bt44=new JButton("+"); - private JButton bt51=new JButton("%"); - private JButton bt52=new JButton("!"); - private JButton bt53=new JButton("~"); - private JButton bt54=new JButton("^"); - JButton jb[]={bt11,bt12,bt13,bt14,bt21,bt22,bt23,bt24, - bt31,bt32,bt33,bt34,bt41,bt42,bt43,bt44,bt51,bt52,bt53,bt54}; - - class MyHandler implements ActionListener - { - String str=""; - - @Override - public void actionPerformed(ActionEvent e) { - JButton source=(JButton) e.getSource(); - - if(source==bt43) - { - str=str+"= "+CalcString.getRes(str); - textField.setText(str); - - int res=JOptionPane.showConfirmDialog(panel, "Calculated, Goon ?", "",JOptionPane.YES_NO_OPTION); - if(res==JOptionPane.YES_OPTION) - { - str=""; - textField.setText("0"); - } - else - System.exit(0); - - } - else - { - str=str+source.getText(); - textField.setText(str); - } - } - } - public CalculatorUI(String title) - { - super(title); - this.ComponentInit(); - MyHandler myHandler=new MyHandler(); - this.ListenerAdd(myHandler); - this.setLocation(300, 200); - this.setSize(600, 450); - } - - private void ComponentInit() - { - this.add(textField,BorderLayout.NORTH); - textField.setPreferredSize(new Dimension(300,80)); - textField.setHorizontalAlignment(JTextField.RIGHT); - textField.setFont(new Font("楷体",Font.PLAIN,32)); - textField.setEditable(false); - - this.add(panel,BorderLayout.CENTER); - panel.setPreferredSize(new Dimension(300,350)); - for(int i=0;i<20;i++){ - jb[i].setFont(new Font("楷体",Font.BOLD,32)); - panel.add(jb[i]); - } - - } - - private void ListenerAdd(MyHandler myHandler) - { - for(int i=0;i<20;i++){ - jb[i].addActionListener(myHandler); - } - } - +class CalcString { + + public static double getRes(String str) { + String s = ""; + double n1 = 0, n2, res = 0; + char op = 0; + + for (int i = 0; i < str.length(); i++) { + if ( + str.charAt(i) != '+' && + str.charAt(i) != '-' && + str.charAt(i) != '*' && + str.charAt(i) != '/' && + str.charAt(i) != '%' + ) { + s = s + str.charAt(i); + } else { + n1 = Double.parseDouble(s); + op = str.charAt(i); + s = ""; + } + } + n2 = Double.parseDouble(s); + + switch (op) { + case '+': + res = n1 + n2; + break; + case '-': + res = n1 - n2; + break; + case '*': + res = n1 * n2; + break; + case '/': + if (0 == n2) { + System.out.println("Error:/0"); + res = 0; + break; + } + res = n1 / n2; + break; + case '%': + res = (int) n1 % (int) n2; + break; + default: + } + + return res; + } } -class CalcString -{ - public static double getRes(String str) - { - String s=""; - double n1 = 0,n2,res = 0; - char op = 0; - - for(int i=0;i Date: Mon, 21 Nov 2022 00:36:55 +0000 Subject: [PATCH 6/6] =?UTF-8?q?update=20GuiCalculator.java.=20//=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=BD=BB=E5=BA=95=E8=A7=A3=E5=86=B3=E9=99=A40?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: czldl --- GuiCalculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GuiCalculator.java b/GuiCalculator.java index 294dd93..5e7ec42 100644 --- a/GuiCalculator.java +++ b/GuiCalculator.java @@ -168,7 +168,7 @@ class CalcString { case '/': if (0 == n2) { System.out.println("Error:/0"); - res = 0; + res = 0;//没有彻底解决除0问题 break; } res = n1 / n2; -- Gitee