博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java按钮退出_java – 如何在此程序中添加退出按钮?怎么样“清楚”?
阅读量:1542 次
发布时间:2019-04-21

本文共 3185 字,大约阅读时间需要 10 分钟。

我需要添加一个“清除计算器”的按钮,以及一个退出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);

}

}

转载地址:http://hordy.baihongyu.com/

你可能感兴趣的文章
PCB上三防漆规范和注意事项
查看>>
进程和线程常见的19个问题
查看>>
Keil MDK 和 IAR EARM发展历程及历史版本下载
查看>>
精选汇总 | 嵌入式软件基础知识
查看>>
为什么大家都看好RISC-V
查看>>
Keil MDK利用 fromelf 实现axf 转 bin 的方法
查看>>
程序员删除代码的快感
查看>>
状态机思路在嵌入式开发中的应用说明及注意事项
查看>>
硬件系统工程师需要具备哪些技能
查看>>
一个产品级MCU菜单框架设计
查看>>
电子工程师都在看什么?送你一份“修炼宝典”
查看>>
宏晶新推出的STC16单片机,有哪些方面的不足?
查看>>
硬件工程师常用的电路基础公式+换算!
查看>>
二极管的反向恢复过程
查看>>
增强嵌入式系统可靠性的几种方法
查看>>
公众号出了新功能
查看>>
精选汇总 | STM32、MCU、单片机
查看>>
精选汇总 | 科普知识
查看>>
精选汇总 | 开心一刻
查看>>
精选汇总 | IoT、物联网、人工智能
查看>>