zoukankan      html  css  js  c++  java
  • GUI实现超简单的计算器

    计算器样式

    在这里插入图片描述

    实现代码

    //实现超简易的计算器
    public class Test02 {
        public static void main(String[] args) {
            Counter counter = new Counter();
        }
    }
    
    
    //计算器类
    class Counter extends Frame{
        public Counter() {
            super("简易计算器");
            this.setLayout(new FlowLayout(FlowLayout.CENTER));
            //3个文本框
            TextField t1 = new TextField(5);
            TextField t2 = new TextField(5);
            TextField t3 = new TextField(10);
    
            //1个标签
            Label label = new Label("+");
    
            //1个按钮
            Button button = new Button("=");
    
            //给按钮添加监听
            button.addActionListener(new myCounterListener(t1,t2,t3));
    
            this.add(t1);
            this.add(label);
            this.add(t2);
            this.add(button);
            this.add(t3);
            this.pack();
            this.setVisible(true);
        }
    }
    
    //监听类
    class myCounterListener implements ActionListener{
        TextField t1;
        TextField t2;
        TextField t3;
    
        public myCounterListener(TextField t1, TextField t2, TextField t3) {
            this.t1 = t1;
            this.t2 = t2;
            this.t3 = t3;
        }
    
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            //1.获取加数和被加数
            int left = Integer.parseInt(t1.getText());
            int right = Integer.parseInt(t2.getText());
    
            //2.两数相加,和传给第三个框
            t3.setText(""+(left+right));
    
            //3.清除前两个框
            t1.setText("");
            t2.setText("");
    
            System.out.println("计算结束");
        }
    }
    

    功能实现,不过用的是面向过程编程,好像太丑了a…

    重构!

    //面向对象+内部类思想重构
    public class Test02Up {
        public static void main(String[] args) {
            new CounterUp().loadFrame();
        }
    }
    
    
    //计算器类
    class CounterUp extends Frame {
        //属性
        TextField t1;
        TextField t2;
        TextField t3;
    
        //方法
        void loadFrame() {
            setTitle("简易计算器");
            t1 = new TextField(5);
            t2 = new TextField(5);
            t3 = new TextField(10);
            Label label = new Label("+");
            Button button = new Button("=");
    
            //给按钮添加监听
            button.addActionListener(new myCounterListenerUp());
    
            //布局
            setLayout(new FlowLayout(FlowLayout.CENTER));
            add(t1);
            add(label);
            add(t2);
            add(button);
            add(t3);
    
            pack();
            setVisible(true);
        }
    
        //监听类
        //内部类的优点,可以直接访问外部类的属性
        class myCounterListenerUp implements ActionListener {
    //        /*获取计算器这个对象,在一个类中组合另外一个类(和继承功能类似)*/
    //        CounterUp counterUp = null;
    //
    //        public myCounterListenerUp(CounterUp counterUp) {   /*直接传入对象,操作对象的属性*/
    //            this.counterUp = counterUp;
    //        }
    
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //1.获取加数和被加数
                //2.两数相加,和传给第三个框
                //3.清除前两个框
                int left = Integer.parseInt(t1.getText());
                int right = Integer.parseInt(t2.getText());
                t3.setText("" + (left + right));
                t1.setText("");
                t2.setText("");
    
                System.out.println("计算结束");
            }
        }
    }
    

    嗯,比之前好看多了@~@

    总结:

    1. String类型要用 **Integer.parseInt()**方法转int型,相反,则用 “”+int 转String型。
    2. 面向对象编程强调对象的属性和方法,操作时,应尽量调用对象本身,再通过对象调用属性和方法。
    3. 内部类的优点,可以畅通无阻的访问外部类的属性,所以建议尽量使用内部类。
  • 相关阅读:
    现代软件工程 第一章 概论 第3题——韩婧
    现代软件工程 第一章 概论 第2题——韩婧
    小组成员邓琨、白文俊、张星星、韩婧
    UVa 10892 LCM的个数 (GCD和LCM 质因数分解)
    UVa 10780 幂和阶乘 求n!中某个因子的个数
    UVa 11859 除法游戏(Nim游戏,质因子)
    Codeforces 703C Chris and Road 二分、思考
    Codeforces 703D Mishka and Interesting sum 树状数组
    hdu 5795 A Simple Nim SG函数(多校)
    hdu 5793 A Boring Question 推公式(多校)
  • 原文地址:https://www.cnblogs.com/shimmernight/p/13441718.html
Copyright © 2011-2022 走看看