ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

用gui制作简易加法计算器

2021-09-17 22:05:08  阅读:221  来源: 互联网

标签:gui 计算器 text2 text3 add 加法 new TextField public


用gui制作简易加法计算器

本文用三种方式制作计算器。

原始方法

代码如下:

package com.cxf.gui.two;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator {
    public static void main(String[] args) {
        Cal cal = new Cal();
    }
}


class Cal extends Frame{
    public Cal(){   // create function
        TextField text1 = new TextField(10);
        Label label = new Label("+");
        TextField text2 = new TextField(10);
        Button eq = new Button("=");
        TextField text3 = new TextField(10);

        setLayout(new FlowLayout());
        add(text1);
        add(label);
        add(text2);
        add(eq);
        add(text3);
        setVisible(true);
        setLocation(400,400);
        pack();

        //listener
        eq.addActionListener(new MyListener(text1,text2,text3));
    }
}

class MyListener implements ActionListener{
    public TextField tt1,tt2,tt3;
    public MyListener(TextField t1,TextField t2,TextField t3){
        tt1 = t1;
        tt2 = t2;
        tt3 = t3;
    }
    public void actionPerformed(ActionEvent e){
        int result = Integer.parseInt(tt1.getText()) + Integer.parseInt(tt2.getText());
        tt3.setText(""+result); //when text3 goes to tt3, tt3 changing results in text3 changing,they are the same thing
        tt1.setText("");
        tt2.setText("");
    }
}

输出结果:

加号左右的方框中输入数字,点击等号后,等号右侧方框显示两数字相加结果。

上面的代码有三个类,分别用作主函数,界面,和监听事件。

界面调用监听事件,主函数调用界面。

用组合类的方法

代码:

package com.cxf.gui.three;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator {
    public static void main(String[] args) {
        Cal cal = new Cal();
        cal.LoadCal();
    }
}


class Cal extends Frame {

    public TextField text1,text2,text3;

    public void LoadCal(){   // create function
        text1 = new TextField(10);
        Label label = new Label("+");
        text2 = new TextField(10);
        Button eq = new Button("=");
        text3 = new TextField(10);

        setLayout(new FlowLayout());
        add(text1);
        add(label);
        add(text2);
        add(eq);
        add(text3);
        setVisible(true);
        setLocation(400,400);
        pack();

        //listener
        eq.addActionListener(new MyListener(this));
    }
}

class MyListener implements ActionListener {
    public Cal cal1;
    public MyListener(Cal cal){ //combining another class ,which means example as a variable.
        cal1 = cal;
    }
    public void actionPerformed(ActionEvent e){
        int result = Integer.parseInt(cal1.text1.getText()) + Integer.parseInt(cal1.text2.getText());
        cal1.text3.setText("" + result); //when text3 goes to tt3, tt3 changing results in text3 changing,they are the same thing.
        cal1.text2.setText("");
        cal1.text1.setText("");
    }
}

输出结果与原始方法相同。

这个方法与原始方法的不同之处在于,原始方法把界面的属性传递给监听事件,这个方法把界面对象传递给了监听事件。

这个方法与原始方法相比,更富含类的思想。

用内部类的方法

代码如下:

package com.cxf.gui.four;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator {
    public static void main(String[] args) {
        Cal cal = new Cal();
        cal.LoadCal();
    }
}

class Cal extends Frame {

    public TextField text1,text2,text3;

    public void LoadCal(){   // create function
        text1 = new TextField(10);
        Label label = new Label("+");
        text2 = new TextField(10);
        Button eq = new Button("=");
        text3 = new TextField(10);

        setLayout(new FlowLayout());
        add(text1);
        add(label);
        add(text2);
        add(eq);
        add(text3);
        setVisible(true);
        setLocation(400,400);
        pack();

        //listener
        eq.addActionListener(new MyListener());
    }
    private class MyListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
            int result = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText());
            text3.setText("" + result); //when text3 goes to tt3, tt3 changing results in text3 changing,they are the same thing.
            text2.setText("");
            text1.setText("");
        }
    }
}

输出结果与前两个方法相同。

这个方法中,监听事件成为了界面的内部类,这让监听事件更容易获取界面的信息。

标签:gui,计算器,text2,text3,add,加法,new,TextField,public
来源: https://www.cnblogs.com/cxf-tech/p/15306445.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有