码迷,mamicode.com
首页 > 编程语言 > 详细

java初学者编译简单的计算机

时间:2019-12-17 00:13:48      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:类型   初学   val   tcl   port   显示   java初学者   font   运算符   

package com.yj.test;

import java.awt.BorderLayout;
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;

public class CaculateFrame extends JFrame{

    private JPanel content,panel;
    private JTextField txtInput;
     //记录运算符操作
    private String operation;
    private double v1,v2,result;
    // 一次计算的开关
    private boolean flag = false; 
    
    public CaculateFrame() {
        this.setTitle("计算器");
        //设置位置和大小
        this.setBounds(300, 200, 300, 300);
        
        txtInput = new JTextField(20);
        txtInput.setHorizontalAlignment(JTextField.RIGHT);
        Font f = new Font("微软雅黑",Font.BOLD,20);
        txtInput.setFont(f);
        
        content = new JPanel();
        content.setLayout(new BorderLayout());
        
        content.add(txtInput,BorderLayout.NORTH);
        
        panel = new JPanel();
        panel.setLayout(new GridLayout(4,4,5,5));
        
        panel.add(useButton("7"));
        panel.add(useButton("8"));
        panel.add(useButton("9"));
        panel.add(useButton("/"));
        
        panel.add(useButton("4"));
        panel.add(useButton("5"));
        panel.add(useButton("6"));
        panel.add(useButton("*"));
        
        panel.add(useButton("1"));
        panel.add(useButton("2"));
        panel.add(useButton("3"));
        panel.add(useButton("-"));
        
        panel.add(useButton("0"));
        panel.add(useButton("."));
        panel.add(useButton("="));
        panel.add(useButton("+"));
        
        content.add(panel,BorderLayout.CENTER);
        this.add(content);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private JButton useButton(String key) {
        JButton btn = new JButton(key);
        btn.addActionListener(new ActionListener() {
            //事件是由哪个控件发出的,通通都记录在ActionEvent这个对象中
            public void actionPerformed(ActionEvent e) {
                //根据按钮上面不同的值,应该做不同的操作
                myAction(key);
            }
        });
        return btn;
    }
    
    private void myAction(String key) {
        if(flag) {
            txtInput.setText("");
            flag = false;
        }
        switch(key) {
        case "0":
        case "1":
        case "2":
        case "3":
        case "4":
        case "5":
        case "6":
        case "7":
        case "8":
        case "9":
        case ".":
            if(txtInput.getText() == null || txtInput.getText().equals("")) {
                if(key.equals("0")) {
                    return;
                }
                if(key.equals(".")) {
                    txtInput.setText("0.");
                    return;
                }
            }if(txtInput.getText().indexOf(".") != -1 && key.equals(".")) {
                return;
            }
            txtInput.setText(txtInput.getText() + key);
            break;
        case "+":
            this.operation = "+";
            
            if(txtInput.getText() == null || txtInput.getText().equals("")) {
                v1 = 0;
            }else {
                //转换为double类型
                v1 = Double.parseDouble(txtInput.getText());
            }
            txtInput.setText("");
            break;
        case "-":
            this.operation = "-";
            if(txtInput.getText() == null || txtInput.getText().equals("")) {
                v1 = 0;
            }else {
                //转换为double类型
                v1 = Double.parseDouble(txtInput.getText());
            }
            txtInput.setText("");
            break;
        case "*":
            this.operation = "*";
            if(txtInput.getText() == null || txtInput.getText().equals("")) {
                v1 = 0;
            }else {
                //转换为double类型
                v1 = Double.parseDouble(txtInput.getText());
            }
            txtInput.setText("");
            break;
        case "/":
            this.operation = "/";
            if(txtInput.getText() == null || txtInput.getText().equals("")) {
                v1 = 0;
            }else {
                //转换为double类型
                v1 = Double.parseDouble(txtInput.getText());
            }
            txtInput.setText("");
            break;
        case "=":
            if(txtInput.getText() == null || txtInput.getText().equals("")) {
                v2 = 0;
            }else {
                //转换为double类型
                v2 = Double.parseDouble(txtInput.getText());
            }
            switch(this.operation) {
                case "+":
                    result = v1 + v2;
                    break;
                case "-":
                    result = v1 - v2;
                    break;
                case "*":
                    result = v1 * v2;
                    break;
                case "/":
                    if (v2 == 0) {
                        return;
                    }
                    result = v1 / v2;
                    break;
            }
            txtInput.setText(String.valueOf(result));
            v1 = 0;
            v2 = 0;
            result = 0;
            flag = true;
        }
    }
}

 

计算机测试显示窗口 :

package com.yj.test;

public class Test {

    public static void main(String[] args) {
        CaculateFrame cf = new CaculateFrame();
        cf.setVisible(true);
    }

}

java初学者编译简单的计算机

标签:类型   初学   val   tcl   port   显示   java初学者   font   运算符   

原文地址:https://www.cnblogs.com/suger-4/p/12052050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!