diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..b7796cc7a51655686ba2b431d4060ec64736ac13 --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,281 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.text.DecimalFormat; +import java.math.BigInteger; +import java.math.BigDecimal; + + +public class Calculator extends JFrame implements ActionListener{ + private JPanel cal_north =new JPanel();//北面布局 + private JPanel cal_center =new JPanel();//中间布局 + private JPanel cal_south =new JPanel();//南面布局 + private JTextField cal_input =new JTextField("0");//输入框(中 + private JTextField cal_process =new JTextField();//过程框(北 + private JMenuBar Menu=new JMenuBar(); + public Calculator() throws HeadlessException { + this.win(); + this.win_NORTH(); + this.win_CENTER(); + this.win_Button(); + this.menu(); + + } + + //创建菜单 + public void menu(){ + JMenu btn1=new JMenu("查看"); + JMenu btn2=new JMenu("编辑"); + JMenu btn3=new JMenu("帮助"); + JMenu btn4=new JMenu("更多"); + JMenu btn5=new JMenu("历史"); + Menu.add(btn1); + Menu.add(btn2); + Menu.add(btn3); + Menu.add(btn4); + Menu.add(btn5); + Menu.setBackground(Color.LIGHT_GRAY); + Menu.setBorder(BorderFactory.createMatteBorder(0, 3, 0, 3, Color.LIGHT_GRAY)); + this.setJMenuBar(Menu); + } + + //创建窗体 + public void win() { + this.setTitle("计算器"); + this.setSize(300,450); + this.setBackground(Color.LIGHT_GRAY); + this.setLayout(new BorderLayout()); + this.setResizable(false);//不可拉伸 + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null);//使窗体居中 + this.cal_input.setHorizontalAlignment(JTextField.RIGHT);//输入内容右对齐 + this.cal_process.setHorizontalAlignment(JTextField.RIGHT);//输入内容右对齐 + } + + //添加输入框 + public void win_CENTER() { + cal_center.setLayout(new BorderLayout()); + cal_center.add(cal_input);//把输入框加入中间布局 + cal_center.setSize(280, 50); + this.add(cal_center,BorderLayout.CENTER); + cal_input.setBackground(Color.LIGHT_GRAY); + cal_input.setFont(new Font("黑体",Font.BOLD,50)); + cal_input.setBorder(null); //删除输入框的边框 + cal_input.setEditable(false); //不能修改结果文本框 + cal_center.setBorder(BorderFactory.createMatteBorder(5, 3, 0, 3, Color.LIGHT_GRAY)); + } + + public void win_NORTH() { + cal_north.setLayout(new BorderLayout()); + cal_north.add(cal_process);//把过程框加入北面布局 + Dimension process_size=new Dimension(280,30);//设置过程框大小 + cal_process.setPreferredSize(process_size);//将大小应用至输入框 + this.add(cal_north,BorderLayout.NORTH); + cal_process.setBackground(Color.LIGHT_GRAY); + cal_process.setFont(new Font("黑体",Font.BOLD,30)); + cal_process.setBorder(null); //删除输入框的边框 + cal_process.setEditable(false); //不能修改结果文本框 + cal_north.setBorder(BorderFactory.createMatteBorder(5, 3, 5, 3, Color.LIGHT_GRAY)); + } + + //添加按钮 + public void win_Button() { + String [] Btns= {"%","CE","C","←","n!","e^x","cos","sin","1/x","x^2","√x","÷","7","8","9","×","4","5","6","-","1","2","3","+","±","0",".","="}; + String num="[1234567890]"; + String sigh="[\\+\\-\\×\\÷]"; + String reset="[C←]"; + this.cal_south.setBackground(Color.LIGHT_GRAY); + this.cal_south.setLayout(new GridLayout(7,4,4,4));//设置按键个数及大小 + cal_south.setBorder(BorderFactory.createMatteBorder(0, 4, 5, 4, Color.LIGHT_GRAY)); + for(int i=0;i<28;i++) { + JButton btn=new JButton(); + btn.setText(Btns[i]); + btn.setBackground(new Color(232, 232, 232)); + btn.setFont(new Font("楷体",Font.BOLD,15)); + btn.setBorderPainted(false); + if(Btns[i].matches(num)) { + btn.setBackground(new Color(245,245,245)); + btn.setFont(new Font("黑体",Font.BOLD,20));//把数字加粗放大 + } + if(Btns[i]=="±"||Btns[i]==".") { + btn.setBackground(new Color(245,245,245)); + } + else if(Btns[i].matches(sigh)) { + btn.setBackground(Color.GRAY); + btn.setForeground(Color.white); + } + else if(Btns[i].matches(reset)) { + btn.setBackground(Color.ORANGE); + btn.setForeground(Color.WHITE); + } + else if(Btns[i]=="=") { + btn.setBackground(Color.BLACK); + btn.setForeground(Color.WHITE); + } + btn.addActionListener(this); + cal_south.add(btn); + } + this.add(cal_south,BorderLayout.SOUTH); + } + + public static void main(String[] args) { + Calculator calculator=new Calculator(); + calculator.setVisible(true); + } + + //为按键添加功能 + private String firstInput=null;//中间过程 + private String operation=null;//运算符 + private boolean judge= true;//判断 + private double result = 0; + private String result_string=null; + @Override + public void actionPerformed(ActionEvent e) { + String input=e.getActionCommand(); + String store=cal_input.getText(); + + //处理数字 + if("0123456789".indexOf(input)!=-1) { + if(judge) { + cal_input.setText(null); + cal_input.setText(input); + } + else { + this.cal_input.setText(store+input);//可从左至右输入数据 + } + judge=false; + } + + //处理. + else if((input.equals("."))&&(store.indexOf(".")==-1)) { + cal_input.setText(store+ "."); + } + + + //处理CE、C、← + else if(input.equals("CE")) { + cal_input.setText("0"); + judge=true; + } + else if(input.equals("C")) { + cal_input.setText("0"); + cal_process.setText(null); + judge=true; + } + else if(input.equals("←")) { + if(store.length()>0) { + store=store.substring(0,store.length()-1); + if (store.length() == 0) { + cal_input.setText("0"); + judge=true; + } + else { + cal_input.setText(store); + } + } + } + + //处理%、±、n!,e^x,cos,sin,1/x,x^2,√x + else if(input.equals("%")||input.equals("±")||input.equals("n!")||input.equals("e^x")||input.equals("cos")||input.equals("sin")||input.equals("1/x")||input.equals("x^2")||input.equals("√x")){ + result=(Double.valueOf(cal_input.getText()).doubleValue()); + cal_input.setFont(new Font("黑体",Font.BOLD,25)); + if(input.equals("%")){ + result=result*0.01; + } + else if(input.equals("±")){ + result=result*(-1); + } + else if(input.equals("n!")) { + int factorial=1; + for (int i=1;i<=result; i++) { + factorial=factorial*i; + } + result=factorial; + cal_process.setText(store+"!"+"="); + } + else if(input.equals("e^x")) { + result=Math.pow(Math.E, result); + cal_process.setText("e^"+store+"="); + } + else if(input.equals("cos")) { + result=Math.cos(result); + cal_process.setText("cos"+store+"="); + } + else if(input.equals("sin")) { + result=Math.sin(result); + cal_process.setText("sin"+store+"="); + } + else if(input.equals("x^2")) { + result=result*result; + cal_process.setText(store+"^2="); + } + else if(input.equals("√x")) { + result=Math.sqrt(result); + cal_process.setText("√"+store+"="); + } + else if(input.equals("1/x")) { + if (result==0) { + cal_input.setText("除数不能为零"); + } + else { + result= 1/result; + cal_process.setText("1/("+cal_input.getText()+")="); + } + } + if((input.equals("1/x"))&&(result==0)) { + cal_input.setText("除数不能为零"); + } + else { + result_string=Double.toString(result); + cal_input.setText(result_string); + } + judge=true; + } + + //处理+-×÷ + else if(input.matches("[\\+\\-\\×\\÷]{1}")){ //{1}是其中一个的意思 + operation=input; + firstInput=this.cal_input.getText(); + this.cal_input.setText(""); + if (input.matches("[\\+\\-\\×\\÷]{1}")){ + cal_process.setText(store+input); + } + } + else if (input.equals("=")) { + double a=Double.valueOf(firstInput); + double b=Double.valueOf(this.cal_input.getText()); + switch(operation) { + case"+": + result=a+b; + break; + case"-": + result=a-b; + break; + case"×": + result=a*b; + break; + case"÷": + if(b!=0) + result=a/b; + break; + } + cal_process.setText(firstInput+operation+this.cal_input.getText()+"="); + if((operation=="÷")&&(b==0)) { + cal_input.setText("除数不能为0"); + judge=true; + } + else { + result_string=Double.toString(result); + cal_input.setText(result_string); + judge=true; + } + + } +} + +} + +