Netbeans Java GUI计算器-如何对2个以上的数字执行计算?
我用netbeans制作了一个java计算器,它对两个数字执行简单的+-*/运算。代码如下:Netbeans Java GUI计算器-如何对2个以上的数字执行计算?,java,parsing,netbeans,calculator,Java,Parsing,Netbeans,Calculator,我用netbeans制作了一个java计算器,它对两个数字执行简单的+-*/运算。代码如下: @ManagedBean @SessionScoped public class Calculation extends javax.swing.JFrame { double firstNum; double secondNum; double thirdNum; double result; String operation; String operation2; /** * Creates
@ManagedBean
@SessionScoped
public class Calculation extends javax.swing.JFrame {
double firstNum;
double secondNum;
double thirdNum;
double result;
String operation;
String operation2;
/**
* Creates new form Calculation
*/
public Calculation() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
// Generated code here for GUI Components....
// Below is my calculations and displays...
private void Btn4ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn4.getText();
txtDisplay.setText(takein);
}
private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn1.getText();
txtDisplay.setText(takein);
}
private void Btn2ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn2.getText();
txtDisplay.setText(takein);
}
private void Btn3ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn3.getText();
txtDisplay.setText(takein);
}
private void Btn5ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn5.getText();
txtDisplay.setText(takein);
}
private void Btn6ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn6.getText();
txtDisplay.setText(takein);
}
private void Btn7ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn7.getText();
txtDisplay.setText(takein);
}
private void Btn8ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn8.getText();
txtDisplay.setText(takein);
}
private void Btn9ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn9.getText();
txtDisplay.setText(takein);
}
private void Btn0ActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + Btn0.getText();
txtDisplay.setText(takein);
}
private void BtnPointActionPerformed(java.awt.event.ActionEvent evt) {
String takein;
takein = txtDisplay.getText() + BtnPoint.getText();
txtDisplay.setText(takein);
}
private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText("");
}
private void BtnPlusActionPerformed(java.awt.event.ActionEvent evt) {
firstNum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("+");
operation = "+";
}
private void BtnMinusActionPerformed(java.awt.event.ActionEvent evt) {
firstNum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("-");
operation = "-";
}
private void BtnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {
firstNum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("*");
operation = "*";
}
private void BtnDivideActionPerformed(java.awt.event.ActionEvent evt) {
firstNum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("/");
operation = "/";
}
private void BtnPlusMinusActionPerformed(java.awt.event.ActionEvent evt) {
double operan = (Double.parseDouble(String.valueOf(txtDisplay.getText())));
operan = operan * (-1);
txtDisplay.setText(String.valueOf(operan));
}
private void BtnEqualsActionPerformed(java.awt.event.ActionEvent evt) {
String answer;
secondNum = Double.parseDouble(txtDisplay.getText());
switch (operation)
{
case "+":
result = firstNum + secondNum;
answer = String.format("%.0f", result);
txtDisplay.setText(answer);
break;
case "-":
result = firstNum - secondNum;
answer = String.format("%.0f", result);
txtDisplay.setText(answer);
break;
case "*":
result = firstNum * secondNum;
answer = String.format("%.0f", result);
txtDisplay.setText(answer);
break;
case "/":
result = firstNum / secondNum;
answer = String.format("%.0f", result);
txtDisplay.setText(answer);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
// Generated code here.. for GUI.. not really relevant.
}
// Variables declaration - do not modify // Auto generated via designview
private javax.swing.JButton Btn0;
private javax.swing.JButton Btn1;
private javax.swing.JButton Btn2;
private javax.swing.JButton Btn3;
private javax.swing.JButton Btn4;
private javax.swing.JButton Btn5;
private javax.swing.JButton Btn6;
private javax.swing.JButton Btn7;
private javax.swing.JButton Btn8;
private javax.swing.JButton Btn9;
private javax.swing.JButton BtnCancel;
private javax.swing.JButton BtnDivide;
private javax.swing.JButton BtnEquals;
private javax.swing.JButton BtnMinus;
private javax.swing.JButton BtnMultiply;
private javax.swing.JButton BtnPlus;
private javax.swing.JButton BtnPlusMinus;
private javax.swing.JButton BtnPoint;
private javax.swing.JTextField txtDisplay;
// End of variables declaration
}
@ManagedBean
@会议范围
公共类计算扩展了javax.swing.JFrame{
双第一数;
双秒数;
双三十毫米;
双重结果;
串操作;
字符串操作2;
/**
*创建新的表单计算
*/
公共计算(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
//此处为GUI组件生成的代码。。。。
//下面是我的计算和显示。。。
私有void Btn4ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn4.getText();
txtDisplay.setText(takein);
}
私有void Btn1ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn1.getText();
txtDisplay.setText(takein);
}
私有void Btn2ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn2.getText();
txtDisplay.setText(takein);
}
私有void Btn3ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn3.getText();
txtDisplay.setText(takein);
}
私有void Btn5ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn5.getText();
txtDisplay.setText(takein);
}
私有void Btn6ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn6.getText();
txtDisplay.setText(takein);
}
私有void Btn7ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn7.getText();
txtDisplay.setText(takein);
}
私有无效Btn8ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn8.getText();
txtDisplay.setText(takein);
}
私有void Btn9ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn9.getText();
txtDisplay.setText(takein);
}
私有无效Btn0ActionPerformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+Btn0.getText();
txtDisplay.setText(takein);
}
私有void btnpotactionperformed(java.awt.event.ActionEvent evt){
弦吸管;
takein=txtDisplay.getText()+BtnPoint.getText();
txtDisplay.setText(takein);
}
私有void BtnCancelActionPerformed(java.awt.event.ActionEvent evt){
txtDisplay.setText(“”);
}
私有void BtnPlusActionPerformed(java.awt.event.ActionEvent evt){
firstNum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“+”);
操作=“+”;
}
private void BtnMinusActionPerformed(java.awt.event.ActionEvent evt){
firstNum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“-”);
操作=“-”;
}
private void BtnMultiplyActionPerformed(java.awt.event.ActionEvent evt){
firstNum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“*”);
操作=“*”;
}
私有void BtnDivideActionPerformed(java.awt.event.ActionEvent evt){
firstNum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“/”);
操作=“/”;
}
私有void BtnPlusMinusActionPerformed(java.awt.event.ActionEvent evt){
double operan=(double.parseDouble(String.valueOf(txtDisplay.getText()));
operan=operan*(-1);
txtDisplay.setText(String.valueOf(operan));
}
私有void BtnEqualsActionPerformed(java.awt.event.ActionEvent evt){
字符串回答;
secondNum=Double.parseDouble(txtDisplay.getText());
开关(操作)
{
格“+”:
结果=firstNum+secondNum;
答案=字符串。格式(“%.0f”,结果);
txtDisplay.setText(应答);
打破
案例“-”:
结果=firstNum-secondNum;
答案=字符串。格式(“%.0f”,结果);
txtDisplay.setText(应答);
打破
案例“*”:
结果=firstNum*secondNum;
答案=字符串。格式(“%.0f”,结果);
txtDisplay.setText(应答);
打破
案例“/:
结果=firstNum/secondNum;
答案=字符串。格式(“%.0f”,结果);