From c9bfa87ea71a3f6c1dd4aeb15be7b1b0ce7a79e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=AC=E5=AE=87=E8=88=AA?= <9241469+wuyuhangzhou@user.noreply.gitee.com> Date: Thu, 10 Jun 2021 11:24:57 +0800 Subject: [PATCH] add src/java2020spring/Calculator. --- src/java2020spring/Calculator | 236 ++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 src/java2020spring/Calculator diff --git a/src/java2020spring/Calculator b/src/java2020spring/Calculator new file mode 100644 index 0000000..9776214 --- /dev/null +++ b/src/java2020spring/Calculator @@ -0,0 +1,236 @@ +package java2020spring; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +public class Calculator extends JFrame implements ActionListener { + private JPanel p1=new JPanel(); + private JPanel p2=new JPanel(); + private JTextField t1; + private JTextField t2; + private JLabel label; + StringBuffer str; + double x,y; + int z; + private JButton b[]=new JButton[18]; + private JButton b1,b2,b3,b4,b5,b6,b7,b8; + public Calculator() { + super("计算器"); + setBackground(Color.white); + getContentPane().setBackground(Color.white); + getContentPane().setForeground(Color.black); + Container c=getContentPane(); + t1=new JTextField(30); + t1.setEditable(false); + t2=new JTextField(30); + t2.setEditable(false); + label=new JLabel("wyh的计算器"); + label.setForeground(Color.black); + str=new StringBuffer(); + p2.add(label); + p2.add(t2); + p2.add(t1); + p2.setLayout(new GridLayout(3,1)); + for(int i=0;i<10;i++) { + String s=""+i; + b[i]=new JButton(s); + b[i].addActionListener(this); + } + b[10]=new JButton("-/+"); + b[11]=new JButton("."); + b1=new JButton("/"); + b2=new JButton("Back"); + b3=new JButton("*"); + b4=new JButton("C"); + b5=new JButton("+"); + b6=new JButton("Sqrt"); + b7=new JButton("-"); + b8=new JButton("="); + b[13]=new JButton("e"); + b[12]=new JButton("%"); + b[14]=new JButton("n!"); + b[15]=new JButton("10^"); + b[16]=new JButton("^"); + for(int i=0;i<=16;i++) { + b[i].setForeground(Color.black); + } + b1.setForeground(Color.RED);b3.setForeground(Color.RED); + b5.setForeground(Color.RED);b7.setForeground(Color.RED); + b2.setForeground(Color.BLACK);b4.setForeground(Color.BLACK); + b6.setForeground(Color.BLACK);b8.setForeground(Color.BLACK); + p1.add(b[7]);p1.add(b[8]);p1.add(b[9]);p1.add(b1);p1.add(b2); + p1.add(b[4]);p1.add(b[5]);p1.add(b[6]);p1.add(b3);p1.add(b4); + p1.add(b[1]);p1.add(b[2]);p1.add(b[3]);p1.add(b5);p1.add(b6); + p1.add(b[0]);p1.add(b[10]);p1.add(b[11]);p1.add(b7);p1.add(b[12]); + p1.add(b[15]);p1.add(b[13]);p1.add(b[14]);p1.add(b[16]);p1.add(b8); + p1.setLayout(new GridLayout(5,5,8,8)); + b[16].addActionListener(this); + b[14].addActionListener(this); b[15].addActionListener(this); + b[12].addActionListener(this); b[13].addActionListener(this); + b[10].addActionListener(this); b[11].addActionListener(this); + b1.addActionListener(this); b2.addActionListener(this); + b3.addActionListener(this); b4.addActionListener(this); + b5.addActionListener(this); b6.addActionListener(this); + b7.addActionListener(this); b8.addActionListener(this); + c.add(p2); + c.add(p1); + c.setLayout(new FlowLayout()); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + setResizable(false); + setSize(420,350); + } + + public static void main(String[] args) { + Calculator f=new Calculator(); + } + public void actionPerformed(ActionEvent e) { + try + { + if(e.getSource()==b4) { + t1.setText("0"); + t1.setHorizontalAlignment(JTextField.RIGHT); + str.setLength(0); + } + else if(e.getSource()==b[10]) { + x=Double.parseDouble(t1.getText().trim()); + t1.setText(""+(-x)); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else if(e.getSource()==b5) { + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=0; + } + else if(e.getSource()==b7) { + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=1; + } + else if(e.getSource()==b3) { + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=2; + } + else if(e.getSource()==b1) { + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=3; + } + else if(e.getSource()==b8) { + str.setLength(0); + switch(z) + { + case 0:t1.setText(""+(x+y));t1.setHorizontalAlignment(JTextField.RIGHT);break; + case 1:t1.setText(""+(x-y));t1.setHorizontalAlignment(JTextField.RIGHT);break; + case 2:t1.setText(""+(x*y));t1.setHorizontalAlignment(JTextField.RIGHT);break; + case 3:t1.setText(""+(x/y));t1.setHorizontalAlignment(JTextField.RIGHT);break; + }} + else if(e.getSource()==b[12]) { + x=Double.parseDouble(t1.getText().trim()); + t1.setText(""+(x*0.01)); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else if(e.getSource()==b[13]) { + x=Double.parseDouble(t1.getText().trim()); + t1.setText(""+2.71828182845); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else if(e.getSource()==b[14]) { + x=Double.parseDouble(t1.getText().trim()); + double sum=1; + if(x==0) { + sum=0; + } + for(double i=1;i<=x;i++) { + sum=sum*i; + } + t1.setText(""+(sum)); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else if(e.getSource()==b[15]) { + x=Double.parseDouble(t1.getText().trim()); + double sum=10; + if(x==0) { + sum=1; + } + for(double i=1;i<=x;i++) { + sum=sum*10; + } + t1.setText(""+(sum)); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else if(e.getSource()==b[16]) { + x=Double.parseDouble(t1.getText().trim()); + t1.setText(""+(x*x)); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else if(e.getSource()==b[11]) { + if(t1.getText().trim().indexOf('.')!=-1) {} + else { + if(t1.getText().trim().equals("0")){ + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else if(t1.getText().trim().equals("")) {} + else { + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + } + y=0d; + } + else if(e.getSource()==b6) { + x=Double.parseDouble(t1.getText().trim()); + if(x<0) { + t1.setText("数字格式异常"); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else { + t1.setText(""+Math.sqrt(x)); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + str.setLength(0); + y=0d; + } + else { + if(e.getSource()==b[0]) { + if(t1.getText().trim().equals("0")) {} + else + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); + y=Double.parseDouble(t1.getText().trim()); + } + else if(e.getSource()==b2) { + if(!t1.getText().trim().equals("0")) { + if(str.length()!=1) { + t1.setText(str.delete(str.length()-1,str.length()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + else { + t1.setText("0"); + t1.setHorizontalAlignment(JTextField.RIGHT); + str.setLength(0); + } + } + y=Double.parseDouble(t1.getText().trim()); + } + else { + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); + y=Double.parseDouble(t1.getText().trim()); + } + } + } + catch(NumberFormatException e1) {t1.setText("数字格式异常"); + t1.setHorizontalAlignment(JTextField.RIGHT);} + catch(StringIndexOutOfBoundsException e1) {t1.setText("字符串索引越界"); + t1.setHorizontalAlignment(JTextField.RIGHT); + } + +} +} \ No newline at end of file -- Gitee