5 Replies - 1345 Views - Last Post: 29 May 2013 - 12:54 PM Rate Topic: -----

#1 shaikha alnuaimi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-January 13

calculator useing GUI

Posted 29 May 2013 - 10:09 AM

hi all


i have i university project to create an calculator . i almost finish it

but i had a problem with the clear butoom and i have to make

many oparation like multiplaction and addition to gother but i have problem with it

package examples;

import java.awt.FlowLayout;
import javax.swing.JPanel;

public class ContactEditor extends javax.swing.JFrame {

    char opp;

    /**
     * Creates new form ContactEditor
     */
    public ContactEditor() {
        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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();
        jButton17 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("E-mail Contacts");

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

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

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

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

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

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

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

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

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

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

        jButton11.setForeground(new java.awt.Color(255, 0, 51));
        jButton11.setText("/");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton12.setForeground(new java.awt.Color(255, 0, 0));
        jButton12.setText("+");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jButton13.setForeground(new java.awt.Color(255, 0, 51));
        jButton13.setText("*");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });

        jButton14.setForeground(new java.awt.Color(255, 0, 51));
        jButton14.setText(".");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        jButton15.setForeground(new java.awt.Color(255, 51, 51));
        jButton15.setText("=");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });

        jButton16.setForeground(new java.awt.Color(255, 0, 51));
        jButton16.setText("-");
        jButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton16ActionPerformed(evt);
            }
        });

        jButton17.setText("Exit");
        jButton17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton17ActionPerformed(evt);
            }
        });

        jToggleButton1.setText("Clear");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jButton1)
                                    .add(jButton4)
                                    .add(jButton7))
                                .add(18, 18, 18)
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                        .add(jPanel2Layout.createSequentialGroup()
                                            .add(jButton5)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .add(jButton6))
                                        .add(jPanel2Layout.createSequentialGroup()
                                            .add(jButton2)
                                            .add(18, 18, 18)
                                            .add(jButton3)))
                                    .add(jPanel2Layout.createSequentialGroup()
                                        .add(jButton8)
                                        .add(18, 18, 18)
                                        .add(jButton9))))
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(jButton10)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(jButton14)
                                .add(18, 18, 18)
                                .add(jButton15)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jButton13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButton12)
                            .add(jButton11)
                            .add(jButton16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(0, 0, Short.MAX_VALUE))
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jButton17)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jToggleButton1)))
                .add(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2)
                    .add(jButton3)
                    .add(jButton11))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton4)
                    .add(jButton5)
                    .add(jButton6)
                    .add(jButton13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton8)
                    .add(jButton7)
                    .add(jButton9)
                    .add(jButton12))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton10)
                    .add(jButton14)
                    .add(jButton15)
                    .add(jButton16))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jToggleButton1)
                    .add(jButton17))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jTextField1)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 42, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

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

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    }                                           

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("3");
        } else {
            jTextField1.setText(jTextField1.getText() + "3");
        }

        // TODO add your handling code here:
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("7");
        } else {
            jTextField1.setText(jTextField1.getText() + "7");
        }
        // TODO add your handling code here:
    }                                        

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // button plus"+"

        numbers[numCounter] = Double.parseDouble(jTextField1.getText());
        numCounter++;
        jTextField1.setText("");
        op = true;
        opp = '+';

    }                                         

    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {                                          

        double sum = 0;
        numbers[numCounter] = Double.parseDouble(jTextField1.getText());
        switch (opp) {
            case '+':
                sum = sum + numbers[0] + numbers[1];
                jTextField1.setText(sum + "");
                break;
            case '-':
                sum = sum + numbers[0] - numbers[1];
                jTextField1.setText(sum + "");
                break;
            case '*':
                sum = sum + numbers[0] * numbers[1];
                jTextField1.setText(sum + "");
                break;
            case '/':
                sum = sum + numbers[0] / numbers[1];
                jTextField1.setText(sum + "");
                break;
        //here is the problem 
        
        }


    }                                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("1");
        } else {
            jTextField1.setText(jTextField1.getText() + "1");
        }

        // TODO add your handling code here:
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("2");
        } else {
            jTextField1.setText(jTextField1.getText() + "2");
        }

        // TODO add your handling code here:
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("4");
        } else {
            jTextField1.setText(jTextField1.getText() + "4");
        }

        // TODO add your handling code here:
    }                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("5");
        } else {
            jTextField1.setText(jTextField1.getText() + "5");
        }

        // TODO add your handling code here:
    }                                        

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("6");
        } else {
            jTextField1.setText(jTextField1.getText() + "6");

        }
        // TODO add your handling code here:
    }                                        

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("8");
        } else {
            jTextField1.setText(jTextField1.getText() + "8");

        }
        // TODO add your handling code here:
    }                                        

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("9");
        } else {
            jTextField1.setText(jTextField1.getText() + "9");
        }

        // TODO add your handling code here:
    }                                        

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        if (op) {
            jTextField1.setText("0");
            op = false;
        }
        if (jTextField1.getText().contentEquals("0")) {
            jTextField1.setText("0");
        } else {
            jTextField1.setText(jTextField1.getText() + "0");
        }
    }                                         

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // buttom mines "-"
        numbers[numCounter] = Double.parseDouble(jTextField1.getText());
        numCounter++;
        jTextField1.setText("");
        op = true;
        opp = '-';
    }                                         

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        //buttom multiplaction '*'
        numbers[numCounter] = Double.parseDouble(jTextField1.getText());
        numCounter++;
        jTextField1.setText("");
        op = true;
        opp = '*';
    }                                         

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        //buttom divition '/'
        numbers[numCounter] = Double.parseDouble(jTextField1.getText());
        numCounter++;
        jTextField1.setText("");
        op = true;
        opp = '/';
    }                                         

    private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        //here is the problem 
    }

    /**
     * @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 {
            javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels = javax.swing.UIManager.getInstalledLookAndFeels();
            for (int idx = 0; idx < installedLookAndFeels.length; idx++) {
                if ("Nimbus".equals(installedLookAndFeels[idx].getName())) {
                    javax.swing.UIManager.setLookAndFeel(installedLookAndFeels[idx].getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ContactEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ContactEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ContactEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ContactEditor.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 ContactEditor().setVisible(true);
            }
        });
    }
    //my own varible
    double numbers[] = new double[10];
    int numCounter = 0;
    Boolean op = false;
    // Variables declaration - do not modify
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration
}



Is This A Good Question/Topic? 0
  • +

Replies To: calculator useing GUI

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,390
  • Joined: 27-December 08

Re: calculator useing GUI

Posted 29 May 2013 - 11:34 AM

We don't support the NetBeans GUI Builder. You should really write your GUI code by hand. It will be much cleaner and much simpler.
Was This Post Helpful? 0
  • +
  • -

#3 shaikha alnuaimi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-January 13

Re: calculator useing GUI

Posted 29 May 2013 - 11:47 AM

View Postmacosxnerd101, on 29 May 2013 - 11:34 AM, said:

We don't support the NetBeans GUI Builder. You should really write your GUI code by hand. It will be much cleaner and much simpler.

what kind of program to do the GUI
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: calculator useing GUI

Posted 29 May 2013 - 11:48 AM

Write your own GUI ! It will probably cut the number of lines of code by at least 2 and you will end up with code that you can understand because you wrote it
Was This Post Helpful? 0
  • +
  • -

#5 Virtent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-May 13

Re: calculator useing GUI

Posted 29 May 2013 - 12:13 PM

You might be interested in this, OP.
http://www.dreaminco...r-using-jframe/

Hope it helps.
Was This Post Helpful? 1
  • +
  • -

#6 shaikha alnuaimi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-January 13

Re: calculator useing GUI

Posted 29 May 2013 - 12:54 PM

thanks all know i under stand what you mean :bigsmile:

:smartass: wish me luck
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1