Java图形界面简单计算器

谢谢... 谢谢 展开
 我来答
刑初素5
2017-11-21 · TA获得超过311个赞
知道小有建树答主
回答量:165
采纳率:69%
帮助的人:140万
展开全部

示例代码如下,请参考:

package test.com.test;

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

public class Cal {

    TextField tf = new TextField(40);       //显示文本框
    StringBuffer s1 = new StringBuffer();   //记录运算数字,以及保留结果
    StringBuffer s2 = new StringBuffer();   //记录运算数字,保留上一个输入的数字或运算结果
    static String flag = new String();      //标记运算符号:+,-,*,/
    boolean start = true;   //标记运算开始或结束,保证一次运算之后,第二次进行运算时能同时清空显示界面,即s1为空

    public void init(){
        //主界面
        JFrame f = new JFrame("计算器");
        JPanel p1 = new JPanel();
        p1.add(tf);
        f.add(p1,BorderLayout.NORTH);
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(1,3));
        //动作监听器
        ActionListener listen =new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                //输入数字0~9
                if(arg0.getActionCommand()=="0"){

                    if(!s1.toString().equals(new String())){
                        if(!start){
                            //s1清零,保证可以重新输入数字
                            s1.delete(0, s1.length());
                        }
                        start = true;
                        tf.setText(s1.append("0").toString());
                    }   
                }
                if(arg0.getActionCommand()=="1"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("1").toString());
                }
                if(arg0.getActionCommand()=="2"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("2").toString());
                }
                if(arg0.getActionCommand()=="3"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("3").toString());
                }
                if(arg0.getActionCommand()=="4"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("4").toString());
                }
                if(arg0.getActionCommand()=="5"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("5").toString());
                }
                if(arg0.getActionCommand()=="6"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("6").toString());
                }
                if(arg0.getActionCommand()=="7"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("7").toString());
                }
                if(arg0.getActionCommand()=="8"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("8").toString());
                }
                if(arg0.getActionCommand()=="9"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    tf.setText(s1.append("9").toString());
                }
                //输入小数点
                if(arg0.getActionCommand()=="."){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start=true;
                    if(s1.toString().equals(new String()))
                        tf.setText(s1.append("0.").toString());
                    else if(s1.length()==1&&s1.charAt(0)=='-')
                        tf.setText(s1.append("0.").toString());
                    else
                        tf.setText(s1.append(".").toString());
                }
                //输入负号
                if(arg0.getActionCommand()=="+/-"){
                    if(!start){
                        s1.delete(0, s1.length());
                    }
                    start = true;
                    if(s1.toString().equals(new String()))
                        tf.setText(s1.append('-').toString());
                }
                //退格Backspace
                if(arg0.getActionCommand()=="Backspace"){
                    start = true;
                    if(s1.length()!=0){
                        //删除最后输入的一位
                        s1.deleteCharAt(s1.length()-1);
                        tf.setText(s1.toString());
                    }
                }
                //归零CE
                if(arg0.getActionCommand()=="CE"){
                    //清空所有,start标记设为true
                    start = true;
                    s1.delete(0, s1.length());
                    s2.delete(0, s2.length());
                    tf.setText(s1.toString());
                }
                //清除C
                if(arg0.getActionCommand()=="C"){
                    //清空当前输入,即s1清零
                    start = true;
                    s1.delete(0, s1.length());
                    tf.setText(s1.toString());
                }
                //加号
                if(arg0.getActionCommand()=="+"){
                    start = true;
                    flag="+";
                    //s2接收s1的值
                    s2.append(String.valueOf(s1.toString()));
                    //s1清零,重新接收下一个数据
                    s1.delete(0, s1.length());
                    tf.setText(s1.toString());
                }
                //减号
                if(arg0.getActionCommand()=="-"){
                    start = true;
                    flag="-";
                    s2.append(String.valueOf(s1.toString()));
                    s1.delete(0, s1.length());
                    tf.setText(s1.toString());
                }
                //乘号
                if(arg0.getActionCommand()=="*"){
                    start = true;
                    flag="*";
                    s2.append(String.valueOf(s1.toString()));
                    s1.delete(0, s1.length());
                    tf.setText(s1.toString());
                }
                //除号
                if(arg0.getActionCommand()=="/"){
                    start = true;
                    flag="/";
                    s2.append(String.valueOf(s1.toString()));
                    s1.delete(0, s1.length());
                    tf.setText(s1.toString());
                }
                //开根号
                if(arg0.getActionCommand()=="sqrt"){
                    start =false;
                    double s=Double.parseDouble(s1.toString());
                    s1.delete(0, s1.length());
                tf.setText(s1.append(String.valueOf(Math.sqrt(s))).toString());
                }
                //求%
                if(arg0.getActionCommand()=="%"){
                    start =false;
                    double s=Double.parseDouble(s1.toString());
                    s1.delete(0, s1.length());
                    tf.setText(s1.append(String.valueOf(s/100)).toString());
                }
                //求1/x
                if(arg0.getActionCommand()=="1/x"&&!s1.toString().equals(new String())&&Double.parseDouble(s1.toString())!=0){
                    start =false;
                    double s=Double.parseDouble(s1.toString());
                    s1.delete(0, s1.length());
                    tf.setText(s1.append(String.valueOf(1/s)).toString());
                }
                //求结果
                if(arg0.getActionCommand()=="="&&s1.length()!=0&&s2.length()!=0){
                    //start标记为false
                    start =false;
                    //分别获取s1和s2的值
                    double s4=Double.parseDouble(s1.toString()); 
                    double s3=Double.parseDouble(s2.toString());
                    //清空s1和s2,便于开始下一次运算
                    s1.delete(0, s1.length());
                    s2.delete(0, s2.length());
                    //四则运算
                    if(flag=="+"){
                        tf.setText(s1.append(String.valueOf(s3+s4)).toString());
                    }
                    if(flag=="-"){
                        tf.setText(s1.append(String.valueOf(s3-s4)).toString());
                    }
                    if(flag=="*"){
                        tf.setText(s1.append(String.valueOf(s3*s4)).toString());
                    }
                    if(flag=="/"){
                        tf.setText(s1.append(String.valueOf(s3/s4)).toString());
                    }
                }       

            }
        };
        //添加按钮以及监听器
        JButton bks = new JButton("Backspace");
        bks.addActionListener(listen);
        JButton ce = new JButton("CE");
        ce.addActionListener(listen);
        JButton c = new JButton("C");
        c.addActionListener(listen);
        p2.add(bks);
        p2.add(ce);
        p2.add(c);
        f.add(p2);
        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(4,5));
        JButton b1 = new JButton("7");
        p3.add(b1);
        b1.addActionListener(listen);
        JButton b2 = new JButton("8");
        p3.add(b2);
        b2.addActionListener(listen);
        JButton b3 = new JButton("9");
        p3.add(b3);
        b3.addActionListener(listen);
        JButton b4 = new JButton("/");
        p3.add(b4);
        b4.addActionListener(listen);
        JButton b5 = new JButton("sqrt");
        p3.add(b5);
        b5.addActionListener(listen);
        JButton b6 = new JButton("4");
        p3.add(b6);
        b6.addActionListener(listen);
        JButton b7 = new JButton("5");
        p3.add(b7);
        b7.addActionListener(listen);
        JButton b8 = new JButton("6");
        p3.add(b8);
        b8.addActionListener(listen);
        JButton b9 = new JButton("*");
        p3.add(b9);
        b9.addActionListener(listen);
        JButton b10 = new JButton("%");
        p3.add(b10);
        b10.addActionListener(listen);
        JButton b11 = new JButton("1");
        p3.add(b11);
        b11.addActionListener(listen);
        JButton b12 = new JButton("2");
        p3.add(b12);
        b12.addActionListener(listen);
        JButton b13 = new JButton("3");
        p3.add(b13);
        b13.addActionListener(listen);
        JButton b14 = new JButton("-");
        p3.add(b14);
        b14.addActionListener(listen);
        JButton b15 = new JButton("1/x");
        p3.add(b15);
        b15.addActionListener(listen);
        JButton b16 = new JButton("0");
        p3.add(b16);
        b16.addActionListener(listen);
        JButton b17 = new JButton("+/-");
        p3.add(b17);
        b17.addActionListener(listen);
        JButton b18 = new JButton(".");
        p3.add(b18);
        b18.addActionListener(listen);
        JButton b19 = new JButton("+");
        p3.add(b19);
        b19.addActionListener(listen);
        JButton b20 = new JButton("=");
        p3.add(b20);
        b20.addActionListener(listen);
        f.add(p3,BorderLayout.SOUTH);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String args[]){
        //调用方法,实现计算器
        new Cal().init();
    }

}
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
百度网友ef054bc
2017-11-21 · 超过13用户采纳过TA的回答
知道答主
回答量:33
采纳率:40%
帮助的人:15.2万
展开全部
package com.lovo.calculator;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 计算器类
*
* @author Administrator
*
*/
public class Calculator extends JFrame implements ActionListener {

private static final long serialVersionUID = 3868243398506940702L;

// 文本框
private JTextField result;
// 按钮数组
private JButton[] buttons;
// 按钮文本
private final String[] characters = { "7", "8", "9", "/", "4", "5", "6",
"*", "1", "2", "3", "-", "0", ".", "=", "+" };
// 是否为第一个输入的数字
private boolean isFirstDigit = true;
// 运算结果
private double resultNum = 0.0;
// 运算符
private String operator = "=";
public Calculator(String title) {
// 设置标题栏
super(title);
// 初始化各组件
init();
// 注册各组件监听器
registerListener();
// 显示窗体
setVisible(true);
}
/**
* 初始化各组件
*/
private void init() {
// 常用属性初始化
setSize(220, 200);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/* 文本框对象初始化 */
result = new JTextField("0");
// 文本右对齐
result.setHorizontalAlignment(JTextField.RIGHT);
// 设置是否可编辑
result.setEditable(false);
/* 按钮初始化 */
buttons = new JButton[characters.length];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(characters[i]);
buttons[i].setFocusable(false); // 不允许按钮定位焦点
}
/* 将文本框与按钮添加到窗体中 */
add(result, BorderLayout.NORTH);
JPanel pnl = new JPanel(new GridLayout(4, 4, 5, 5));
for (JButton jButton : buttons) {
pnl.add(jButton);
}
add(pnl);
this.getContentPane().setFocusable(true);
}
/**
* 注册监听器
*/
private void registerListener() {
for (JButton jButton : buttons) {
jButton.addActionListener(this);
}
// 注册键盘事件
this.getContentPane().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
String text = String.valueOf(e.getKeyChar());
if (Character.isDigit(text.charAt(0)) || ".".equals(text)) { // 数字或小数点
handleNumber(text);
} else if ("+-*/=".indexOf(text) != -1) { // 运算符
handleOperator(text);
} else if (e.getKeyCode() == 8) { // 退格键
String tmp = result.getText().trim();
if (tmp.length() == 1) {
result.setText("0");
isFirstDigit = true;
} else {
result.setText(tmp.substring(0, tmp.length() - 1));
}
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
String text = btn.getText().trim();
if (Character.isDigit(text.charAt(0)) || ".".equals(text)) { // 处理数字和小数点
handleNumber(text);
} else { // 处理运算符
handleOperator(text);
}
}
/**
* 处理数字和小数点
*
* @param text
*/
private void handleNumber(String text) {
if (isFirstDigit) { // 第一次输入
if (".".equals(text)) {
this.result.setText("0.");
} else {
this.result.setText(text);
}
} else if ("0".equals(text) && "0".equals(this.result.getText())) {
isFirstDigit = true;
return;
} else if (".".equals(text) && this.result.getText().indexOf(".") == -1) {
this.result.setText(this.result.getText() + ".");
} else if (!".".equals(text)) {
this.result.setText(this.result.getText() + text);
}
isFirstDigit = false;
}
/**
* 处理运算符
*
* @param text
*/
private void handleOperator(String text) {
switch (operator) { // 处理各项运算 适用于JDK1.7版本的
case "+":
resultNum += Double.parseDouble(this.result.getText());
break;
case "-":
resultNum -= Double.parseDouble(this.result.getText());
break;
case "*":
resultNum *= Double.parseDouble(this.result.getText());
break;
case "/":
resultNum /= Double.parseDouble(this.result.getText());
break;
case "=":
resultNum = Double.parseDouble(this.result.getText());
break;
}
// 将文本框的值修改为运算结果
this.result.setText(String.valueOf(resultNum));
// 将点击的运算符放入operator保存
operator = text;
// 下一个数字第一次点击
isFirstDigit = true;
}
}
测试:
package com.lovo.calculator;
public class Test {
public static void main(String[] args) {
new Calculator("My Calculator");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
北京巴卜技术有限公司
2017-11-21 · 【免费测试,验证码5秒必达】
北京巴卜技术有限公司
北京巴卜技术有限公司(以下简称巴卜)是具备国际水准的移动商务平台技术和应用方案提供商。自成立以来,巴卜始终 致力于为国内外企业提供具备国际技术水准的移动商务平台及运营服务。
向TA提问
展开全部
布局不是最重要的,重要的是你要理解编程的思想。上面的功能其实很简单吧,不过多新手来说,能做好,说明你的基本功还是不错的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
雨寅08d
2017-11-21 · 超过33用户采纳过TA的回答
知道答主
回答量:189
采纳率:33%
帮助的人:45.9万
展开全部
现在很少人用java做图形界面了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式