• 第8.9次作业


    个人信息2:

    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("20163311121",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 计算器();
        }
    }

  • 相关阅读:
    对模拟器虚假设备识别能力提升15%!每日清理大师App集成系统完整性检测
    教你实现华为快应用深色主题适配
    如何实现一个malloc
    如何提高团队管理能力?
    大型网站架构演变和知识体系
    call_user_func 和 call_user_func_array用法
    源码研究:php变量
    http协议简介
    用c语言实现http请求
    用socket写一个简单的客户端和服务端程序
  • 原文地址:https://www.cnblogs.com/aa2178d2/p/9230663.html
Copyright © 2020-2023  润新知