From ad1900caa1bf928d65cdbce9a5759cfb518b75dc Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Mon, 30 May 2022 16:18:23 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 74 ++++++++++++++++++++++++++++++ 1 file changed, 74 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..7c167ba --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,74 @@ +package java2022spring; + +import java.awt.*; +import java.awt.BorderLayout; + +import javax.swing.*; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.border.LineBorder; + +public class Calculator extends JFrame { + JPanel panel; + JTextArea textArea; + JMenuBar menuBar; + JMenu menu1, menu2, menu3; + JButton[][] button = new JButton[6][5]; + + + public Calculator() { + init(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + validate(); + } + + public void init() { + setTitle("覃国权的计算器"); + setBounds(300,50,640,700); + setLayout(new BorderLayout()); + LineBorder border = new LineBorder(Color.LIGHT_GRAY,6,true); + setResizable(false); + textArea = new JTextArea(2,10); + textArea.setFont(new Font("Calibre",Font.PLAIN,60)); + textArea.setBorder(border); + + String[][] str = { + {"MC", "MR", "MS", "M+", "M-"}, + {"<—", "CE", "C", "±", "√"}, + {"7", "8", "9", "/", "%"}, + {"4", "5", "6", "*", "1/x"}, + {"1", "2", "3", "-", "^2"}, + {"00", "0", ".", "+", "="} + }; + + panel = new JPanel(); + panel.setLayout(new GridLayout(6,5)); + for(int i=0;i<6;i++) { + for(int j=0;j<5;j++) { + button[i][j] = new JButton(str[i][j]); + button[i][j].setFont(new Font("黑体",Font.BOLD,30)); + panel.add(button[i][j]); + } + } + add(textArea, BorderLayout.NORTH); + add(panel); + + menu1 = new JMenu("更多"); + menu1.setFont(new Font("宋体",Font.BOLD,20)); + menu2 = new JMenu("关于"); + menu2.setFont(new Font("宋体",Font.BOLD,20)); + menu3 = new JMenu("使用说明"); + menu3.setFont(new Font("宋体",Font.BOLD,20)); + menuBar = new JMenuBar(); + menuBar.add(menu1); + menuBar.add(menu2); + menuBar.add(menu3); + setJMenuBar(menuBar); + } + + public static void main(String[] args) { + Calculator calculator = new Calculator(); + calculator.setVisible(true); + } +} -- Gitee From 1c2b21e715c562c45cac112671aaf2a6b81fb8ad Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Tue, 31 May 2022 23:26:59 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E6=94=B9=E8=BF=9B=E9=9D=A2=E6=9D=BF=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=AE=9E=E7=8E=B0=E9=83=A8=E5=88=86=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=A0=8F=E9=80=89=E9=A1=B9=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 | 72 ++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 7c167ba..2e4f39b 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,19 +1,19 @@ package java2022spring; import java.awt.*; -import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import javax.swing.*; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JTextField; import javax.swing.border.LineBorder; + public class Calculator extends JFrame { JPanel panel; JTextArea textArea; JMenuBar menuBar; - JMenu menu1, menu2, menu3; + JMenu menu1, menu2; + JMenuItem item1, item2, item3, item4, item5; JButton[][] button = new JButton[6][5]; @@ -35,7 +35,7 @@ public class Calculator extends JFrame { String[][] str = { {"MC", "MR", "MS", "M+", "M-"}, - {"<—", "CE", "C", "±", "√"}, + {"←", "CE", "C", "±", "√"}, {"7", "8", "9", "/", "%"}, {"4", "5", "6", "*", "1/x"}, {"1", "2", "3", "-", "^2"}, @@ -51,6 +51,23 @@ public class Calculator extends JFrame { panel.add(button[i][j]); } } + button[2][0].setBackground(new Color(230,240,250)); + button[2][1].setBackground(new Color(230,240,250)); + button[2][2].setBackground(new Color(230,240,250)); + button[3][0].setBackground(new Color(230,240,250)); + button[3][1].setBackground(new Color(230,240,250)); + button[3][2].setBackground(new Color(230,240,250)); + button[4][0].setBackground(new Color(230,240,250)); + button[4][1].setBackground(new Color(230,240,250)); + button[4][2].setBackground(new Color(230,240,250)); + button[5][0].setBackground(new Color(230,240,250)); + button[5][1].setBackground(new Color(230,240,250)); + button[5][2].setBackground(new Color(230,240,250)); + button[5][4].setForeground(Color.RED); + button[5][4].setBackground(new Color(230,240,250)); + button[5][4].setFont(new Font("黑体",Font.BOLD,50)); + button[1][0].setFont(new Font("黑体",Font.BOLD,40)); + add(textArea, BorderLayout.NORTH); add(panel); @@ -58,15 +75,52 @@ public class Calculator extends JFrame { menu1.setFont(new Font("宋体",Font.BOLD,20)); menu2 = new JMenu("关于"); menu2.setFont(new Font("宋体",Font.BOLD,20)); - menu3 = new JMenu("使用说明"); - menu3.setFont(new Font("宋体",Font.BOLD,20)); menuBar = new JMenuBar(); menuBar.add(menu1); menuBar.add(menu2); - menuBar.add(menu3); setJMenuBar(menuBar); + item1 = new JMenuItem("重量转换"); + item2 = new JMenuItem("长度转换"); + item3 = new JMenuItem("开发者"); + item4 = new JMenuItem("开发时间"); + item5 = new JMenuItem("敬请期待"); + menu1.add(item1); + menu1.add(item2); + menu2.add(item3); + menu2.add(item4); + menu1.add(item5); + + item3.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"21信管1班\n202125710120\n覃国权","开发者", JOptionPane.PLAIN_MESSAGE); + } + }); + item4.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"2022/05/30","开发时间", JOptionPane.PLAIN_MESSAGE); + } + }); + + + item1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + } + }); + item2.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + } + }); + + + } + + + + public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setVisible(true); -- Gitee From 5ee01f6d68b5ab340521f5dec18edcc5edf924e9 Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Mon, 6 Jun 2022 17:42:52 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=9E=E7=8E=B0?= =?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 | 165 +++++++++++++++++++++++++---- 1 file changed, 145 insertions(+), 20 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 2e4f39b..109c7e8 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,6 +1,7 @@ package java2022spring; import java.awt.*; + import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -8,14 +9,19 @@ import javax.swing.*; import javax.swing.border.LineBorder; -public class Calculator extends JFrame { + + +public class Calculator extends JFrame implements ActionListener{ JPanel panel; - JTextArea textArea; + JTextField textField = new JTextField(); JMenuBar menuBar; JMenu menu1, menu2; JMenuItem item1, item2, item3, item4, item5; - JButton[][] button = new JButton[6][5]; - + private JButton[][] button = new JButton[6][5]; + + private String getbutton; + private double result; + private boolean start; public Calculator() { init(); @@ -29,9 +35,18 @@ public class Calculator extends JFrame { setLayout(new BorderLayout()); LineBorder border = new LineBorder(Color.LIGHT_GRAY,6,true); setResizable(false); - textArea = new JTextArea(2,10); - textArea.setFont(new Font("Calibre",Font.PLAIN,60)); - textArea.setBorder(border); + textField.setPreferredSize(new Dimension(230,150)); + textField.setBackground(new Color(230,240,250)); + textField.setForeground(new Color(30,60,90)); + textField.setFont(new Font("黑体",Font.BOLD,40)); + textField.setEditable(false); + textField.setText("0"); + textField.setHorizontalAlignment(JTextField.RIGHT); + start = true; + result = 0; + getbutton = "="; + this.setResizable(false); + this.add( textField ); String[][] str = { {"MC", "MR", "MS", "M+", "M-"}, @@ -48,9 +63,12 @@ public class Calculator extends JFrame { for(int j=0;j<5;j++) { button[i][j] = new JButton(str[i][j]); button[i][j].setFont(new Font("黑体",Font.BOLD,30)); + button[i][j].addActionListener( this ); panel.add(button[i][j]); + } } + button[2][0].setBackground(new Color(230,240,250)); button[2][1].setBackground(new Color(230,240,250)); button[2][2].setBackground(new Color(230,240,250)); @@ -68,8 +86,8 @@ public class Calculator extends JFrame { button[5][4].setFont(new Font("黑体",Font.BOLD,50)); button[1][0].setFont(new Font("黑体",Font.BOLD,40)); - add(textArea, BorderLayout.NORTH); - add(panel); + add(textField, BorderLayout.NORTH); + add(panel,BorderLayout.CENTER); menu1 = new JMenu("更多"); menu1.setFont(new Font("宋体",Font.BOLD,20)); @@ -88,20 +106,23 @@ public class Calculator extends JFrame { menu1.add(item2); menu2.add(item3); menu2.add(item4); - menu1.add(item5); - + menu1.add(item5); + itemEvent(); + } + + public void itemEvent() { item3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"21信管1班\n202125710120\n覃国权","开发者", JOptionPane.PLAIN_MESSAGE); } }); + item4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"2022/05/30","开发时间", JOptionPane.PLAIN_MESSAGE); } }); - - + item1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -109,19 +130,123 @@ public class Calculator extends JFrame { }); item2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - + } }); - - - } - + public void actionPerformed(ActionEvent e){ + if( + e.getSource().equals( button[0][0] )||e.getSource().equals( button[0][1] )|| + e.getSource().equals( button[0][2] )||e.getSource().equals( button[0][3] )|| + e.getSource().equals( button[0][4] )|| e.getSource().equals( button[1][0] )|| + e.getSource().equals( button[1][1] )|| e.getSource().equals( button[1][2] )|| + e.getSource().equals( button[1][3] ) ||e.getSource().equals( button[2][0] )|| + e.getSource().equals( button[2][1] )||e.getSource().equals( button[2][2])|| + e.getSource().equals( button[3][0] )||e.getSource().equals( button[3][1] )|| + e.getSource().equals( button[3][2] )||e.getSource().equals( button[4][0] )|| + e.getSource().equals( button[4][1] )||e.getSource().equals( button[4][2] )|| + e.getSource().equals( button[5][0] )||e.getSource().equals( button[5][1] )|| + e.getSource().equals( button[5][2] ) + ) { + String input = e.getActionCommand(); + if (start){ + textField.setText(""); + start = false; + if( input.equals( "±" ) ) + textField.setText( textField.getText()+ "-" ); + } + else if( !input.equals( "±" ) ){ + String str = textField.getText(); + if( input.equals( "←") ){ + if( str.length() > 0 ) + textField.setText( str.substring(0,str.length() - 1 ) ); + } + else if( input.equals( "CE" ) ){ + textField.setText( "0"); + start = true; + } + else if(input.equals("C")){ + textField.setText("0"); + result = 0.0d; + start = true; + } + else if( input.equals( "MC" ) ){ + textField.setText( "暂未实现该功能"); + start = true; + } + else if( input.equals( "MS" ) ){ + textField.setText( "暂未实现该功能"); + start = true; + } + else if( input.equals( "MR" ) ){ + textField.setText( "暂未实现该功能"); + start = true; + } + else if( input.equals( "M+" ) ){ + textField.setText( "暂未实现该功能"); + start = true; + } + else if( input.equals( "M-" ) ){ + textField.setText( "暂未实现该功能"); + start = true; + } + else + textField.setText( textField.getText() + input ); + } + } + else + { + String command = e.getActionCommand(); + if( start ) + { + getbutton = command; + } + else + { + calculate( Double.parseDouble( textField.getText() ) ); + getbutton = command; + start = true; + } + } + } + public void calculate( double x ) { + double a = 0; + if ( getbutton.equals( "+" ) ) + result=result+x; + else if (getbutton.equals( "-" ) ) + result=result-x; + else if ( getbutton.equals( "*" ) ) + result=result*x; + else if ( getbutton.equals( "/" ) ){ + if (Double.parseDouble(textField.getText()) == 0.0) { + textField.setText("ERROR"); } + else { + result=result/Double.parseDouble(textField.getText());} + } + else if ( getbutton.equals("1/x")) { + if (Double.parseDouble(textField.getText()) == 0.0) { + textField.setText("ERROR"); } + else { + result = 1 / result;} + } + else if ( getbutton.equals("^2")) + result = result * result; + else if(getbutton.equals("%") ) + result = x/100; + else if ( getbutton.equals( "=" ) ) + result=x; + else if ( getbutton.equals( "√" ) ){ + if (a>=0) { + a = Math.sqrt(x); + result = a;} + else textField.setText("请输入一个非负数"); + } + textField.setText(" "+ result ); +} - - public static void main(String[] args) { + public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setVisible(true); } -- Gitee From 0282a6125a44485e14a0e9ed78656757214d836e Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Mon, 6 Jun 2022 19:51:29 +0800 Subject: [PATCH 4/9] =?UTF-8?q?MS=E7=AD=89=E5=8A=9F=E8=83=BD=E7=9A=84?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 34 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 109c7e8..eb1173b 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -17,6 +17,8 @@ public class Calculator extends JFrame implements ActionListener{ JMenuBar menuBar; JMenu menu1, menu2; JMenuItem item1, item2, item3, item4, item5; + + Double Mem = 0.0; private JButton[][] button = new JButton[6][5]; private String getbutton; @@ -139,10 +141,10 @@ public class Calculator extends JFrame implements ActionListener{ if( e.getSource().equals( button[0][0] )||e.getSource().equals( button[0][1] )|| e.getSource().equals( button[0][2] )||e.getSource().equals( button[0][3] )|| - e.getSource().equals( button[0][4] )|| e.getSource().equals( button[1][0] )|| - e.getSource().equals( button[1][1] )|| e.getSource().equals( button[1][2] )|| - e.getSource().equals( button[1][3] ) ||e.getSource().equals( button[2][0] )|| - e.getSource().equals( button[2][1] )||e.getSource().equals( button[2][2])|| + e.getSource().equals( button[0][4] )||e.getSource().equals( button[1][0] )|| + e.getSource().equals( button[1][1] )||e.getSource().equals( button[1][2] )|| + e.getSource().equals( button[1][3] )||e.getSource().equals( button[2][0] )|| + e.getSource().equals( button[2][1] )||e.getSource().equals( button[2][2] )|| e.getSource().equals( button[3][0] )||e.getSource().equals( button[3][1] )|| e.getSource().equals( button[3][2] )||e.getSource().equals( button[4][0] )|| e.getSource().equals( button[4][1] )||e.getSource().equals( button[4][2] )|| @@ -153,10 +155,10 @@ public class Calculator extends JFrame implements ActionListener{ if (start){ textField.setText(""); start = false; - if( input.equals( "±" ) ) + if( input.equals( "±" ) ) textField.setText( textField.getText()+ "-" ); } - else if( !input.equals( "±" ) ){ + if( !input.equals( "±" ) ){ String str = textField.getText(); if( input.equals( "←") ){ if( str.length() > 0 ) @@ -172,24 +174,30 @@ public class Calculator extends JFrame implements ActionListener{ start = true; } else if( input.equals( "MC" ) ){ - textField.setText( "暂未实现该功能"); + Mem = 0.0; start = true; } else if( input.equals( "MS" ) ){ - textField.setText( "暂未实现该功能"); + Mem = result; + textField.setText(" "+ result ); start = true; } - else if( input.equals( "MR" ) ){ - textField.setText( "暂未实现该功能"); + else if( input.equals( "MR" ) ){ + textField.setText( " "+Mem); start = true; } else if( input.equals( "M+" ) ){ - textField.setText( "暂未实现该功能"); + double x1, x2; + x1 = Mem; + x2 = result; + Mem = x1 + x2; start = true; } else if( input.equals( "M-" ) ){ - textField.setText( "暂未实现该功能"); - start = true; + double x1, x2; + x1 = Mem; + x2 = result; + Mem = x2 - x1; } else textField.setText( textField.getText() + input ); -- Gitee From 6a64ea503f3f755439d5ac7498fa3e32541ae0dd Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Tue, 7 Jun 2022 23:28:13 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E6=8C=89=E9=94=AE=E7=9A=84=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E8=BF=90=E7=AE=97=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E6=95=B4=E4=B8=AA=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 78 +++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index eb1173b..0dc5ad6 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -15,8 +15,8 @@ public class Calculator extends JFrame implements ActionListener{ JPanel panel; JTextField textField = new JTextField(); JMenuBar menuBar; - JMenu menu1, menu2; - JMenuItem item1, item2, item3, item4, item5; + JMenu menu1, menu2, menu3; + JMenuItem item1, item2, item3, item4, item5, item6; Double Mem = 0.0; private JButton[][] button = new JButton[6][5]; @@ -55,7 +55,7 @@ public class Calculator extends JFrame implements ActionListener{ {"←", "CE", "C", "±", "√"}, {"7", "8", "9", "/", "%"}, {"4", "5", "6", "*", "1/x"}, - {"1", "2", "3", "-", "^2"}, + {"1", "2", "3", "-", "x^2"}, {"00", "0", ".", "+", "="} }; @@ -95,20 +95,25 @@ public class Calculator extends JFrame implements ActionListener{ menu1.setFont(new Font("宋体",Font.BOLD,20)); menu2 = new JMenu("关于"); menu2.setFont(new Font("宋体",Font.BOLD,20)); + menu3 = new JMenu("说明"); + menu3.setFont(new Font("宋体",Font.BOLD,20)); menuBar = new JMenuBar(); menuBar.add(menu1); menuBar.add(menu2); + menuBar.add(menu3); setJMenuBar(menuBar); - item1 = new JMenuItem("重量转换"); + item1 = new JMenuItem("质量转换"); item2 = new JMenuItem("长度转换"); item3 = new JMenuItem("开发者"); item4 = new JMenuItem("开发时间"); item5 = new JMenuItem("敬请期待"); + item6 = new JMenuItem("使用说明"); menu1.add(item1); menu1.add(item2); menu2.add(item3); menu2.add(item4); menu1.add(item5); + menu3.add(item6); itemEvent(); } @@ -121,20 +126,61 @@ public class Calculator extends JFrame implements ActionListener{ item4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - JOptionPane.showMessageDialog(null,"2022/05/30","开发时间", JOptionPane.PLAIN_MESSAGE); + JOptionPane.showMessageDialog(null,"2022/06/08","开发时间", JOptionPane.PLAIN_MESSAGE); } }); item1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - + JOptionPane.showMessageDialog(null,"即将上线,敬请期待","质量转换", JOptionPane.PLAIN_MESSAGE); } }); + item2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - + JOptionPane.showMessageDialog(null,"即将上线,敬请期待","长度转换", JOptionPane.PLAIN_MESSAGE); + } + }); + + item6.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"(1)“C”:将之前输入的数字,计算结果等功能全部归零;\r\n" + + "\r\n" + + "(2)“CE”:清除键,清空当前输入的数据;\r\n" + + "\r\n" + + "(3)“MS”:存储当前的显示值;\r\n" + + "\r\n" + + "(4)“MC”:清空当前存储区中的值;\r\n" + + "\r\n" + + "(5)“MR”:将存储区的数调出到显示栏,同时存储区中的值不变;\r\n" + + "\r\n" + + "(6)“M+”:将存储区的数与当前显示的数相加,结果存到存储区,并不会直接显示;\r\n" + + "\r\n" + + "(7)“M-”:将当前显示的数与存储区的数相减,结果存到存储区;\r\n" + + "\r\n" + + "(8)“CE”:清空当前显示的所有值;\r\n" + + "\r\n" + + "(9)“C”:清空当前显示的所有值;以小数点形式归零;\r\n" + + "\r\n" + + "(8)“±”:正负号转换器;\r\n" + + "\r\n" + + "(8)“√”:先点击该按钮后写入求根号的数再点击“=”按钮后即可求得根号后的值;\r\n" + + "\r\n" + + "(8)“%”:先点击该按钮后写入求百分数的数再点击“=”按钮后即可求得百分数后的值;\r\n" + + "\r\n" + + "(8)“1/x”:先点击该按钮后写入求倒数的数再点击“=”按钮后即可求得倒数后的值;\r\n" + + "\r\n" + + "(8)“x^2”:先点击该按钮后写入求平方的数再点击“=”按钮后即可求得平方后的值。\r\n" + ,"使用说明", JOptionPane.PLAIN_MESSAGE); + } + }); + + item5.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"功能多有不全,使用不便请多海涵","敬请期待", JOptionPane.PLAIN_MESSAGE); } }); + } public void actionPerformed(ActionEvent e){ @@ -155,10 +201,11 @@ public class Calculator extends JFrame implements ActionListener{ if (start){ textField.setText(""); start = false; - if( input.equals( "±" ) ) + if( input.equals( "±" ) ) textField.setText( textField.getText()+ "-" ); + } - if( !input.equals( "±" ) ){ + if( !input.equals( "±" ) ){ String str = textField.getText(); if( input.equals( "←") ){ if( str.length() > 0 ) @@ -237,12 +284,17 @@ public class Calculator extends JFrame implements ActionListener{ if (Double.parseDouble(textField.getText()) == 0.0) { textField.setText("ERROR"); } else { - result = 1 / result;} + result = 1 / Double.parseDouble(textField.getText()); + textField.setText(" "+ result );} } - else if ( getbutton.equals("^2")) - result = result * result; - else if(getbutton.equals("%") ) + else if ( getbutton.equals("x^2")) { + result = Double.parseDouble(textField.getText()); + result = result * result; + } + else if(getbutton.equals("%") ) { + result = Double.parseDouble(textField.getText()); result = x/100; + } else if ( getbutton.equals( "=" ) ) result=x; else if ( getbutton.equals( "√" ) ){ -- Gitee From 22f914ae306c5876c95d1c2eee5f2b0a381d86c3 Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Wed, 8 Jun 2022 12:21:19 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 0dc5ad6..70cbaa7 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -177,7 +177,7 @@ public class Calculator extends JFrame implements ActionListener{ item5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - JOptionPane.showMessageDialog(null,"功能多有不全,使用不便请多海涵","敬请期待", JOptionPane.PLAIN_MESSAGE); + JOptionPane.showMessageDialog(null,"功能多有不全,使用不便请多海涵,感谢您的支持","敬请期待", JOptionPane.PLAIN_MESSAGE); } }); -- Gitee From b8242138bd25a72129590078abace6b22bfb85bb Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Wed, 8 Jun 2022 14:15:29 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 70cbaa7..119f78c 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -162,15 +162,15 @@ public class Calculator extends JFrame implements ActionListener{ + "\r\n" + "(9)“C”:清空当前显示的所有值;以小数点形式归零;\r\n" + "\r\n" - + "(8)“±”:正负号转换器;\r\n" + + "(10)“±”:正负号转换器;\r\n" + "\r\n" - + "(8)“√”:先点击该按钮后写入求根号的数再点击“=”按钮后即可求得根号后的值;\r\n" + + "(11)“√”:先点击该按钮后写入求根号的数再点击“=”按钮后即可求得根号后的值;\r\n" + "\r\n" - + "(8)“%”:先点击该按钮后写入求百分数的数再点击“=”按钮后即可求得百分数后的值;\r\n" + + "(12)“%”:先点击该按钮后写入求百分数的数再点击“=”按钮后即可求得百分数后的值;\r\n" + "\r\n" - + "(8)“1/x”:先点击该按钮后写入求倒数的数再点击“=”按钮后即可求得倒数后的值;\r\n" + + "(13)“1/x”:先点击该按钮后写入求倒数的数再点击“=”按钮后即可求得倒数后的值;\r\n" + "\r\n" - + "(8)“x^2”:先点击该按钮后写入求平方的数再点击“=”按钮后即可求得平方后的值。\r\n" + + "(14)“x^2”:先点击该按钮后写入求平方的数再点击“=”按钮后即可求得平方后的值。\r\n" ,"使用说明", JOptionPane.PLAIN_MESSAGE); } }); -- Gitee From 64a9c8c23f656ab04d816e06fb6da20598ddf436 Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Wed, 8 Jun 2022 14:25:31 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E5=AE=B9=E9=94=99=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 35 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 119f78c..2094ab5 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -158,7 +158,7 @@ public class Calculator extends JFrame implements ActionListener{ + "\r\n" + "(7)“M-”:将当前显示的数与存储区的数相减,结果存到存储区;\r\n" + "\r\n" - + "(8)“CE”:清空当前显示的所有值;\r\n" + + "(8)“CE”:类似隐藏当前显示的所有值;\r\n" + "\r\n" + "(9)“C”:清空当前显示的所有值;以小数点形式归零;\r\n" + "\r\n" @@ -171,6 +171,8 @@ public class Calculator extends JFrame implements ActionListener{ + "(13)“1/x”:先点击该按钮后写入求倒数的数再点击“=”按钮后即可求得倒数后的值;\r\n" + "\r\n" + "(14)“x^2”:先点击该按钮后写入求平方的数再点击“=”按钮后即可求得平方后的值。\r\n" + + "\r\n" + + "(14)“←”:将显示的数或符号从右回退一位\r\n" ,"使用说明", JOptionPane.PLAIN_MESSAGE); } }); @@ -268,21 +270,26 @@ public class Calculator extends JFrame implements ActionListener{ public void calculate( double x ) { double a = 0; - if ( getbutton.equals( "+" ) ) - result=result+x; - else if (getbutton.equals( "-" ) ) - result=result-x; - else if ( getbutton.equals( "*" ) ) - result=result*x; + if ( getbutton.equals( "+" ) ) { + result=result+x; + textField.setText(" "+ result );} + else if (getbutton.equals( "-" ) ) { + result=result-x; + textField.setText(" "+ result );} + else if ( getbutton.equals( "*" ) ) { + result=result*x; + textField.setText(" "+ result );} else if ( getbutton.equals( "/" ) ){ if (Double.parseDouble(textField.getText()) == 0.0) { textField.setText("ERROR"); } else { - result=result/Double.parseDouble(textField.getText());} + result=result/Double.parseDouble(textField.getText()); + textField.setText(" "+ result );} } else if ( getbutton.equals("1/x")) { if (Double.parseDouble(textField.getText()) == 0.0) { - textField.setText("ERROR"); } + textField.setText("ERROR"); + } else { result = 1 / Double.parseDouble(textField.getText()); textField.setText(" "+ result );} @@ -290,20 +297,24 @@ public class Calculator extends JFrame implements ActionListener{ else if ( getbutton.equals("x^2")) { result = Double.parseDouble(textField.getText()); result = result * result; + textField.setText(" "+ result ); } else if(getbutton.equals("%") ) { result = Double.parseDouble(textField.getText()); result = x/100; + textField.setText(" "+ result ); } - else if ( getbutton.equals( "=" ) ) + else if ( getbutton.equals( "=" ) ) { result=x; + textField.setText(" "+ result );} else if ( getbutton.equals( "√" ) ){ if (a>=0) { a = Math.sqrt(x); - result = a;} + result = a; + textField.setText(" "+ result );} else textField.setText("请输入一个非负数"); } - textField.setText(" "+ result ); + } public static void main(String[] args) { -- Gitee From 6be163634734adc5ebda867a4ae59fc14101d8e1 Mon Sep 17 00:00:00 2001 From: 10799 <10799@LAPTOP-ST71V0QM> Date: Wed, 8 Jun 2022 14:34:54 +0800 Subject: [PATCH 9/9] =?UTF-8?q?bug=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 2094ab5..66abc3b 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -308,7 +308,7 @@ public class Calculator extends JFrame implements ActionListener{ result=x; textField.setText(" "+ result );} else if ( getbutton.equals( "√" ) ){ - if (a>=0) { + if (Double.parseDouble(textField.getText())>=0) { a = Math.sqrt(x); result = a; textField.setText(" "+ result );} -- Gitee