diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..76536353306b36e01c2b0ea3aa23228e3bdacb72 --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,595 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class Calculator extends JFrame implements ActionListener + { + JTextArea memoryArea=new JTextArea(" ",1,5); + JTextArea process=new JTextArea(" ",1,20); + JTextArea dispresult=new JTextArea("0. ",1,30); + Font font1=new Font("Vani",Font.BOLD,30 ); + Font font2=new Font("Arial Rounded MT Bold",Font.PLAIN,10 ); + Font font3=new Font("Arial Rounded MT Bold",Font.PLAIN,20 ); + Font font4=new Font("Vani",Font.PLAIN,15); + + JButton[]jbuttons=new JButton[32]; + + double result=0,first=0,second=0; + double memery=0; + char firstsymbol='\0',secondsymbol='\0'; + boolean prev=true,repeat=true,dot=true; + Calculator() + { + super("Calculator"); + this.setTitle("计算器"); + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch(Exception e) + { + System.out.println(e); + } + JPanel resultField=new JPanel(); + resultField.setLayout(new GridLayout(3,1)); + + JPanel buttonField=new JPanel(); + Container all=getContentPane(); + + GridLayout grid1; + grid1=new GridLayout(8,4,0,0); + + memoryArea.setFont(font3); + dispresult.setFont(font3); + process.setFont(font4); + memoryArea.setEditable(false); + process.setEditable(false); + dispresult.setEditable(false); + + + resultField.add(memoryArea); + resultField.add(process); + resultField.add(dispresult); + + all.add(resultField,"North"); + + String[]buttonname={"MS","MC","MR","M+","1/x","x2","√","㏒","%","sin","cos","tan","CE","C","back","÷","7","8","9","×","4","5","6","-","1","2","3","+","±","0",".","="}; + + buttonField.setLayout(grid1); + buttonField.setBackground(Color.gray); + for(int i=0;i<4;i++) + { + + for(int j=0;j<8;j++) + { + jbuttons[i*8+j]=new JButton(buttonname[i*8+j]); + jbuttons[i*8+j].addActionListener(this); + jbuttons[i*8+j].setBackground(Color.LIGHT_GRAY ); + buttonField.add(jbuttons[i*8+j]); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + + } + } + for(int j=0;j<4;j++){ + jbuttons[j].setFont(font2); + jbuttons[j].setBackground(new Color(250,250,250)); + jbuttons[j].setPreferredSize(new Dimension(40,40)); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + } + for(int j=4;j<16;j++){ + jbuttons[j].setFont(font4); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + + } + for(int j=16;j<32;j++){ + jbuttons[j].setFont(font1); + jbuttons[j].setBorder(BorderFactory.createRaisedBevelBorder()); + + } + jbuttons[15].setFont(font4); + jbuttons[19].setFont(font4); + + all.add(buttonField,"Center"); + setSize(422,405); + setResizable(true); + setVisible(true); + //this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + public void pressNumber(String n) + { + process.setText(process.getText()+n); + if(prev) + { + dispresult.setText(n); + prev=false; + } + else dispresult.append(n); + } + public boolean divide(double d) + { + if(d==0) + { + dispresult.setText("除数不能为0!"); + prev=true; + repeat=true; + firstsymbol='\0'; + secondsymbol='\0'; + return true; + } + return false; + } + public void actionPerformed(ActionEvent e) + { + Object source=e.getSource(); + if(source==jbuttons[12]) + { + dispresult.setText("0."); + dot=true; + prev=true; + repeat=true; + return; + } + + if(source==jbuttons[13]) + { + process.setText(null); + dispresult.setText("0."); + firstsymbol='\0'; + secondsymbol='\0'; + prev=true; + repeat=true; + dot=true; + return; + } + if(source==jbuttons[14]){ + String text=process.getText(); + int i=text.length(); + if(i>0){ + text=text.substring(0,i-1); + if(text.length()==0){ + dispresult.setText("0."); + process.setText(""); + firstsymbol='\0'; + secondsymbol='\0'; + first=second=0; + prev=true; + repeat=true; + dot=true; + } + else{ + dispresult.setText(text); + process.setText(text); + } + + } + } + if(source==jbuttons[1]) + { + memery=0; + memoryArea.setText(" "); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[29]) + { + pressNumber("0"); + repeat=false; + return; + } + if(source==jbuttons[24]) + { + pressNumber("1"); + repeat=false; + return; + } + if(source==jbuttons[25]) + { + pressNumber("2"); + repeat=false; + return; + } + if(source==jbuttons[4]) + { + double temp=1/Double.parseDouble(dispresult.getText()); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[5]) + { + double temp=Double.parseDouble(dispresult.getText())*Double.parseDouble(dispresult.getText()); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[6]) + { + + if(-Double.parseDouble(dispresult.getText())<0){ + double temp=Math.sqrt(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + }else{ + dispresult.setText("请输入有效数字"); + } + + } + if(source==jbuttons[7]) + { + + if(-Double.parseDouble(dispresult.getText())<0){ + double temp=Math.log10(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + }else{ + dispresult.setText("请输入有效数字"); + } + + } + if(source==jbuttons[8]) + { + double temp=Double.parseDouble(dispresult.getText())/100; + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[9]) + { + double temp=Math.sin(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[10]) + { + double temp=Math.cos(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[2]) + { + if(memoryArea.getText().equals(" M ")) + dispresult.setText(String.valueOf(memery)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[26]) + { + pressNumber("3"); + repeat=false; + return; + } + if(source==jbuttons[20]) + { + pressNumber("4"); + repeat=false; + return; + } + if(source==jbuttons[21]) + { + pressNumber("5"); + repeat=false; + return; + } + if(source==jbuttons[11]) + { + double temp=Math.tan(Double.parseDouble(dispresult.getText())); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[0]) + { + memery=Double.parseDouble(dispresult.getText()); + if(memery!=0)memoryArea.setText("M"); + prev=true; + repeat=false; + dot=true; + return; + } + if(source==jbuttons[22]) + { + pressNumber("6"); + repeat=false; + return; + } + + + if(source==jbuttons[16]) + { pressNumber("7"); + repeat=false; + return; + } + if(source==jbuttons[17]) + { pressNumber("8"); + repeat=false; + return;} + if(source==jbuttons[28]) + { double temp=-Double.parseDouble(dispresult.getText()); + dispresult.setText(String.valueOf(temp)); + prev=true; + repeat=false; + dot=true; + return;} + if(source==jbuttons[3]) + { memery+=-Double.parseDouble(dispresult.getText()); + if(memery!=0) memoryArea.setText("M"); + prev=true; + repeat=false; + dot=true; + return;} + if(source==jbuttons[18]) + { pressNumber("9"); + repeat=false; + return; + } + if(source==jbuttons[30]) + { if(dot){ + pressNumber("."); + dot=false; + repeat=false; + } + return; + } + + if(source==jbuttons[31]) + { + second=Double.parseDouble(dispresult.getText()); + dot=true; + switch(secondsymbol) + { + case'*': + second*=first; + break; + case'/': + if(divide(second)) return; + second=first/second; + }//end of switch(secondsymbol) + secondsymbol='\0'; + switch(firstsymbol) + { + case'+': + result+=second; + break; + case'-': + result-=second; + break; + case'*': + result*=second; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second)) return; + result/=second; + } + if(firstsymbol!='\0') dispresult.setText(String.valueOf(result)); + firstsymbol='\0'; + prev=true; + repeat=false; + return; + } + if(source==jbuttons[27]) + { + process.setText(process.getText()+"+"); + dot=true; + if(repeat){ + firstsymbol='+'; + return; + + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'*': + second*=first; + break; + case'/': + if(divide(second))return; + second=first/second; + } + secondsymbol='\0'; + switch(firstsymbol){ + case'\0': + result=second; + firstsymbol='+'; + break; + case'+': + result+=second; + dispresult.setText(String.valueOf(result)); + break; + case'-': + result-=second; + firstsymbol='+'; + dispresult.setText(String.valueOf(result)); + break; + case'*': + result*=second; + firstsymbol='+'; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + firstsymbol='+'; + dispresult.setText(String.valueOf(result)); + + } + prev=true; + repeat=true; + return; + } + if(source==jbuttons[23]) + { + process.setText(process.getText()+"-"); + dot=true; + if(repeat){ + firstsymbol='-'; + return; + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'*': + second*=first; + break; + case'/': + if(divide(second))return; + second=first/second; + + } + secondsymbol='\0'; + switch(firstsymbol){ + case'\0': + result=second; + firstsymbol='-'; + break; + case'+': + result=second; + firstsymbol='-'; + dispresult.setText(String.valueOf(result)); + break; + case'-': + result-=second; + dispresult.setText(String.valueOf(result)); + break; + case'*': + result*=second; + firstsymbol='-'; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + firstsymbol='-'; + dispresult.setText(String.valueOf(result)); + + } + prev=true; + repeat=true; + return; + } + if(source==jbuttons[19]) + { + process.setText(process.getText()+"*"); + dot=true; + if(repeat){ + if(secondsymbol=='\0')firstsymbol='*'; + else secondsymbol='*'; + return; + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'\0': + switch(firstsymbol){ + case'\0': + firstsymbol='*'; + result=second; + break; + case'+': + case'-': + first=second; + secondsymbol='*'; + break; + case'*': + result*=second; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + dispresult.setText(String.valueOf(result)); + firstsymbol='*'; + } + break; + case'*': + first*=second; + dispresult.setText(String.valueOf(first)); + break; + case'/': + if(divide(second))return; + first/=second; + secondsymbol='*'; + dispresult.setText(String.valueOf(first)); + } + prev=true; + repeat=true; + return; + } + if(source==jbuttons[15]) + { + process.setText(process.getText()+"/"); + dot=true; + if(repeat){ + if(secondsymbol=='\0')firstsymbol='/'; + else secondsymbol='/'; + return; + } + second=Double.parseDouble(dispresult.getText()); + switch(secondsymbol){ + case'\0': + switch(firstsymbol){ + case'\0': + firstsymbol='/'; + result=second; + break; + case'+': + case'-': + first=second; + secondsymbol='/'; + break; + case'*': + result*=second; + firstsymbol='/'; + dispresult.setText(String.valueOf(result)); + break; + case'/': + if(divide(second))return; + result/=second; + dispresult.setText(String.valueOf(result)); + } + break; + case'*': + first*=second; + secondsymbol='/'; + dispresult.setText(String.valueOf(first)); + break; + case'/': + if(divide(second))return; + first/=second; + dispresult.setText(String.valueOf(first)); + } + prev=true; + repeat=true; + return; + } + } + + public static void main(String args[]) + { + Calculator mc=new Calculator(); + } +} +