java - Netbeans计算器错误

标签 java swing netbeans

我在 netbeans 中制作了一个计算器,到目前为止我添加了加号和减号按钮来工作。但是假设我做 5+5,它给我 10,如果我再添加 5,它给我 15。这样部分就正确了。但如果我做9-5,它会给我-4,如果我从-4中减去2,我会得到正6。请帮忙,我对java相当陌生。

public class Calculator extends javax.swing.JFrame {

/**
 * Creates new form Calculator
 */
public Calculator() {
    initComponents();
}

double fNums = 0.0;
double sNums = 0.0;
boolean add = false;
boolean sub = false;

/**
 * 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() {

    jPanel1 = new javax.swing.JPanel();
    display = new javax.swing.JTextField();
    seven = new javax.swing.JButton();
    eight = new javax.swing.JButton();
    nine = new javax.swing.JButton();
    plus = new javax.swing.JButton();
    four = new javax.swing.JButton();
    five = new javax.swing.JButton();
    six = new javax.swing.JButton();
    minus = new javax.swing.JButton();
    one = new javax.swing.JButton();
    two = new javax.swing.JButton();
    three = new javax.swing.JButton();
    multiply = new javax.swing.JButton();
    zero = new javax.swing.JButton();
    decimal = new javax.swing.JButton();
    divide = new javax.swing.JButton();
    enter = new javax.swing.JButton();
    clear = new javax.swing.JButton();
    jMenuBar2 = new javax.swing.JMenuBar();
    jMenu4 = new javax.swing.JMenu();
    Home = new javax.swing.JMenuItem();
    jMenu5 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    display.setEditable(false);

    seven.setText("7");
    seven.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            sevenActionPerformed(evt);
        }
    });

    eight.setText("8");
    eight.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            eightActionPerformed(evt);
        }
    });

    nine.setText("9");
    nine.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nineActionPerformed(evt);
        }
    });

    plus.setText("+");
    plus.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            plusActionPerformed(evt);
        }
    });

    four.setText("4");
    four.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            fourActionPerformed(evt);
        }
    });

    five.setText("5");
    five.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            fiveActionPerformed(evt);
        }
    });

    six.setText("6");
    six.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            sixActionPerformed(evt);
        }
    });

    minus.setText("-");
    minus.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            minusActionPerformed(evt);
        }
    });

    one.setText("1");
    one.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            oneActionPerformed(evt);
        }
    });

    two.setText("2");
    two.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            twoActionPerformed(evt);
        }
    });

    three.setText("3");
    three.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            threeActionPerformed(evt);
        }
    });

    multiply.setText("*");
    multiply.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            multiplyActionPerformed(evt);
        }
    });

    zero.setText("0");
    zero.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            zeroActionPerformed(evt);
        }
    });

    decimal.setText(".");
    decimal.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            decimalActionPerformed(evt);
        }
    });

    divide.setText("/");
    divide.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            divideActionPerformed(evt);
        }
    });

    enter.setText("Enter");
    enter.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            enterActionPerformed(evt);
        }
    });

    clear.setText("C");
    clear.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            clearActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(36, 36, 36)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(display)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(54, Short.MAX_VALUE))
    );

    jMenu4.setText("File");

    Home.setText("Home");
    Home.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            HomeActionPerformed(evt);
        }
    });
    jMenu4.add(Home);

    jMenuBar2.add(jMenu4);

    jMenu5.setText("Edit");
    jMenuBar2.add(jMenu5);

    setJMenuBar(jMenuBar2);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void HomeActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    Menu m = new Menu();
    m.setVisible(true);
}                                    

private void sixActionPerformed(java.awt.event.ActionEvent evt) {                                    
    // TODO add your handling code here:
    display.setText(display.getText() + six.getText());
}                                   

private void divideActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    display.setText(display.getText() + divide.getText());
}                                      

private void sevenActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    display.setText(display.getText() + seven.getText());
}                                     

private void eightActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    display.setText(display.getText() + eight.getText());
}                                     

private void nineActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    display.setText(display.getText() + nine.getText());
}                                    

private void plusActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    fNums = fNums + Double.parseDouble(display.getText());   
    display.setText("");
    add = true;
}                                    

private void fourActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    display.setText(display.getText() + four.getText());
}                                    

private void fiveActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    display.setText(display.getText() + five.getText());
}                                    

private void minusActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    fNums = fNums - Double.parseDouble(display.getText()); 
    display.setText("");
    sub = true;
}                                     

private void oneActionPerformed(java.awt.event.ActionEvent evt) {                                    
    // TODO add your handling code here:
    display.setText(display.getText() + one.getText());
}                                   

private void twoActionPerformed(java.awt.event.ActionEvent evt) {                                    
    // TODO add your handling code here:
    display.setText(display.getText() + two.getText());
}                                   

private void threeActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    display.setText(display.getText() + three.getText());
}                                     

private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    display.setText(display.getText() + multiply.getText());
}                                        

private void zeroActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    display.setText(display.getText() + zero.getText());
}                                    

private void decimalActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    display.setText(display.getText() + decimal.getText());
}                                       

private void enterActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your enter code here:
    if(add = true){
    sNums = fNums + Double.parseDouble(display.getText());
    display.setText(String.valueOf(sNums));
    fNums = 0;
    add = false;
    } else {
    if(sub = true){
    sNums = fNums + Double.parseDouble(display.getText());
    display.setText(String.valueOf(sNums));
    fNums = 0;
    sub = false;
    }
    }
}                                     

private void clearActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    display.setText("");
    fNums = 0;
    sNums = 0;
}                                     

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Calculator().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JMenuItem Home;
private javax.swing.JButton clear;
private javax.swing.JButton decimal;
private javax.swing.JTextField display;
private javax.swing.JButton divide;
private javax.swing.JButton eight;
private javax.swing.JButton enter;
private javax.swing.JButton five;
private javax.swing.JButton four;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton minus;
private javax.swing.JButton multiply;
private javax.swing.JButton nine;
private javax.swing.JButton one;
private javax.swing.JButton plus;
private javax.swing.JButton seven;
private javax.swing.JButton six;
private javax.swing.JButton three;
private javax.swing.JButton two;
private javax.swing.JButton zero;
// End of variables declaration                   
}

最佳答案

首先:您缺少 EnterActionPerformed 中的 ==。使用两个 = 符号。

第二:如果我理解正确的话,每当有一个“-”时,数字就会被否定。这是在写入第一个数字后发生的。

9-5 给你 4,因为你要否定第一个数字并将其添加到第二个数字: (-9)+5 =-4

错误在于以下代码中,当您按“-”按钮时,您正在执行 0-valueEntered。

private void minusActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    fNums = fNums - Double.parseDouble(display.getText()); 
    display.setText("");
    sub = true;
}        

关于java - Netbeans计算器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950077/

相关文章:

java - 如何使用反射获取以 List<SomeObject> 作为参数的方法

java - 如何使用java仅保存zip文件并删除其他文件夹?

java - 将特定数据加载到框架上的 jtable 上

java - 从 JTable 中删除列

java - 如何在java标签框中输入摄氏度?

java - 将现有 Swing 项目导入 NetBeans

JavaFX: ScrollView 滑动效果

java - Java实数的正则表达式

java - 将文本导入 JTable - 行不会分隔,它们只是创建一个非常长的行。 - Java Swing

java - 在 Swing 中使用线程的困难