From 260690214abae0a567bdefbf4d1acc0f3fd9407a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A9=BA=E9=A2=9C?= <491048072@qq.com> Date: Wed, 9 Jun 2021 23:20:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add=20src/java2020spring/My=5FCalculator5.?= =?UTF-8?q?=20=E9=80=80=E6=A0=BC=E8=BF=98=E5=9C=A8=E7=A0=94=E7=A9=B6?= =?UTF-8?q?=EF=BC=8C=E5=87=86=E5=A4=87=E5=9C=A8=E6=94=BE=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/My_Calculator5 | 244 ++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 src/java2020spring/My_Calculator5 diff --git a/src/java2020spring/My_Calculator5 b/src/java2020spring/My_Calculator5 new file mode 100644 index 0000000..aaa689a --- /dev/null +++ b/src/java2020spring/My_Calculator5 @@ -0,0 +1,244 @@ + +第一个Java程序 +package java2020spring; + + +public class My_Calculator5 { + + public static void main(String[] args) { + // TODO 自动生成的方法存根 + WindowOperation win = new WindowOperation(); + win.setTitle("Calculator"); + win.setSize(450, 600); + } + + +} + + +第二个Java程序 +package java2020spring; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.text.NumberFormat; +import javax.swing.*; + +public class WindowOperation extends JFrame { + + + JLabel input_shuru = new JLabel(); + JLabel output_result = new JLabel(); + JPanel xianshi = new JPanel(); + JPanel anjian = new JPanel(); + boolean qishi = true; + boolean kaishi = true; + String command = "="; + private double result; + public WindowOperation() { + init(); + setVisible(true); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + private void init() { + // TODO 自动生成的方法存根 + + String[] biaoshi = { "AC", "CE", "退格", "+", + "7", "8", "9", "-", + "4", "5", "6", "*", + "1", "2", "3", "/", + "00","0", ".", "=" }; + JButton shuzifuhao []= new JButton[biaoshi.length]; // 按键 + double result = 0; // 结果 + + input_shuru.setText(" "); // 标签内容初始化为空 + input_shuru.setHorizontalAlignment(JLabel.RIGHT); // 右对齐 + input_shuru.setFont(new Font("宋体", Font.BOLD, 25)); + input_shuru.setForeground(Color.BLACK); // 设置字体颜色为黑色,似乎不设置也是黑色 + + output_result.setText("0");// 标签内容初始化内容为0 + output_result.setHorizontalAlignment(JLabel.RIGHT); // 右对齐 + output_result.setFont(new Font("宋体", Font.BOLD, 35));// 。 + output_result.setForeground(Color.BLACK); // 设置字体颜色为蓝色 + + xianshi.setLayout(new BorderLayout()); + xianshi.add(input_shuru, BorderLayout.CENTER);// 把输入信息放在中间 + xianshi.add(output_result, BorderLayout.SOUTH);// 把输出结果放在下面 + + anjian.setLayout(new GridLayout(5, 5,6,6)); + + for (int i = 0; i < biaoshi.length; i++) { + shuzifuhao[i] = new JButton(biaoshi[i]); + + anjian.add(shuzifuhao[i]);// 把每个按钮分别添加到面板上 + } + shuzifuhao[3].addActionListener(new OperatorListener()); + shuzifuhao[7].addActionListener(new OperatorListener()); + shuzifuhao[11].addActionListener(new OperatorListener()); + shuzifuhao[15].addActionListener(new OperatorListener()); + shuzifuhao[19].addActionListener(new OperatorListener()); + //shuzifuhao[16].addActionListener(new OperatorListener()); + + shuzifuhao[0].addActionListener(new ComputerListener()); + shuzifuhao[1].addActionListener(new ComputerListener()); + shuzifuhao[2].addActionListener(new ComputerListener()); + shuzifuhao[4].addActionListener(new ComputerListener()); + shuzifuhao[5].addActionListener(new ComputerListener()); + shuzifuhao[6].addActionListener(new ComputerListener()); + shuzifuhao[8].addActionListener(new ComputerListener()); + shuzifuhao[9].addActionListener(new ComputerListener()); + shuzifuhao[10].addActionListener(new ComputerListener()); + shuzifuhao[12].addActionListener(new ComputerListener()); + shuzifuhao[13].addActionListener(new ComputerListener()); + shuzifuhao[14].addActionListener(new ComputerListener()); + shuzifuhao[16].addActionListener(new ComputerListener()); + shuzifuhao[17].addActionListener(new ComputerListener()); + shuzifuhao[18].addActionListener(new ComputerListener()); + add(anjian, BorderLayout.CENTER); + add(xianshi, BorderLayout.NORTH); + //((Object) shuzifuhao[3]).setAccelerator(KeyStroke.getKeyStroke('+')); + + } + + +public class ComputerListener implements ActionListener +{ + @Override + public void actionPerformed(ActionEvent e) + { + String str = e.getActionCommand(); + + if (qishi == true) + { + input_shuru.setText(" "); + qishi = false; + + } + + if (str.equals("AC")) + { + input_shuru.setText(" "); + output_result.setText("0"); + result = 0; + qishi = true; + kaishi = true; + } + + if (str.equals("CE")) + { + input_shuru.setText(" "); + output_result.setText("0"); + result = 0; + qishi = true; + kaishi = true; + } + + if (str.equals("退格")) + { + output_result.setText("功能待开发,请按AC键重新输入!"); + } + + //if(str.equals("平方")) { + // caculate(Double.parseDouble(input_shuru.getText())); + // result=result*result; + //} + if(str.equals("0")) + { //点击0,1,2,3,4,5,6,7,8,9时显示原数 + input_shuru.setText(input_shuru.getText() + str); + } + else + { + input_shuru.setText(input_shuru.getText() + str); + } + } +} + +class OperatorListener implements ActionListener +{ + @Override + public void actionPerformed(ActionEvent e) + { + String str = e.getActionCommand(); + if (qishi == false) + { + caculate(Double.parseDouble(input_shuru.getText())); + qishi = true; + command = str; + } + else + { + command = str; + if ( kaishi == true) + { + kaishi = false; + command = "="; + } + } + } +} + +//运算 +public void caculate(double shuzi) +{ + + if(command=="+") + { + result =result+shuzi; + } + + if(command=="-") + { + result = result-shuzi; + } + + if(command=="*") + { + result =result*shuzi; + } + + if(command=="/") + { + if(shuzi==0) + { + output_result.setText("提示:除数不能为0"); + return; + } + else { + result =result/shuzi; + } + + } + + if(command=="=") { + result =shuzi; + } + + if(command=="%") { + double num1=result; + int num_1=(int)num1; + double num2=shuzi; + int num_2=(int)num2; + if(num_1==result && num_2==shuzi) + result =result% shuzi; + } + + if(command=="平方") { + result=shuzi*shuzi; + } + //String jieguo=(String) result; + output_result.setText(""+String.valueOf(result)); //显示计算结果 + + +} + + + + + + + + +} + + +还有错误还有未实现的功能 \ No newline at end of file -- Gitee From f93da4c130285355ba06f41b2958eff375554285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A9=BA=E9=A2=9C?= <491048072@qq.com> Date: Thu, 10 Jun 2021 11:21:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?add=20src/java2020spring/My=5FCalclutor6.?= =?UTF-8?q?=20=E5=B0=86pow=E6=94=B9=E6=88=90=E7=AB=8B=E6=96=B9=E4=BA=86?= =?UTF-8?q?=EF=BC=8C=E6=83=B3=E5=AE=9E=E7=8E=B0=E5=87=BD=E6=95=B0pow?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E4=BD=86=E6=98=AF=E5=AE=9E=E5=9C=A8=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E4=B8=8D=E6=88=90=E5=8A=9F=E3=80=82=E6=83=B3=E7=BB=99?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8=E5=A2=9E=E5=8A=A0=E8=B4=9F=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B0=9A=E6=9C=AA=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/My_Calclutor6 | 322 +++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 src/java2020spring/My_Calclutor6 diff --git a/src/java2020spring/My_Calclutor6 b/src/java2020spring/My_Calclutor6 new file mode 100644 index 0000000..4b55e15 --- /dev/null +++ b/src/java2020spring/My_Calclutor6 @@ -0,0 +1,322 @@ +package shishi; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; + + + + +public class WindowOperation extends JFrame { + + + JLabel input_shuru = new JLabel(); + JLabel output_result = new JLabel(); + JPanel xianshi = new JPanel(); + JPanel anjian = new JPanel(); + boolean qishi = true; + boolean kaishi = true; + String command = "="; + private double result; + public WindowOperation() { + init(); + setVisible(true); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + private void init() { + // TODO 自动生成的方法存根 + + String[] biaoshi = { "立方","1/n","平方","负号", + "AC", "%", "退格", "+", + "7", "8", "9", "-", + "4", "5", "6", "*", + "1", "2", "3", "/", + "00","0", ".", "=" }; + JButton shuzifuhao []= new JButton[biaoshi.length]; // 按键 + double result = 0; // 结果 + + input_shuru.setText(" "); // 标签内容初始化为空 + input_shuru.setHorizontalAlignment(JLabel.RIGHT); // 右对齐 + input_shuru.setFont(new Font("宋体", Font.BOLD, 25)); + input_shuru.setForeground(Color.BLACK); // 设置字体颜色为黑色,似乎不设置也是黑色 + + output_result.setText("0.0");// 标签内容初始化内容为0 + output_result.setHorizontalAlignment(JLabel.RIGHT); // 右对齐 + output_result.setFont(new Font("宋体", Font.BOLD, 35));// 。 + output_result.setForeground(Color.BLACK); // 设置字体颜色为蓝色 + + xianshi.setLayout(new BorderLayout()); + xianshi.add(input_shuru, BorderLayout.CENTER);// 把输入信息放在中间 + xianshi.add(output_result, BorderLayout.SOUTH);// 把输出结果放在下面 + + anjian.setLayout(new GridLayout(6, 5,6,6)); + + for (int i = 0; i < biaoshi.length; i++) { + shuzifuhao[i] = new JButton(biaoshi[i]); + + anjian.add(shuzifuhao[i]);// 把每个按钮分别添加到面板上 + } + + shuzifuhao[0].addActionListener(new OperatorListener()); + shuzifuhao[1].addActionListener(new OperatorListener()); + shuzifuhao[2].addActionListener(new OperatorListener()); + //shuzifuhao[].addActionListener(new OperatorListener()); + shuzifuhao[3].addActionListener(new OperatorListener()); + shuzifuhao[7].addActionListener(new OperatorListener()); + shuzifuhao[11].addActionListener(new OperatorListener()); + shuzifuhao[15].addActionListener(new OperatorListener()); + shuzifuhao[19].addActionListener(new OperatorListener()); + shuzifuhao[23].addActionListener(new OperatorListener()); + //shuzifuhao[16].addActionListener(new OperatorListener()); + + shuzifuhao[0].addActionListener(new ComputerListener()); + shuzifuhao[1].addActionListener(new ComputerListener()); + shuzifuhao[2].addActionListener(new ComputerListener()); + shuzifuhao[4].addActionListener(new ComputerListener()); + shuzifuhao[5].addActionListener(new ComputerListener()); + shuzifuhao[6].addActionListener(new ComputerListener()); + shuzifuhao[8].addActionListener(new ComputerListener()); + shuzifuhao[9].addActionListener(new ComputerListener()); + shuzifuhao[10].addActionListener(new ComputerListener()); + shuzifuhao[12].addActionListener(new ComputerListener()); + shuzifuhao[13].addActionListener(new ComputerListener()); + shuzifuhao[14].addActionListener(new ComputerListener()); + shuzifuhao[16].addActionListener(new ComputerListener()); + shuzifuhao[17].addActionListener(new ComputerListener()); + shuzifuhao[18].addActionListener(new ComputerListener()); + shuzifuhao[20].addActionListener(new ComputerListener()); + shuzifuhao[21].addActionListener(new ComputerListener()); + shuzifuhao[22].addActionListener(new ComputerListener()); + //shuzifuhao[23].addActionListener(new ComputerListener()); + add(anjian, BorderLayout.CENTER); + add(xianshi, BorderLayout.NORTH); + + + } + + +public class ComputerListener implements ActionListener +{ + @Override + public void actionPerformed(ActionEvent e) + { + String str = e.getActionCommand(); + + if (qishi == true) + { + input_shuru.setText(" "); + qishi = false; + + } + + if (str.equals("AC")) + { + input_shuru.setText(" "); + output_result.setText("0.0"); + result = 0; + qishi = true; + kaishi = true; + } + if (str.equals("退格")) + { + + + + String text=input_shuru.getText(); + int i = text.length(); + if (i > 0) { + + text = text.substring(0, i - 1); + if (text.length() == 0) { + + input_shuru.setText("0"); + qishi = true; + command = "="; + } else { + // 显示新的文本 + input_shuru.setText(text); + } + } + //output_result.setText("功能待开发,请按AC键重新输入!"); + } + + + + if(str.equals("1")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("2")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("3")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("4")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("5")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("6")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("7")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("8")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("9")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("0")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals("00")) + { + input_shuru.setText(input_shuru.getText() + str); + } + if(str.equals(".")) + { + input_shuru.setText(input_shuru.getText() + str); + } + } +} + + + + + +class OperatorListener implements ActionListener +{ + @Override + public void actionPerformed(ActionEvent e) + { + String str = e.getActionCommand(); + if (qishi == false) + { + caculate(Double.parseDouble(input_shuru.getText())); + qishi = true; + command = str; + } + else + { + command = str; + if (str == "%" && kaishi == true) + { + kaishi = false; + command = "="; + } + } + } +} + +//运算 +public void caculate(double shuzi) +{ + + if(command=="+") + { + result =result+shuzi; + } + + if(command=="-") + { + result = result-shuzi; + } + + if(command=="*") + { + result =result*shuzi; + } + + if(command=="/") + { + if(shuzi==0) + { + output_result.setText("提示:除数不能为0"); + return; + } + else { + result =result/shuzi; + } + + } + + if(command=="=") + { + result =shuzi; + } + + + if(command=="%") + { + result=shuzi/100; + } + + + if(command=="平方") { + result=shuzi*shuzi; + } + + if(command=="立方") { + result=shuzi*shuzi*shuzi; + + } + + if (command==("1/n")) + { + result=1/shuzi; + //output_result.setText(""+String.valueOf(1/result)); + } + if(command=="开平方") { + //result=shuzi*shuzi; + //double x=2; + double result=Math.sqrt(2); + //double z=Math.pow(x,3); + //System.out.println(y); + //System.out.println(z); + }//631489766 + + + if (command==("fuhao")) + { + result=0-shuzi; + } + + //String jieguo=(String) result; + output_result.setText(""+String.valueOf(result)); //显示计算结果 + + +} + +public void ZeroText(double s) { + long sun1; + double star2; + sun1 = (long) s; + star2 = s-sun1; + if (star2 == 0) { + output_result.setText(String.valueOf(sun1)); + } else { + output_result.setText(String.valueOf(s)); + } +} + + + + + + +} + + -- Gitee