SWUST_Java实验四(计算器)

本文介绍了使用JavaSwing库创建一个简单的命令行计算器应用程序,包括界面设计、事件处理和基本算术运算功能。
摘要由CSDN通过智能技术生成
package Lab_4_Calculator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Calculater {
    public JFrame frame;
    public JTextField outField;
    private String num1="0";
    private String num2="";
    private String operator="+";
    
    
    
    public Calculater() {
        
        frame=new JFrame();
        frame.setSize(800,600);
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭
        frame.setVisible(true);//显示窗口
        
        frame.setLayout(new BorderLayout());//
        
        outField=new JTextField();
        outField.setHorizontalAlignment(SwingConstants.RIGHT);//字体右居中
        outField.setFont(new Font("幼圆",Font.BOLD,60));
        frame.add(outField,BorderLayout.NORTH);//outField 最上面的输出框
        
        JPanel panel=new JPanel();//中间容器  
        panel.setBackground(Color.BLACK);
        frame.add(panel,BorderLayout.CENTER);
        
        panel.setLayout(new GridLayout(4,4,5,5));
        
        String [] txt= {"7","8","9","/", "4","5","6","*",
                "1","2","3","-", "0",".","=","+"};
        
        for(int i=0;i<16;i++) {
            JButton btn=new JButton(txt[i]);
            btn.setFont(new Font("幼圆",Font.BOLD,40));
            btn.addActionListener(newAction());
            panel.add(btn);
        }
    }
    private ActionListener newAction() {
        ActionListener actionListener=new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                String cmd=e.getActionCommand();//识别每个按钮
                if("+-*/".contains(cmd)) {
                	
                    caculate(operator);
                    operator=cmd;
                    //operator=cmd;//记录操作符
                    //caculate(cmd);
                }else if(cmd.equals("=")) {
                    caculate(operator);//为=就计算上一个操作符
                }
                else if(cmd.equals(".")) {
                    if(num2.contains(".")) {//第一次按了小数点. 第二次再按就要忽略
                        return;
                    }
                    num2=num2+cmd;
                    outField.setText(num2);
                }else {
                    num2=num2+cmd;
                    outField.setText(num2);
                }
                
            }
            private void caculate(String cmd) {
                double d1=Double.valueOf(num1);
                double d2=0;
                if(num2.equals("")) {//判断num2 是否为空
                    return;//为空就不用再计算
                    
                }
                d2=Double.valueOf(num2);
                String result="";
                switch (cmd){
                    case "+":result=String.valueOf(d1+d2);break;
                    case "-":result=String.valueOf(d1-d2);break;
                    case "*":result=String.valueOf(d1*d2);break;
                    case "/":result=String.valueOf(d1/d2);break;
                    
                }
                
                num1=result;
                num2="";
                outField.setText(result);
            }
        }; 
        
        return actionListener;
    }
    
    
    public static void main(String[] args) {
        Calculater ca=new Calculater();
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值