You are on page 1of 9

package calculator; public class MainForm extends javax.swing.

JFrame { int first; int second; int total; int int int int plusClk; minusClk; multiClk; divideClk;

public MainForm() { 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(); txt = new javax.swing.JTextField(); btn7 = new javax.swing.JButton(); btn8 = new javax.swing.JButton(); btn9 = new javax.swing.JButton(); btn5 = new javax.swing.JButton(); btn4 = new javax.swing.JButton(); btn6 = new javax.swing.JButton(); btn1 = new javax.swing.JButton(); btnClear = new javax.swing.JButton(); btn3 = new javax.swing.JButton(); btn0 = new javax.swing.JButton(); BtnDivide = new javax.swing.JButton(); btnMulti = new javax.swing.JButton(); btnMinus = new javax.swing.JButton(); btnAdd = new javax.swing.JButton(); btn10 = new javax.swing.JButton(); btnEqual = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); txt.setEditable(false); txt.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N txt.setBorder(javax.swing.BorderFactory.createEtchedBorder()); btn7.setText("7"); btn7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn7ActionPerformed(evt); } });

btn8.setText("8"); btn8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn8ActionPerformed(evt); } }); btn9.setText("9"); btn9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn9ActionPerformed(evt); } }); btn5.setText("5"); btn5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn5ActionPerformed(evt); } }); btn4.setText("4"); btn4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn4ActionPerformed(evt); } }); btn6.setText("6"); btn6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn6ActionPerformed(evt); } }); btn1.setText("1"); btn1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn1ActionPerformed(evt); } }); btnClear.setText("C"); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btn3.setText("3"); btn3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn3ActionPerformed(evt); } }); btn0.setText("0"); btn0.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {

btn0ActionPerformed(evt); } }); BtnDivide.setText("/"); BtnDivide.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BtnDivideActionPerformed(evt); } }); btnMulti.setText("*"); btnMulti.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnMultiActionPerformed(evt); } }); btnMinus.setText("-"); btnMinus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnMinusActionPerformed(evt); } }); btnAdd.setText("+"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); btn10.setText("2"); btn10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn10ActionPerformed(evt); } }); btnEqual.setText("="); btnEqual.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEqualActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPan el1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel 1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGrou p() .addComponent(btn4, javax.swing.GroupLay out.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btn5, javax.swing.GroupLay out.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGrou p() .addComponent(btn7, javax.swing.GroupLay out.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btn8, javax.swing.GroupLay out.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(java x.swing.GroupLayout.Alignment.TRAILING) .addComponent(btn6, javax.swing.GroupLayout. PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn9, javax.swing.GroupLayout. PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING, false) .addComponent(btn1, javax.swing.GroupLayout. DEFAULT_SIZE, 41, Short.MAX_VALUE) .addComponent(btn0, javax.swing.GroupLayout. DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING) .addComponent(btnClear, javax.swing.GroupLay out.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn10, javax.swing.GroupLayout .PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING) .addComponent(btn3, javax.swing.GroupLayout. PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEqual)))) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING, false) .addComponent(btnAdd, javax.swing.GroupLayout.DEFAUL T_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnMinus, javax.swing.GroupLayout.DEFA ULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnMulti, javax.swing.GroupLayout.DEFA ULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(BtnDivide, javax.swing.GroupLayout.PRE FERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(txt, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE))) .addContainerGap()) );

jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 35, j avax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.BASELINE) .addComponent(btn7, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn8, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn9, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.BASELINE) .addComponent(btn4, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn5, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn6, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.BASELINE) .addComponent(btn1, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn3, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn10, javax.swing.GroupLayout.PREFERR ED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.BASELINE) .addComponent(btn0, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnClear, javax.swing.GroupLayout.PREF ERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(BtnDivide, javax.swing.GroupLayout.PREFERR ED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addComponent(btnMulti, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addComponent(btnMinus, javax.swing.GroupLayout.PREFERRE D_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.BASELINE) .addComponent(btnAdd, javax.swing.GroupLayout.PREFER RED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEqual, javax.swing.GroupLayout.PREF ERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(0, 11, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP ane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, j avax.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.PREFERRED_SIZE, j avax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE)) ); pack(); }// </editor-fold> private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(""); } private void btnEqualActionPerformed(java.awt.event.ActionEvent evt) { second=(Integer.parseInt(String.valueOf(txt.getText()))); if(plusClk > 0){ total=first+second; txt.setText((String.valueOf(total))); first=0; second=0; plusClk=0; } else if (minusClk > 0){ total=first-second; txt.setText((String.valueOf(total))); first=0; second=0; minusClk=0; }

else if (multiClk > 0){ total=first*second; txt.setText((String.valueOf(total))); first=0; second=0; multiClk=0; } else if (divideClk > 0){ total=first/second; txt.setText((String.valueOf(total))); first=0; second=0; divideClk=0; } } private void btn0ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"0"); } private void btn1ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"1"); } private void btn10ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"2"); } private void btn3ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"3"); } private void btn4ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"4"); } private void btn5ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"5"); } private void btn6ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"6"); } private void btn7ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"7"); }

private void btn8ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"8"); } private void btn9ActionPerformed(java.awt.event.ActionEvent evt) { txt.setText(txt.getText()+"9"); } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { first=(Integer.parseInt(String.valueOf(txt.getText()))); txt.setText(""); plusClk=1; } private void btnMinusActionPerformed(java.awt.event.ActionEvent evt) { first=(Integer.parseInt(String.valueOf(txt.getText()))); txt.setText(""); minusClk=1; } private void btnMultiActionPerformed(java.awt.event.ActionEvent evt) { first=(Integer.parseInt(String.valueOf(txt.getText()))); txt.setText(""); multiClk=1; } private void BtnDivideActionPerformed(java.awt.event.ActionEvent evt) { first=(Integer.parseInt(String.valueOf(txt.getText()))); txt.setText(""); divideClk=1; } /** * @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 cod e (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the d efault look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lo okandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIMana ger.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } }

} catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainForm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton BtnDivide; private javax.swing.JButton btn0; private javax.swing.JButton btn1; private javax.swing.JButton btn10; private javax.swing.JButton btn3; private javax.swing.JButton btn4; private javax.swing.JButton btn5; private javax.swing.JButton btn6; private javax.swing.JButton btn7; private javax.swing.JButton btn8; private javax.swing.JButton btn9; private javax.swing.JButton btnAdd; private javax.swing.JButton btnClear; private javax.swing.JButton btnEqual; private javax.swing.JButton btnMinus; private javax.swing.JButton btnMulti; private javax.swing.JPanel jPanel1; private javax.swing.JTextField txt; // End of variables declaration }

You might also like