zoukankan      html  css  js  c++  java
  • 计应193第一组个人流程——任远航

    package 软件工程;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import java.awt.GridLayout;
    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.JButton;
    import javax.swing.BoxLayout;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.FlowLayout;
    import java.awt.CardLayout;
    import com.jgoodies.forms.layout.FormLayout;
    import com.jgoodies.forms.layout.ColumnSpec;
    import com.jgoodies.forms.layout.RowSpec;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import net.miginfocom.swing.MigLayout;
    import javax.swing.border.LineBorder;
    import javax.swing.SwingConstants;
    
    public class Calculator extends JFrame {
    
        private JPanel contentPane;
        private JTextField numberField;
        private JPanel panel;
        private JPanel panel_1;
        private JButton one;
        private JButton seven;
        private JButton subtraction;
        private JButton two;
        private JButton eight;
        private JButton four;
        private JButton nine;
        private JButton six;
        private JButton three;
        private JButton five;
        private JPanel panel_2;
        private JButton multiplication;
        private JButton sum;
        private JButton as;
        private JButton division;
        private String result="";
        private JButton Clear;
        private JButton zero;
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Calculator frame = new Calculator();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public Calculator() {
            
            this.setResizable(false);
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 600);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(new BorderLayout(0, 0));
            
            //******************************************这个地方放显示的数据一个Panel里面装的JText
            JPanel Top = new JPanel();                //**
            Top.setPreferredSize(new Dimension(450, 100));
            contentPane.add(Top, BorderLayout.NORTH);
            Top.setLayout(new GridLayout(0, 1, 0, 0));
                                                    //**
            numberField = new JTextField();
            numberField.setFont(new Font("幼圆", Font.BOLD, 30));
            numberField.setBackground(Color.LIGHT_GRAY);
            numberField.setText("0");//
            Top.add(numberField);                    //**
            numberField.setColumns(10);
            
            numberField.setEditable(false);//不可编辑
            //监听器
            ActionListener click=null;
            click=new ActionListener() {
                
                @Override            
                public void actionPerformed(ActionEvent e) {            
                    // TODO Auto-generated method stub
                    //当输入一个数时,计算器上面的0消失        fi(FIrsInput)第一个数字
                    String fi=numberField.getText().valueOf(0);
                    System.out.println(fi);
                    if(numberField.getText().equals("0")){numberField.setText(numberField.getText().replaceFirst("0", ""));}
                    //@param operate是判断当时的按钮是什么
                    String  operate=e.paramString().substring(21, 22);
                    //考虑到如果先输入+-*/,这样会报错,不能让你输入
                    if(numberField.getText().equals("")&&(operate.equals("+")||operate.equals("-")||operate.equals("x")||operate.equals("/"))) {
                        numberField.setText("");
                        System.out.println(fi);}
                    else {    //如果按的是操作符+-*/
                            if(operate.equals("/")||operate.equals("+")||operate.equals("-")||operate.equals("x")) 
                            {    
                                if(numberField.getText().matches("\d*([x]|[/]|[-]|[+])\d*"))
                                {
                                    numberField.setText(""+cal());
                                }
                                else {
                                if(!(numberField.getText().endsWith("x")||numberField.getText().endsWith("-")||numberField.getText().endsWith("+")||numberField.getText().endsWith("/"))) 
                                {
                                    numberField.setText(numberField.getText()+operate);
                                }
                                }
                            }
                            else//这是按了其他数字键
                            {
                                int number=Integer.parseInt(operate);
                                numberField.setText(numberField.getText()+number);
                            }
                        }
                }
            };
        
            panel = new JPanel();
            panel.setPreferredSize(new Dimension(320, 250));
            contentPane.add(panel, BorderLayout.WEST);
            
            two = new JButton("2");
            two.addActionListener(click);
            two.setBorder(new LineBorder(new Color(0, 0, 0)));
            two.setMargin(new Insets(10, 14, 2, 14));
            two.setFont(new Font("幼圆", Font.PLAIN, 30));
            two.setPreferredSize(new Dimension(100, 115));
            
            three = new JButton("3");
            three.addActionListener(click);
            three.setBorder(new LineBorder(new Color(0, 0, 0)));
            three.setMargin(new Insets(10, 14, 2, 14));
            three.setFont(new Font("幼圆", Font.PLAIN, 30));
            three.setPreferredSize(new Dimension(100, 115));
            
            four = new JButton("4");
            four.addActionListener(click);
            four.setBorder(new LineBorder(new Color(0, 0, 0)));
            four.setMargin(new Insets(10, 14, 2, 14));
            four.setFont(new Font("幼圆", Font.PLAIN, 30));
            four.setPreferredSize(new Dimension(100, 115));
            
            five = new JButton("5");
            five.addActionListener(click);
            five.setBorder(new LineBorder(new Color(0, 0, 0)));
            five.setMargin(new Insets(10, 14, 2, 14));
            five.setFont(new Font("幼圆", Font.PLAIN, 30));
            five.setPreferredSize(new Dimension(100, 115));
            
            six = new JButton("6");
            six.addActionListener(click);
            six.setBorder(new LineBorder(new Color(0, 0, 0)));
            six.setMargin(new Insets(10, 14, 2, 14));
            six.setFont(new Font("幼圆", Font.PLAIN, 30));
            six.setPreferredSize(new Dimension(100, 115));
            
            seven = new JButton("7");
            seven.addActionListener(click);
            seven.setBorder(new LineBorder(new Color(0, 0, 0)));
            seven.setMargin(new Insets(10, 14, 2, 14));
            seven.setFont(new Font("幼圆", Font.PLAIN, 30));
            seven.setPreferredSize(new Dimension(100, 115));
            
            eight = new JButton("8");
            eight.addActionListener(click);
            eight.setBorder(new LineBorder(new Color(0, 0, 0)));
            eight.setMargin(new Insets(10, 14, 2, 14));
            eight.setFont(new Font("幼圆", Font.PLAIN, 30));
            eight.setPreferredSize(new Dimension(100, 115));
            
            nine = new JButton("9");
            nine.addActionListener(click);
            nine.setBorder(new LineBorder(new Color(0, 0, 0)));
            nine.setMargin(new Insets(10, 14, 2, 14));
            nine.setFont(new Font("幼圆", Font.PLAIN, 30));
            nine.setPreferredSize(new Dimension(100, 115));
            panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
            
            one = new JButton("1");
            //**************
            one.addActionListener(click);
            one.setBorder(new LineBorder(new Color(0, 0, 0)));
            one.setMargin(new Insets(10, 14, 2, 14));
            one.setFont(new Font("幼圆", Font.PLAIN, 30));
            one.setPreferredSize(new Dimension(100, 115));
            panel.add(one);
            panel.add(two);
            panel.add(three);
            panel.add(four);
            panel.add(five);
            panel.add(six);
            panel.add(seven);
            panel.add(eight);
            panel.add(nine);
            
            panel_1 = new JPanel();
            contentPane.add(panel_1, BorderLayout.EAST);
            panel_1.setLayout(new BorderLayout(0, 0));
            
            subtraction = new JButton("-");
            subtraction.addActionListener(click);
            subtraction.setFont(new Font("幼圆", Font.BOLD, 30));
            subtraction.setPreferredSize(new Dimension(90, 126));
            panel_1.add(subtraction, BorderLayout.NORTH);
            
            sum = new JButton("+");
            sum.addActionListener(click);
            sum.setFont(new Font("幼圆", Font.BOLD, 30));
            sum.setPreferredSize(new Dimension(90, 100));
            panel_1.add(sum, BorderLayout.SOUTH);
            
            Clear = new JButton("C");
            Clear.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    numberField.setText("");
                }
            });
            Clear.setPreferredSize(new Dimension(90, 126));
            panel_1.add(Clear, BorderLayout.WEST);
            
            panel_2 = new JPanel();
            panel_2.setPreferredSize(new Dimension(10, 80));
            contentPane.add(panel_2, BorderLayout.SOUTH);
            panel_2.setLayout(new BorderLayout(0, 0));
            
            multiplication = new JButton("x");
            multiplication.addActionListener(click);
            multiplication.setFont(new Font("幼圆", Font.BOLD, 30));
            multiplication.setMargin(new Insets(2, 0, 2, 50));
            multiplication.setPreferredSize(new Dimension(150, 100));
            panel_2.add(multiplication, BorderLayout.WEST);
            
            as = new JButton("=");
            as.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    numberField.setText(""+cal());
                }
            });
            
            as.setFont(new Font("幼圆", Font.BOLD, 30));
            as.setPreferredSize(new Dimension(150, 100));
            panel_2.add(as, BorderLayout.EAST);
            
            division = new JButton("/");
            division.addActionListener(click);
            division.setFont(new Font("幼圆", Font.BOLD, 30));
            division.setPreferredSize(new Dimension(150, 100));
            panel_2.add(division, BorderLayout.CENTER);
            
            zero = new JButton("0");
            zero.addActionListener(click);
            zero.setFont(new Font("宋体", Font.PLAIN, 20));
            panel_2.add(zero, BorderLayout.NORTH);
            //******************************************
            
        
    
        }
        
        public int sum(String zhi) 
        {
            this.result=result+zhi;
            
            return Integer.parseInt(result);
        }
        //运算的方法,运算;;;;
        public int cal() {
            //如果为空直接返回了
            String patt= "\d*([x]|[-]|[+]|[/])\d*";
            if(!numberField.getText().matches(patt)) {return 0;}
                                                   
            //加减乘除
            String zhi=numberField.getText();
            if        (zhi.contains("x")) {
                String head=zhi.split("x")[0];
                String foot=zhi.split("x")[1];
                //返回乘法
                return p(head)*p(foot);    
            }else if(zhi.contains("/")) {
                String head=zhi.split("/")[0];
                String foot=zhi.split("/")[1];
                //返回触发
                return p(head)/p(foot);
            
            }else if(zhi.contains("+")) {
                String head=zhi.split("[+]")[0];
                String foot=zhi.split("[+]")[1];
                
                //返回假发
                return (p(head))+(p(foot));
            }else if(zhi.contains("-")) {
                String head=zhi.split("-")[0];
                String foot=zhi.split("-")[1];
                //返回减法
                return p(head)-p(foot);
            }
            
            
            return 0;
            
    //        System.out.println(zhi.indexOf("x"));
    //        System.out.println("head:"+head);
    //        System.out.println("foot:"+foot);
    //        return parseInt(head)*parseInt(foot);
        }
        //parseInt磨成int类型
        public int p(String str) 
        {
            return Integer.parseInt(str);    
        }
    }
  • 相关阅读:
    待思考问题---数据库的分层模型
    iOS的常用类库
    java spring是元编程框架---使用的机制是注解+配置
    Java Virtual Machine (JVM), Difference JDK, JRE & JVM – Core Java
    Spring注解的使用和组件扫描
    Spring Boot实战:拦截器与过滤器
    问题、存在与认知---问题产生的原因是对存在的不完全或错误认知
    元编程的分层模型
    元编程的本质---高级语言的表达通过解释生成低级语言的表达(代码)
    aop分层模型——aop是元编程的一种
  • 原文地址:https://www.cnblogs.com/bladepoint/p/14645184.html
Copyright © 2011-2022 走看看