NetBeans实现计算器的加减乘除

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author rt
 */
public class jisuan1 extends javax.swing.JFrame {
     int firstNum;
     int secondNum;
     int fangfa;


    /**
     * Creates new form jisuan1
     */
    public jisuan1() {
        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() {


        jPanel1 = new javax.swing.JPanel();
        jBut_7 = new javax.swing.JButton();
        jBut_4 = new javax.swing.JButton();
        jBut_1 = new javax.swing.JButton();
        jBut_0 = new javax.swing.JButton();
        jBut_2 = new javax.swing.JButton();
        jBut_9 = new javax.swing.JButton();
        jBut_8 = new javax.swing.JButton();
        jBut_5 = new javax.swing.JButton();
        jBut_3 = new javax.swing.JButton();
        jBut_6 = new javax.swing.JButton();
        jBut_dengyu = new javax.swing.JButton();
        jBut_C = new javax.swing.JButton();
        jBut_jian = new javax.swing.JButton();
        jBut_add = new javax.swing.JButton();
        jBut_cheng = new javax.swing.JButton();
        jBut_chu = new javax.swing.JButton();
        lab1 = new javax.swing.JLabel();
        txt = new javax.swing.JTextField();


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


        jPanel1.setLayout(new java.awt.GridLayout(4, 4));


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


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


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


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


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


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


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


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


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


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


        jBut_dengyu.setText("=");
        jBut_dengyu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_dengyuActionPerformed(evt);
            }
        });


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


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


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


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


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


        lab1.setText("结果");


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lab1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(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))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jBut_4)
                                    .addComponent(jBut_7)
                                    .addComponent(jBut_1, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jBut_0, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBut_C)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jBut_dengyu))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jBut_8)
                                        .addComponent(jBut_5))
                                    .addComponent(jBut_2))
                                .addGap(18, 18, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jBut_3)
                                    .addComponent(jBut_6)
                                    .addComponent(jBut_9))))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBut_add)
                            .addComponent(jBut_jian)
                            .addComponent(jBut_chu)
                            .addComponent(jBut_cheng))))
                .addGap(161, 161, 161))
        );
        layout.setVerticalGroup(
            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)
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lab1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBut_7)
                    .addComponent(jBut_8)
                    .addComponent(jBut_9)
                    .addComponent(jBut_add))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBut_4)
                    .addComponent(jBut_5)
                    .addComponent(jBut_6)
                    .addComponent(jBut_jian))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBut_1)
                    .addComponent(jBut_2)
                    .addComponent(jBut_3)
                    .addComponent(jBut_cheng))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBut_C)
                    .addComponent(jBut_0)
                    .addComponent(jBut_dengyu)
                    .addComponent(jBut_chu))
                .addContainerGap(50, Short.MAX_VALUE))
        );


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


    private void jBut_0ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        //获取数字0
        txt.setText(txt.getText()+"0");
    }                                      


    private void jBut_1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"1");
    }                                      


    private void jBut_2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"2");
    }                                      


    private void jBut_3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"3");
    }                                      


    private void jBut_4ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"4");
    }                                      


    private void jBut_5ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"5");
    }                                      


    private void jBut_6ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"6");
    }                                      


    private void jBut_7ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"7");
    }                                      


    private void jBut_8ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"8");
    }                                      


    private void jBut_9ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         txt.setText(txt.getText()+"9");
    }                                      


    private void jBut_addActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
         //获取第一个数字
         String a=txt.getText();//获取文本框中的值
         firstNum=Integer.parseInt(a);//转换类型
         txt.setText(null);
         fangfa=1;
    }                                        


    private void jBut_CActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        txt.setText(null);
    }                                      


    private void jBut_dengyuActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        //获取第二个数字
        String b=txt.getText();
        secondNum=Integer.parseInt(b);
        switch(fangfa){
            case 1:
                int result1=(firstNum+secondNum);
                String r1=String.valueOf(result1);
                txt.setText(r1);
                break;
            case 2:
                int result2=(firstNum-secondNum);
                String r2=String.valueOf(result2);
                txt.setText(r2);
                break;
            case 3:
                int result3=(firstNum*secondNum);
                String r3=String.valueOf(result3);
                txt.setText(r3);
                break;
            case 4:
                int result4=(firstNum/secondNum);
                String r4=String.valueOf(result4);
                txt.setText(r4);
                break;
        }
       
    }                                           


    private void jBut_jianActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        //获取第一个数字
        String a=txt.getText();
        firstNum=Integer.parseInt(a);//强制类型转换
        txt.setText(null);
        fangfa=2;
    }                                         


    private void jBut_chengActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        String a=txt.getText();
        firstNum=Integer.parseInt(a);
        txt.setText(null);
        fangfa=3;
    }                                          


    private void jBut_chuActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String a=txt.getText();
        firstNum=Integer.valueOf(a);
        txt.setText(null);
        fangfa=4;
    }                                        


    /**
     * @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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(jisuan1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(jisuan1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(jisuan1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(jisuan1.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 jisuan1().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify                     
    private javax.swing.JButton jBut_0;
    private javax.swing.JButton jBut_1;
    private javax.swing.JButton jBut_2;
    private javax.swing.JButton jBut_3;
    private javax.swing.JButton jBut_4;
    private javax.swing.JButton jBut_5;
    private javax.swing.JButton jBut_6;
    private javax.swing.JButton jBut_7;
    private javax.swing.JButton jBut_8;
    private javax.swing.JButton jBut_9;
    private javax.swing.JButton jBut_C;
    private javax.swing.JButton jBut_add;
    private javax.swing.JButton jBut_cheng;
    private javax.swing.JButton jBut_chu;
    private javax.swing.JButton jBut_dengyu;
    private javax.swing.JButton jBut_jian;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lab1;
    private javax.swing.JTextField txt;
    // End of variables declaration                   

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值