Java简易计算器

news/2024/7/4 23:08:47 标签: java, button, import, applet, div, object
<div id="article_content" class="article_content clearfix"> <div id="content_views" class="htmledit_views">

 

<div class="ns_content"> 只实现了加功能。
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class Calculator extends Applet implements ActionListener{
    Button[] bu_array =  new Button[10];
    TextField result = new TextField(30);
    private int before ;        
    Button add = new Button("+");
    Button equal = new Button("=");
    
    public void init(){
        Panel p_text = new Panel();
        Panel p_num  = new Panel();
        Panel p_calc = new Panel();

        this.setLayout(new BorderLayout());
        this.add(p_text, BorderLayout.NORTH);
        this.add(p_num, BorderLayout.CENTER);
        this.add(p_calc, BorderLayout.EAST);
        
        p_text.add(result);
    
        p_num.setLayout(new GridLayout(4,3));
        
        
        for(int i=0; i<bu_array.length; i++){
            bu_array[i] =new Button(Integer.toString(i));
            p_num.add(bu_array[i]);
            bu_array[i].addActionListener(this);
        }
        
        p_calc.setLayout(new GridLayout(4,2));
    
        Button sub = new Button("-");
        Button mul = new Button("*");
        Button div = new Button("/");
        Button mod = new Button("%");
        Button clear = new Button("C");
        
        p_calc.add(add);
        p_calc.add(sub);
        p_calc.add(mul);
        p_calc.add(div);
        p_calc.add(mod);    
        p_calc.add(equal);
        p_calc.add(clear);        

        add.addActionListener(this);
        sub.addActionListener(this);
        mul.addActionListener(this);
        div.addActionListener(this);
        mod.addActionListener(this);
        equal.addActionListener(this);
        clear.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e){
        Object source =    e.getSource();
        
        if(source.equals(add)){
            this.before = Integer.parseInt(result.getText());
            result.setText("");
        }

        if(source.equals(equal)){
            result.setText(Integer.toString((this.before + Integer.parseInt(result.getText()))));
        }
        
        for(int i=0; i<bu_array.length; i++){
            if(source.equals(bu_array[i])){
                result.setText(result.getText()+Integer.toString(i));
            }
        }
        
    }

    public static void main(String [] args){
    }
}

<html>
<head></head>
<body>
<applet height="200" width="200" code="Calculator.class"></applet>
</body>
</html>
div> div> div> <div id="treeSkill">div>

http://www.niftyadmin.cn/n/1051251.html

相关文章

skip-grant-tables:非常有用的mysql启动参数

介绍一个非常有用的mysql启动参数—— --skip-grant-tables。顾名思义&#xff0c;就是在启动mysql时不启动grant-tables&#xff0c;授权表。有什么用呢&#xff1f;当然是忘记管理员密码后有用。 操作方法&#xff1a; 1、杀掉原来进行着的mysql&#xff1a; rcmy…

JAVA随笔篇一(Timer源码分析和scheduleAtFixedRate的使用)

写完了基础篇&#xff0c;想了很久要不要去写进阶篇&#xff0c;去写JSP等等的使用方法&#xff0c;最后决定先不去写&#xff0c;因为自己并不是JAVA方面的大牛&#xff0c;目前也在边做边学&#xff0c;所以决定先将自己不懂的拿出来学并记下来。 Timer是Java自带的java.uti…

js中Number()、parseInt()和parseFloat()的区别

一&#xff1a;Number() 如果是Boolean值&#xff0c;true和false值将分别被转换为1和0。 如果是数字值&#xff0c;只是简单的传入和返回。 如果是null值&#xff0c;返回0。 如果是undefined&#xff0c;返回NaN。 如果是字符串&#xff1a; a. 如果字符串中只包含数字时&am…

equals==的使用

package stringyiwen; /* * :比较运算符&#xff0c;在基本数据类型比较的是值* &#xff1a;引用数据类型比较的是地址值 *//* * equals方法&#xff1a;【只】用于【引用数据数据类型】&#xff0c;如果对象没有继承Object类中的equals方法 * equals方法和 " "…

JAVA随笔篇二(深入分析JAVA简单类型、String和对象的值传递和引用传递)

关于JAVA的值传递和引用传递&#xff0c;翻看了很多资料和博客&#xff0c;感觉大多数讲的很乱&#xff0c;都是自己明白了之后就不讲了的样子&#xff0c;终于算是比较理解这几个概念了&#xff0c;下面做一个总结。 1、简单类型的参数传递 Java方法的参数是简单类型的时候&a…

Redis 和 I/O 多路复用

最近在看 UNIX 网络编程并研究了一下 Redis 的实现&#xff0c;感觉 Redis 的源代码十分适合阅读和分析&#xff0c;其中 I/O 多路复用&#xff08;mutiplexing&#xff09;部分的实现非常干净和优雅&#xff0c;在这里想对这部分的内容进行简单的整理。 几种 I/O 模型 为什么 …

Leetcode +160 Intersection of Two Linked Lists

Leetcode 160 Intersection of Two Linked Lists 题目描述 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Example 1: Input: intersect…

Maven项目初开发(一)Maven项目开发的初配置(1)

首先一个五脏俱全的eclipse是必须的&#xff0c;J2EE版的eclipse一般都是整合了Maven插件。 Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说&#xff0c;一个用户会对应的拥有一个本地仓库。当然你可以通过修改${user.home}/.m2/settings.xml 配置这个地址…