From 09878626ea0659d37388a84eafef1ae7e662727e Mon Sep 17 00:00:00 2001 From: 412 <375057209@qq.com> Date: Sun, 29 May 2022 10:11:37 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..a2add60 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -2,6 +2,7 @@ package java2022spring; public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + new CalculatorJFrame(); + } } -- Gitee From 0b86b38b2cf2641a0ee0c3757924eb6b02fe9763 Mon Sep 17 00:00:00 2001 From: 412 <375057209@qq.com> Date: Mon, 6 Jun 2022 23:02:04 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8=E2=80=94?= =?UTF-8?q?=E2=80=94=E2=80=94=E2=80=94412?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/CalculatorJFrame.java | 126 +++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/java2022spring/CalculatorJFrame.java diff --git a/src/java2022spring/CalculatorJFrame.java b/src/java2022spring/CalculatorJFrame.java new file mode 100644 index 0000000..c5053c7 --- /dev/null +++ b/src/java2022spring/CalculatorJFrame.java @@ -0,0 +1,126 @@ +package java2022spring; +import java.awt.*; +import java.lang.*; +import javax.swing.*; +import java.awt.event.*; +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +public class CalculatorJFrame extends JFrame implements ActionListener{ + private JTextField Text1=new JTextField(); + private JPanel northJP=new JPanel(); + private JPanel JP=new JPanel(); + CalculatorJFrame(){ + this.Init(); + } + public void Init() { + setTitle("计算器"); + JPanel centerPanel= new JPanel(); + + //设置好按钮 + Text1.setPreferredSize(new Dimension(70, 80)); + this.JP.setLayout(new GridLayout(4, 4));//设置布局 + String btn_text = "1234567890+-*.=/"; + String regex = "[\\+\\-*/.=]"; + for (int i = 0; i < 16; i++) { + String temp = btn_text.substring(i, i + 1); + JButton btn = new JButton(); + btn.setText(temp); + btn.setBackground(Color.WHITE); + if (temp.matches(regex)) { + btn.setFont(new Font("粗体", Font.BOLD, 50)); + btn.setForeground(Color.RED); + btn.setBackground(Color.white); + } + JButton clear=new JButton("清除"); + + clear.setFont(new Font("粗体", Font.BOLD, 50)); + clear.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + Text1.setText(""); + + } + }); + JButton Pi=new JButton("PI"); + Pi.setFont(new Font("粗体",Font.BOLD,50)); + Pi.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Text1.setText("3.141593"); + } + }); + JButton zhengfu=new JButton("+/-"); + zhengfu.setFont(new Font("粗体",Font.BOLD,50)); + zhengfu.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Text1.setText("-"); + } + }); + JButton meiyong=new JButton("没用"); + + btn.addActionListener(this); + JP.add(btn); + + + + Text1.setSize(50,20); + Text1.setFont(new Font("宋体",Font.PLAIN,80)); + add(centerPanel,BorderLayout.SOUTH); + add(clear,BorderLayout.EAST); + add(Text1,BorderLayout.NORTH); + add(JP); + + setBounds(5,5,15,20); + setSize(1050,930); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + validate(); + } + } + private String firstInput = null; + private String operator = null; + + + +public void actionPerformed(ActionEvent e) { + + String click = e.getActionCommand(); + if (".0123456789".indexOf(click) != -1) { + this.Text1.setText(Text1.getText() + click); + this.Text1.setHorizontalAlignment(JTextField.RIGHT); + + } else if (click.matches("[\\+\\-*/]{1}")) { + operator = click; + firstInput = this.Text1.getText(); + this.Text1.setText(""); + } else if (click.equals("=")) { + Double a = Double.valueOf(firstInput); + Double b = Double.valueOf(this.Text1.getText()); + 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.Text1.setText(result.toString()); + } + + + + + } + + + +} -- Gitee From 4504118c2bf552a00953e589c7ebc4b325d63aa0 Mon Sep 17 00:00:00 2001 From: 412 <375057209@qq.com> Date: Tue, 7 Jun 2022 10:54:08 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8412?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/CalculatorJFrame.java | 65 +++++++++++++++++------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/src/java2022spring/CalculatorJFrame.java b/src/java2022spring/CalculatorJFrame.java index c5053c7..330f64a 100644 --- a/src/java2022spring/CalculatorJFrame.java +++ b/src/java2022spring/CalculatorJFrame.java @@ -8,7 +8,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CalculatorJFrame extends JFrame implements ActionListener{ private JTextField Text1=new JTextField(); - private JPanel northJP=new JPanel(); + private JPanel JP=new JPanel(); CalculatorJFrame(){ this.Init(); @@ -16,10 +16,10 @@ public class CalculatorJFrame extends JFrame implements ActionListener{ public void Init() { setTitle("计算器"); JPanel centerPanel= new JPanel(); - + JPanel WestJP=new JPanel(); //设置好按钮 Text1.setPreferredSize(new Dimension(70, 80)); - this.JP.setLayout(new GridLayout(4, 4));//设置布局 + this.JP.setLayout(new GridLayout(4,4));//设置布局 String btn_text = "1234567890+-*.=/"; String regex = "[\\+\\-*/.=]"; for (int i = 0; i < 16; i++) { @@ -32,9 +32,10 @@ public class CalculatorJFrame extends JFrame implements ActionListener{ btn.setForeground(Color.RED); btn.setBackground(Color.white); } - JButton clear=new JButton("清除"); - clear.setFont(new Font("粗体", Font.BOLD, 50)); + JButton clear=new JButton("清除"); + clear.setBounds(10, 20, 10, 10); + clear.setFont(new Font("粗体", Font.BOLD, 20)); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -42,36 +43,63 @@ public class CalculatorJFrame extends JFrame implements ActionListener{ } }); + JButton Pi=new JButton("PI"); - Pi.setFont(new Font("粗体",Font.BOLD,50)); + Pi.setFont(new Font("粗体",Font.BOLD,20)); Pi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Text1.setText("3.141593"); } }); JButton zhengfu=new JButton("+/-"); - zhengfu.setFont(new Font("粗体",Font.BOLD,50)); + zhengfu.setFont(new Font("粗体",Font.BOLD,20)); zhengfu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Text1.setText("-"); } }); - JButton meiyong=new JButton("没用"); + + + JButton genhao=new JButton("sqrt"); + genhao.setFont(new Font("粗体",Font.BOLD,20)); + genhao.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + double i=Double.parseDouble(Text1.getText()); + double c=Math.sqrt(i); + firstInput=String.valueOf(c); + } + }); + + JButton daoshu=new JButton("1/x"); + daoshu.setFont(new Font("粗体",Font.BOLD,20)); + daoshu.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + double z=Double .parseDouble(Text1.getText()); + double h=1/z; + firstInput=String.valueOf(h); + } + }); + btn.addActionListener(this); - btn.addActionListener(this); - JP.add(btn); - - - Text1.setSize(50,20); + JP.add(btn); + WestJP.add(genhao); + + Text1.setSize(20,20); Text1.setFont(new Font("宋体",Font.PLAIN,80)); add(centerPanel,BorderLayout.SOUTH); - add(clear,BorderLayout.EAST); + add(daoshu,BorderLayout.WEST); + add(clear,BorderLayout.SOUTH); + add(Pi,BorderLayout.EAST); + + add(Text1,BorderLayout.NORTH); - add(JP); + add(JP,BorderLayout.CENTER); + - setBounds(5,5,15,20); - setSize(1050,930); + setBounds(10,20,15,20); + setSize(550,530); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); validate(); @@ -79,7 +107,8 @@ public class CalculatorJFrame extends JFrame implements ActionListener{ } private String firstInput = null; private String operator = null; - + + public void actionPerformed(ActionEvent e) { -- Gitee From b1a32d9db2aa63515938a550ef3777f426947d9b Mon Sep 17 00:00:00 2001 From: 412 <375057209@qq.com> Date: Thu, 9 Jun 2022 19:42:20 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=9C=80=E7=BB=88=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/CalculatorJFrame.java | 68 +++++++++++++++++++----- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/CalculatorJFrame.java b/src/java2022spring/CalculatorJFrame.java index 330f64a..4f5139d 100644 --- a/src/java2022spring/CalculatorJFrame.java +++ b/src/java2022spring/CalculatorJFrame.java @@ -1,12 +1,18 @@ package java2022spring; import java.awt.*; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; import java.lang.*; +import java.rmi.AccessException; + import javax.swing.*; import java.awt.event.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class CalculatorJFrame extends JFrame implements ActionListener{ +import java.io.IOException; +public class CalculatorJFrame extends JFrame implements ActionListener { private JTextField Text1=new JTextField(); private JPanel JP=new JPanel(); @@ -67,7 +73,8 @@ public class CalculatorJFrame extends JFrame implements ActionListener{ double i=Double.parseDouble(Text1.getText()); double c=Math.sqrt(i); - firstInput=String.valueOf(c); + String iu=String.valueOf(c); + Text1.setText(iu); } }); @@ -77,21 +84,22 @@ public class CalculatorJFrame extends JFrame implements ActionListener{ public void actionPerformed(ActionEvent e) { double z=Double .parseDouble(Text1.getText()); double h=1/z; - firstInput=String.valueOf(h); + String o=String.valueOf(h); + Text1.setText(o); } }); btn.addActionListener(this); JP.add(btn); - WestJP.add(genhao); + Text1.setSize(20,20); Text1.setFont(new Font("宋体",Font.PLAIN,80)); add(centerPanel,BorderLayout.SOUTH); add(daoshu,BorderLayout.WEST); add(clear,BorderLayout.SOUTH); - add(Pi,BorderLayout.EAST); + add(genhao,BorderLayout.EAST); add(Text1,BorderLayout.NORTH); @@ -103,26 +111,63 @@ public class CalculatorJFrame extends JFrame implements ActionListener{ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); validate(); + daoshu.addActionListener(new ActionListener(){ + + public void actionPerformed(ActionEvent e) { + int y=Text1.getText().length(); + if(y==0) { + JOptionPane.showMessageDialog(null, "你没有输入数字"); + } + + } + }); + genhao.addActionListener(new ActionListener(){ + + public void actionPerformed(ActionEvent e) { + int y=Text1.getText().length(); + if(y==0) { + JOptionPane.showMessageDialog(null, "你没有输入数字"); + } + + } + }); + clear.addActionListener(new ActionListener(){ + + public void actionPerformed(ActionEvent e) { + int y=Text1.getText().length(); + if(y==0) { + JOptionPane.showMessageDialog(null, "你没有输入数字"); + } + + } + }); } - } + + + } + + private String firstInput = null; private String operator = null; -public void actionPerformed(ActionEvent e) { +public void actionPerformed(ActionEvent m) { - String click = e.getActionCommand(); + String click = m.getActionCommand(); if (".0123456789".indexOf(click) != -1) { this.Text1.setText(Text1.getText() + click); this.Text1.setHorizontalAlignment(JTextField.RIGHT); } else if (click.matches("[\\+\\-*/]{1}")) { + operator = click; firstInput = this.Text1.getText(); + this.Text1.setText(""); } else if (click.equals("=")) { + if(Text1.getText().length()==0) {Text1.setText("你没有输入数字");} Double a = Double.valueOf(firstInput); Double b = Double.valueOf(this.Text1.getText()); Double result = null; @@ -145,11 +190,8 @@ public void actionPerformed(ActionEvent e) { this.Text1.setText(result.toString()); } - - - - } - } +} + -- Gitee From 335cc64f6900ba86a06263333eedfcdf055e5129 Mon Sep 17 00:00:00 2001 From: 412 <375057209@qq.com> Date: Thu, 9 Jun 2022 19:46:39 +0800 Subject: [PATCH 5/5] =?UTF-8?q?final=20=E7=89=88=E6=9C=AC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/CalculatorJFrame.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/CalculatorJFrame.java b/src/java2022spring/CalculatorJFrame.java index 4f5139d..967a123 100644 --- a/src/java2022spring/CalculatorJFrame.java +++ b/src/java2022spring/CalculatorJFrame.java @@ -120,7 +120,7 @@ public class CalculatorJFrame extends JFrame implements ActionListener { } } - }); + });//网上看的错误处理解决弹出窗口的方法 genhao.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { @@ -130,7 +130,7 @@ public class CalculatorJFrame extends JFrame implements ActionListener { } } - }); + });//网上看的错误处理解决弹出窗口的方法 clear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { @@ -140,7 +140,7 @@ public class CalculatorJFrame extends JFrame implements ActionListener { } } - }); + });//网上看的错误处理解决弹出窗口的方法 } -- Gitee