用程序实现基本计算器功能

本文档详细介绍了使用Java编程语言开发一个GUI计算器的过程,包括设计按钮控件输入算术表达式,验证表达式合法性,实现混合运算,以及创建友好的交互界面。程序采用IntelliJ IDEA Community Edition作为开发环境,并实践了JavaGUI编程和frame框架。
摘要由CSDN通过智能技术生成

1,项目概述
1.1项目目标和主要内容
1) 能通过设计的按钮控件输入并 实现算术 表达式 ,表达式 在文本框中 显示
2) 能够检验算术表达式的合法性;
3) 能够 实现混合运算 的求解 ,算术表达式中包括加、减、乘、除等运算符 ;
4) 要求交互界面友好,程序健壮;
5)使用Java编程语言,在IntelliJ IDEA Community Edition上进行开发。
6)学习并实践JavaGUI编程和frame框架
1.2项目的主要功能
基本的加,减,乘,除,四则运算

package start;

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

public class Carculator extends JFrame implements ActionListener {
    /*      上方控件      */
    private JPanel jp_north=new JPanel();
    private JTextField input_text=new JTextField();
    private JButton c_Btn=new JButton("C");

    /*      中间控件      */
    private JPanel jp_center=new JPanel();
    public Carculator() throws HeadlessException {
        this.init();
        this.addNorthComponent();
        this.addCenterComponent();
    }
    //初始化方法
    public void init(){
        this.setTitle(Const.Title);
        this.setSize(Const.FRAME_W,Const.FRAME_H);
        this.setLayout(new BorderLayout());
        this.setResizable(false);
        this.setLocation(Const.Fame_x,Const.Fame_y);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    //上方控件方法
    public void addNorthComponent(){
        this.input_text.setPreferredSize(new Dimension(230,30));
        jp_north.add(input_text);
        this.c_Btn.setForeground(Color.RED);
        c_Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                input_text.setText("");
            }
        });
        jp_north.add(c_Btn);
        this.add(jp_north,BorderLayout.NORTH);
    }
    //中间控件方法
    public void addCenterComponent(){
        String btn_Text="123+456-789*0.=/";
        this.jp_center.setLayout(new GridLayout(4,4));
        for (int i=0;i<16;i++){
            String temp=btn_Text.substring(i,i+1);
            JButton btn=new JButton();
            btn.setText(temp);
            if (temp.equals("+")||
                    temp.equals("-")||
                    temp.equals("*")||
                    temp.equals("/")||
                    temp.equals(".")||
                    temp.equals("=")){
                btn.setFont(new Font("粗体",Font.BOLD,16));
                btn.setForeground(Color.RED);
            }
            btn.addActionListener(this);

            jp_center.add(btn);
        }
        this.add(jp_center,BorderLayout.CENTER);
    }



    public static void main(String[] args){
        Carculator carlacutor=new Carculator();
        carlacutor.setVisible(true);
    }
    private String firstInput=null;
    private String operator=null;
    @Override
    public void actionPerformed(ActionEvent e) {
        String clicker=e.getActionCommand();
        if ("-.1234567890".indexOf

        )!=-1) {
            this.input_text.setText(input_text.getText()+clicker);
            this.input_text.setHorizontalAlignment(JTextField.RIGHT);
            //JOptionPane.showMessageDialog(this, clicker);
        }else if (clicker.matches("[\\+\\-*/]{1}")){
            firstInput=this.input_text.getText();
            operator=clicker;
            this.input_text.setText("");
        }else if (clicker.equals("=")){
            Double a=Double.valueOf(firstInput);
            Double b=Double.valueOf(this.input_text.getText());
            Double result=0.0;
            switch (operator){
                case "+":
                    result=a+b;
                    break;
                case "-":
                    result=a-b;
                    break;
                case "*":
                    result=a*b;
                    break;
                case "/":
                    if (b!=0){
                    result=a/b;
                    break;
            }
            }this.input_text.setText(result.toString());
        }
    }
}
package start;

import java.awt.*;

public class Const {
    public static final int FRAME_W=300;
    public static final int FRAME_H=300;
    public static final int SCREEN_W= Toolkit.getDefaultToolkit().getScreenSize().height;
    public static final int SCREEN_H=Toolkit.getDefaultToolkit().getScreenSize().width;
    public static final int Fame_x=(SCREEN_H-FRAME_H)/2;
    public static final int Fame_y=(SCREEN_W-FRAME_W)/2;
    public static final String Title="计算器";
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值