ZBLOG

import java.awt.*; // 引入awt包中的所有类 import java.awt.event.AdjustmentEvent; // 引入awt.event包中的AdjustmentEvent类 import java.awt.event.AdjustmentListener; // 引入awt.event包中的AdjustmentListener类 public class E23 { // 定义E23类     Frame f = new Frame("调

import java.awt.*; // 引入awt包中的所有类

import java.awt.event.ActionEvent; // 引入awt.event包中的ActionEvent类

import java.awt.event.ActionListener; // 引入awt.event包中的ActionListener类

public class Calculator { // 定义Calculator类

    Frame f = new Frame(“计算器”); // 创建一个Frame对象f并设置标题为“计算器”

    TextField tf = new TextField(); // 创建一个TextField对象tf     Button b0 = new Button(“0”); // 创建数字按钮0     Button b1 = new Button(“1”); // 创建数字按钮1     Button b2 = new Button(“2”); // 创建数字按钮2     Button b3 = new Button(“3”); // 创建数字按钮3     Button b4 = new Button(“4”); // 创建数字按钮4     Button b5 = new Button(“5”); // 创建数字按钮5     Button b6 = new Button(“6”); // 创建数字按钮6     Button b7 = new Button(“7”); // 创建数字按钮7     Button b8 = new Button(“8”); // 创建数字按钮8     Button b9 = new Button(“9”); // 创建数字按钮9

    String operator;               // 运算符号,初始值为null 

double num1, num2, result;       // 存储两个操作数和结果值

void init() {                     

        Panel p1 = new Panel();       // 第一行面板p1,用于存放文本框tf       

    p1.setLayout(new BorderLayout()); 

        p1.add(tf,BorderLayout.CENTER); // 将文本框添加到p1中

        Panel p2 = new Panel();       // 第二行面板p2,用于存放数字按钮

    p2.setLayout(new GridLayout(4,3)); 

        p2.add(b1); // 将数字按钮1添加到p2中

    p2.add(b2); // 将数字按钮2添加到p2中

        p2.add(b3); // 将数字按钮3添加到p2中         p2.add(b4); // 将数字按钮4添加到p2中         p2.add(b5); // 将数字按钮5添加到p2中         p2.add(b6); // 将数字按钮6添加到p2中         p2.add(b7); // 将数字按钮7添加到p2中         p2.add(b8); // 将数字按钮8添加到p2中         p2.add(b9); // 将数字按钮9添加到p2中

    Panel p3 = new Panel();        // 第三行面板p3,用于存放运算符和清零键       
    GridLayout gl = new GridLayout(1,4);
    gl.setHgap(10);
    gl.setVgap(10);
    p3.setLayout(gl);

        Button bAdd = new Button(“+”);                     

    bAdd.addActionListener(new ActionListener() {                                             
        public void actionPerformed(ActionEvent e) {
            operator = "+";      
            num1 = Double.parseDouble(tf.getText());
            tf.setText("");              
        }
    });

    Button bSubtract = new Button("-");                
    bSubtract.addActionListener(new ActionListener() {                                      
        public void actionPerformed(ActionEvent e) {
            operator = "-";      
            num1 = Double.parseDouble(tf.getText());
            tf.setText("");              
        }
    });

    Button bMultiply = new Button("*");     
    bMultiply.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) {
            operator = "*";      
            num1 = Double.parseDouble(tf.getText());
            tf.setText("");              
        }
    });

    Button bDivide = new Button("/");     
    bDivide.addActionListener(new ActionListener() {           
        public void actionPerformed(ActionEvent e) {
            operator = "/";      
            num1 = Double.parseDouble(tf.getText());
            tf.setText("");              
        }
    });

        p3.add(bAdd);     // 将加号按钮添加到p3中         p3.add(bSubtract); // 将减号按钮添加到p3中         p3.add(bMultiply); // 将乘号按钮添加到p3中         p3.add(bDivide);   // 将除号按钮添加到p3中

        Panel p4 = new Panel();       // 第四行面板p4,用于存放数字0和清零键

    GridLayout gl2 = new GridLayout(1,2);
    gl2.setHgap(10);
    gl2.setVgap(10);

        p4.setLayout(gl2);

        Button bZero = new Button(“0”);         bZero.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
        	if (!tf.getText().equals("0")) { // 如果文本框不是0,则在文本框后追加0
                tf.setText(tf.getText() + "0");
            }
        }
    });

        Button bClear = new Button(“清零”);         bClear.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
        	operator = null;
        	num1 = 0;
        	num2 = 0;
        	result = 0;
            tf.setText("");
        }
    });

        p4.add(bZero);    // 将数字按钮0添加到p4中         p4.add(bClear);   // 将清零键添加到p4中

        f.setLayout(new GridLayout(4,1,10,10)); // 设置f的布局为4行1列的GridLayout,并指定行间距和列间距均为10         f.add(p1); // 将面板p1添加到f窗口中         f.add(p2); // 将面板p2添加到f窗口中         f.add(p3); // 将面板p3添加到f窗口中         f.add(p4); // 将面板p4添加到f窗口中

        f.setSize(250,300); // 设置f窗口的大小为250*300像素         f.setVisible(true); // 设置f窗口可见     }

    public static void main(String[] args) { // 主方法

        Calculator c = new Calculator(); // 创建一个Calculator对象c

    c.init();     
} 

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=913

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?