idzik icon

Programowanie okienkowe/Java/Netbeans-Calculator-wersja-1

idzik | PRO | 01/06/16 11:54:51 AM UTC | 0 ⭐ | 341 👁️ | Never ⏰ | []
Java |

23.76 KB

|

None

|

0 👍

/

0 👎

/*
 * 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 Tomasz
 */
public class Kalkulator extends javax.swing.JFrame {
    private double pierwsza;
    private double druga;
    private double wynik;
    
    private boolean plus=false;
    private boolean minus=false;
    private boolean pomnoz=false;
    private boolean podziel=false;
    /**
     * Creates new form Kalkulator
     */
    public Kalkulator() {
        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();
        przycisk7 = new javax.swing.JButton();
        pole = new javax.swing.JTextField();
        przycisk8 = new javax.swing.JButton();
        przycisk9 = new javax.swing.JButton();
        przyciskplus = new javax.swing.JButton();
        przyciskczysc = new javax.swing.JButton();
        przycisk4 = new javax.swing.JButton();
        przycisk5 = new javax.swing.JButton();
        przycisk6 = new javax.swing.JButton();
        przyciskminus = new javax.swing.JButton();
        przycisksilnia = new javax.swing.JButton();
        przycisk1 = new javax.swing.JButton();
        przycisk2 = new javax.swing.JButton();
        przycisk3 = new javax.swing.JButton();
        przyciskpomnoz = new javax.swing.JButton();
        przyciskoblicz = new javax.swing.JButton();
        przycisk0 = new javax.swing.JButton();
        przyciskprzecinek = new javax.swing.JButton();
        przyciskpodziel = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        przycisk7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk7.setText("7");
        przycisk7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk7ActionPerformed(evt);
            }
        });
 
        pole.setEditable(false);
        pole.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        pole.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        pole.setText("0");
 
        przycisk8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk8.setText("8");
        przycisk8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk8ActionPerformed(evt);
            }
        });
 
        przycisk9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk9.setText("9");
        przycisk9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk9ActionPerformed(evt);
            }
        });
 
        przyciskplus.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przyciskplus.setText("+");
        przyciskplus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przyciskplusActionPerformed(evt);
            }
        });
 
        przyciskczysc.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przyciskczysc.setText("C");
        przyciskczysc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przyciskczyscActionPerformed(evt);
            }
        });
 
        przycisk4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk4.setText("4");
        przycisk4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk4ActionPerformed(evt);
            }
        });
 
        przycisk5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk5.setText("5");
        przycisk5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk5ActionPerformed(evt);
            }
        });
 
        przycisk6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk6.setText("6");
        przycisk6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk6ActionPerformed(evt);
            }
        });
 
        przyciskminus.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przyciskminus.setText("-");
 
        przycisksilnia.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisksilnia.setText("!");
 
        przycisk1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk1.setText("1");
        przycisk1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk1ActionPerformed(evt);
            }
        });
 
        przycisk2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk2.setText("2");
        przycisk2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk2ActionPerformed(evt);
            }
        });
 
        przycisk3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk3.setText("3");
        przycisk3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk3ActionPerformed(evt);
            }
        });
 
        przyciskpomnoz.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przyciskpomnoz.setText("*");
 
        przyciskoblicz.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przyciskoblicz.setText("=");
        przyciskoblicz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przyciskobliczActionPerformed(evt);
            }
        });
 
        przycisk0.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przycisk0.setText("0");
        przycisk0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przycisk0ActionPerformed(evt);
            }
        });
 
        przyciskprzecinek.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przyciskprzecinek.setText(",");
 
        przyciskpodziel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        przyciskpodziel.setText("/");
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(pole)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(przycisk7, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(przycisk8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(przycisk9, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(przyciskplus, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(przyciskczysc, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(przycisk4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(przycisk5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(przycisk6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(przyciskminus, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(przycisksilnia, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(przycisk1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(przycisk2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(przycisk3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(przyciskpomnoz, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(przycisk0, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(przyciskprzecinek, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(przyciskpodziel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(przyciskoblicz, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(pole, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(przycisk7, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przycisk8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przycisk9, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przyciskplus, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przyciskczysc, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(przycisk4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przycisk5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przycisk6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przyciskminus, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(przycisksilnia, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(przycisk1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(przycisk2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(przycisk3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(przyciskpomnoz, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(przycisk0, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(przyciskprzecinek, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(przyciskpodziel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(przyciskoblicz, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
 
        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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
 
        pack();
    }// </editor-fold>                        
 
    private void przycisk7ActionPerformed(java.awt.event.ActionEvent evt) {                                          
       if (pole.getText().equals("0")){
           pole.setText(przycisk7.getText());
       }
       else pole.setText(pole.getText()+przycisk7.getText());
    }                                         
 
    private void przycisk8ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk8.getText());
       }
       else pole.setText(pole.getText()+przycisk8.getText());
    }                                         
 
    private void przycisk9ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk9.getText());
       }
       else pole.setText(pole.getText()+przycisk9.getText());
    }                                         
 
    private void przycisk4ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk4.getText());
       }
       else pole.setText(pole.getText()+przycisk4.getText());
    }                                         
 
    private void przycisk5ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk5.getText());
       }
       else pole.setText(pole.getText()+przycisk5.getText());
    }                                         
 
    private void przycisk6ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk6.getText());
       }
       else pole.setText(pole.getText()+przycisk6.getText());
    }                                         
 
    private void przycisk1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk1.getText());
       }
       else pole.setText(pole.getText()+przycisk1.getText());
    }                                         
 
    private void przycisk2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk2.getText());
       }
       else pole.setText(pole.getText()+przycisk2.getText());
    }                                         
 
    private void przycisk3ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         if (pole.getText().equals("0")){
           pole.setText(przycisk3.getText());
       }
       else pole.setText(pole.getText()+przycisk3.getText());
    }                                         
 
    private void przycisk0ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        if (pole.getText().equals("0")){
           pole.setText(przycisk0.getText());
       }
       else pole.setText(pole.getText()+przycisk0.getText());
    }                                         
 
    private void przyciskczyscActionPerformed(java.awt.event.ActionEvent evt) {                                              
        pole.setText("0");
    }                                             
 
    private void przyciskplusActionPerformed(java.awt.event.ActionEvent evt) {                                             
        pierwsza=Double.parseDouble(pole.getText());
        plus=true;
        pole.setText("0");
    }                                            
 
    private void przyciskobliczActionPerformed(java.awt.event.ActionEvent evt) {                                               
        druga=Double.parseDouble(pole.getText());
        plus=false;
        wynik=pierwsza+druga;
        pole.setText(String.valueOf(wynik));
        //po zakończeniu działania
        pierwsza=0;
        druga=0;
        wynik=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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.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 Kalkulator().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField pole;
    private javax.swing.JButton przycisk0;
    private javax.swing.JButton przycisk1;
    private javax.swing.JButton przycisk2;
    private javax.swing.JButton przycisk3;
    private javax.swing.JButton przycisk4;
    private javax.swing.JButton przycisk5;
    private javax.swing.JButton przycisk6;
    private javax.swing.JButton przycisk7;
    private javax.swing.JButton przycisk8;
    private javax.swing.JButton przycisk9;
    private javax.swing.JButton przyciskczysc;
    private javax.swing.JButton przyciskminus;
    private javax.swing.JButton przyciskoblicz;
    private javax.swing.JButton przyciskplus;
    private javax.swing.JButton przyciskpodziel;
    private javax.swing.JButton przyciskpomnoz;
    private javax.swing.JButton przyciskprzecinek;
    private javax.swing.JButton przycisksilnia;
    // End of variables declaration                   
}

Comments