我需要添加一个“清除计算器”的按钮,以及一个退出butPanel上的程序的按钮.它也需要是非常基本的

Java代码,因为我是初学者,并且有一个糟糕的comp.sci.老师.我有一个带退出按钮的代码示例,但我不确定如何将它放入我的程序中.我已经尝试了这么多.

此外,如果有一个更好的“错误检查”方式,将非常感激.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator2 extends JFrame implements ActionListener

{

JLabel heading = new JLabel ("2. Calculator");

JLabel intro1 = new JLabel ("This program stimulates a four-function calculator.");

JLabel intro2 = new JLabel ("It takes an operator and a number. It can add, subtract,");

JLabel intro3 = new JLabel (" multiply and divide. Enter in the format eg. '+35'. ");

JLabel inLabel = new JLabel (" Operation: ");

JLabel outLabel = new JLabel (" Total: ");

JTextField inOper = new JTextField (7);

JTextField outTota = new JTextField (7); // intro

//panels

JPanel titlePanel = new JPanel ();

JPanel intro1Panel = new JPanel ();

JPanel intro2Panel = new JPanel ();

JPanel intro3Panel = new JPanel ();

JPanel operPanel = new JPanel ();

JPanel totaPanel = new JPanel ();

JPanel butPanel = new JPanel ();

String operTemp;

String totaTemp;

public Calculator2 ()

{

setTitle ("C - 2.");

inOper.addActionListener (this);

outTota.setEditable (false);

getContentPane ().setLayout (new FlowLayout ());

titlePanel.add (heading);

intro1Panel.add (intro1);

intro2Panel.add (intro2);

intro3Panel.add (intro3);

operPanel.add (inLabel);

operPanel.add (inOper);

totaPanel.add (outLabel);

totaPanel.add (outTota); //adds components to panels

getContentPane ().add (titlePanel);

getContentPane ().add (intro1Panel);

getContentPane ().add (intro2Panel);

getContentPane ().add (intro3Panel);

getContentPane ().add (operPanel);

getContentPane ().add (totaPanel); //Adds panels to Frame

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}

public static int isInteger (String input)

{

try

{

Integer.parseInt (input);

return Integer.parseInt (input);

}

catch (NumberFormatException nfe)

{

return 0;

}

} //isInteger method

// The application

public String calculate (String operation, String newtotal)

{

int total = isInteger (newtotal);

String totalS;

char operator;

int number = 0;

operator = operation.charAt (0);

if (operator == '+')

{

number = isInteger (operation.substring (1));

total = total + number;

totalS = Integer.toString (total);

}

else if (operator == '-')

{

number = isInteger (operation.substring (1));

total = total - number;

totalS = Integer.toString (total);

}

else if (operator == '*')

{

number = isInteger (operation.substring (1));

total = total * number;

totalS = Integer.toString (total);

}

else if (operator == '/')

{

number = isInteger (operation.substring (1));

total = total / number;

totalS = Integer.toString (total);

}

else

{

totalS = ("ERROR");

}

if (number == 0)

{

totalS = ("ERROR");

}

return totalS;

} // calculate method

public void actionPerformed (ActionEvent evt)

{

String userIn = inOper.getText ();

String totalIn = outTota.getText ();

try

{

totaTemp = calculate (userIn, totalIn);

outTota.setText (totaTemp + "");

}

catch (Exception ex)

{

outTota.setText ("ERROR");

}

repaint ();

}

public static void main (String[] args)

{

Calculator2 calc = new Calculator2 ();

calc.setSize (350, 350);

calc.setResizable (false);

calc.setVisible (true);

}

}

