zoukankan      html  css  js  c++  java
  • 个人信息与计算器

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Date;
    import javax.swing.*;
    public class 个人信息{
        public static void main(String arg[])
        {
            Frame f=new Frame("个人信息");
            f.setSize(250,300);
            f.setLocation(300,300);
            f.setBackground(Color.lightGray);
            f.setLayout(new FlowLayout(2));
            f.add(new JLabel("姓名:"));
            f.add(new TextField("关菁森",20));
            f.add(new JLabel("班级:"));
            f.add(new TextField("计算机科学与技术16(D)",20));
            f.add(new JLabel("学号:"));
            f.add(new TextField("20163311127",20));
            f.add(new JLabel("性别:"));
            f.add(new TextField("男",20));
            JButton button1=new JButton("OK");
            JButton button2=new JButton("CLOSE");
            f.add(button1);
            f.add(button2);
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {        //OK按钮事件
                    JOptionPane.showMessageDialog(null,"你是谁,我是谁?","提示",JOptionPane.PLAIN_MESSAGE) ;     
                }
            });
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {        //CLOSE按钮事件
                    long d=e.getWhen();  //事件发生的时间
                    Date date=new Date(d);   //转换为相应的时间
                    System.out.println(date);
                    System.exit(0);
                }
            });
            f.setVisible(true);
        }
    }

    计算器:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Stack;   //Stack是一个后进先出的堆栈

    public class 计算器 extends JFrame implements ActionListener {
        String []An= {"7","8","9","+","4","5","6","-",
                "1","2","3","*","0",".","=","/"};
        JButton b[]=new JButton[16];
        JTextField jt;
        String input="";      //输入的字符串
        public 计算器() {
            Container P=getContentPane();     //用getContentPane()方法获得JFrame的内容面板
            JPanel jp1=new JPanel();
            JPanel jp2=new JPanel();
            JPanel jp3=new JPanel();
            JButton b1=new JButton("清零");
            JButton b2=new JButton("退格");
            GridLayout g=new GridLayout(4,4,3,3);   //4*4网格,间距3
            jp1.setLayout(new BorderLayout());     //边布局
            jp2.setLayout(g);
            jp3.setLayout(new GridLayout(1, 2,3,3));

            jt=new JTextField();
            jt.setPreferredSize(new Dimension(160,30));   //文本大小
            jt.setHorizontalAlignment(SwingConstants.LEFT);  //左对齐
            P.add(jp1,BorderLayout.NORTH);  //北
            jp1.add(jt,BorderLayout.WEST);  //西
            jp1.add(jp3,BorderLayout.EAST);   //东
            jp3.add(b1);
            jp3.add(b2);
            b1.setBackground(Color.lightGray);
            b2.setBackground(Color.lightGray);
            b1.addActionListener(this);     //定义处理事件的方法
            b2.addActionListener(this);

            for(int i=0;i<16;i++)    //添加按钮
            {
                b[i]=new JButton(An[i]);
                jp2.add(b[i]);
                b[i].addActionListener(this);
            }
            P.add(jp2,BorderLayout.CENTER);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(300, 250);
            setTitle("计算器");
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            int t=0;
            String s=e.getActionCommand();
            if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) {
                input+=" "+s+" ";        //如果碰到运算符,就在运算符前后分别加一个空格
            }else if(s.equals("清零")) {
                input="";
            }else if(s.equals("退格")) {
                if((input.charAt(input.length()-1))==' ')     //检测字符串的最后一个字符是否为空格
                {
                    input=input.substring(0,input.length()-3);     //如果是则删除末尾3个字符
                }
                else      //否则删除1个字符
                    {
                    input=input.substring(0,input.length()-1);
                    }
            }
            else if(s.equals("=")) {
                input=compute(input);
                jt.setText(input);
                input="";
                t=1;
            }
            else
                input += s;
            if(t==0) {
                jt.setText(input);
            }
        }
        private String compute(String str) {
            String array[];
            array=str.split(" ");
            Stack<Double> s=new Stack<Double>();
            Double a=Double.parseDouble(array[0]);
            s.push(a);
            for(int i=1;i<array.length;i++) {
                if(i%2==1) {
                    if(array[i].compareTo("+")==0)
                    {
                        double b= Double.parseDouble(array[i+1]);
                        s.push(b);
                    }
                    if(array[i].compareTo("-")==0)
                    {
                        double b= Double.parseDouble(array[i+1]);
                        s.push(-b);
                    }
                    if(array[i].compareTo("*")==0)
                    {
                        double b= Double.parseDouble(array[i+1]);
                        double c=s.pop();
                        c*=b;
                        s.push(c);
                    }
                    if(array[i].compareTo("/")==0)
                    {
                        double b= Double.parseDouble(array[i+1]);
                        double c=s.peek();
                        s.pop();
                        c/=b;
                        s.push(c);
                    }
                }
            }
            double sum=0;
            while(!s.isEmpty()) {
                sum+=s.pop();
            }
            String result=String.valueOf(sum);
            return result;
        }
        public static void main(String[] args) {
            new 计算器();
        }
    }

  • 相关阅读:
    团队法则100条
    How To Install and Use Redis
    李元芳履职梗概
    免费私有gitLab服务推荐
    Tornado、Bottle以及Flask
    Top Open Source Projects to Watch in 2017
    openmediavault 4.1.3 插件开发
    前端面试绝对会考的JS问题!【已经开源】
    使用window.open打开新窗口被浏览器拦截的解决方案
    微信小程序的坑之wx.miniProgram.postMessage
  • 原文地址:https://www.cnblogs.com/maskman/p/9231399.html
Copyright © 2011-2022 走看看