java按钮退出_java – 如何在此程序中添加退出按钮?怎么样“清楚”?相关推荐

  1. java gui 艺术字_Java 在Word文档中添加艺术字

    与普通文字相比,艺术字更加美观有趣也更具有辨识度,常见于一些设计精美的杂志或宣传海报中.我们在日常工作中编辑Word文档时,也可以通过添加艺术字体来凸显文章的重点,美化页面排版.这篇文章将介绍如何使用 ...

  2. java显示艺术字_Java 在Word文档中添加艺术字的示例

    与普通文字相比,艺术字更加美观有趣也更具有辨识度,常见于一些设计精美的杂志或宣传海报中.我们在日常工作中编辑Word文档时,也可以通过添加艺术字体来凸显文章的重点,美化页面排版.这篇文章将介绍如何使用 ...

  3. netbeans缺少java文件夹_Java,如何在netbeans中添加库文件?

    在Netbeans 8.2中 1.从Web源下载二进制文件. 该阿帕奇Commos是:http://commons.apache.org/components.html][1] 在这种情况下,你必须选 ...

  4. java怎么加定时器_JAVA WEB程序中添加定时器

    JAVA WEB程序中添加定时器 //这是我的定时器类,用来定时执行某段任务: package com.my.time; import java.text.ParseException; import ...

  5. java窗体中添加图片_在java窗体程序中添加图片的方法

    在java窗体程序中添加图片的方法 发布时间:2020-06-16 11:24:13 来源:亿速云 阅读:148 作者:Leah 这篇文章主要为大家详细介绍了在java窗体程序中添加图片的方法,图文详 ...

  6. java channel midi_为Java程序中添加播放MIDI音乐功能

    下载本文示例代码 Java在多媒体处理方面的确优势不大,但是我们在程序中有些时候又需要一些音乐做为点缀,如果播放的音乐是wav等波形音频文件,又挺大,所以背景音乐最好就是MIDI了,可是网上很多播放M ...

  7. matlab 集体注释,向程序中添加注释 - MATLAB Simulink - MathWorks 中国

    向程序中添加注释 编写代码时,最好添加描述代码的注释.注释有助于其他人员理解您的代码,并且有助您在稍后返回代码时再度记起.在程序开发和测试期间,您还可以使用注释来注释掉任何不需要运行的代码. 在实时编 ...

  8. MFC应用程序中添加控制台窗口

    在MFC程序中输出调试信息的方法有两种,一种是使用TRACE宏,可以向Output窗口输出调试信息:另一种是用MessageBox,弹出消息框来输出调试信息,但会影响程序的运行. 其实有一种方法可以更 ...

  9. 如何在RCP程序中添加一个banner栏

    前言:这段时间还算比较空闲,我准备把过去做过的有些形形色色,甚至有些奇怪的研究总结一下,也许刚好有人用的着也不一定,不枉为之抓耳挠腮的时光和浪费的电力.以前有个客户提出要在RCP程序中添加一个bann ...

最新文章

  1. 2020年最新!百度、微软、浪潮、谷歌企业级综述更新!
  2. 转:.Net 中的反射(反射特性) - Part.3
  3. 数字图像处理形态学运算
  4. 【Java开发】生成二维码
  5. day4 Python的selenium库
  6. 【OS学习笔记】二十二 保护模式六:保户模式下操作系统内核如何加载用户程序并运行 对应的汇编代码之用户程序
  7. 感觉又学到了不少,在这里写下来,但也有一个问题,不知道是为甚吗?
  8. Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. Select
  9. 数据库报12516linux,ORA-12516故障解决
  10. dom块级元素的各种宽高
  11. 关于C#操作mysql数据库乱码
  12. vue 使用vue-print-nb 实现打印功能 和 用针式打印机打印模糊问题
  13. 小程序轮播图与图片处理
  14. 百度HI QQ和MSN 阿里旺旺贸易通MSN在线客服聊天代码
  15. 一文看懂Modbus, Rtu, Rs485等名词的联系
  16. java中API什么意思
  17. 猫抓m3u8,遇到该媒体已加密,请注意下载key文件
  18. c语言实现循环结构的语句有哪些?它们的区别是什么?,2011年04月份计算机软件基础(一)复习资料二...
  19. iterm配置alias
  20. 百度阿里网易大疆等大小厂前端校招面筋 1

热门文章

  1. 专有网络(VPC)的六大应用场景
  2. 同学,要不要来挑战双11零点流量洪峰?
  3. Mars 是什么、能做什么、如何做的——记 Mars 在 PyCon China 2018 上的分享
  4. 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  5. 火山引擎进军云市场,计划未来三年服务十万客户
  6. 报告:5G 网络切片可能会给不法分子留下漏洞!
  7. 【IPF2020】浪潮集团副总裁、渠道管理部总经理王峰:赋能智慧生态 筑基新基建
  8. 我最喜欢的云 IDE 推荐!
  9. 涨姿势,一个通信项目从开始到结束,原来还包括这些工作
  10. php include无效,php 两次include后,第一个include里的变量无效了