By Salvia Fatma
This is a simple calculator which can do basic arithmetic operations. It has been built using Java and used the NetBeans platform to write code.
Java
Java Swing
NetBeans IDE
It can do addition, substraction, multiplication, division and change the sign of number. We can also enter decimal number.
package calculations; /** * * @author HP */ public class calculators extends javax.swing.JFrame { double firstNumber; double secondNumber; double result; String operations; /** * Creates new form calculators */ public calculators() { 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") // private void initComponents() { display = new javax.swing.JTextField(); jbtn1 = new javax.swing.JButton(); jbtn2 = new javax.swing.JButton(); jbtn3 = new javax.swing.JButton(); jbtn4 = new javax.swing.JButton(); jbtn5 = new javax.swing.JButton(); jbtn6 = new javax.swing.JButton(); jbtn7 = new javax.swing.JButton(); jbtn8 = new javax.swing.JButton(); jbtn9 = new javax.swing.JButton(); jbtn10 = new javax.swing.JButton(); jbtn11 = new javax.swing.JButton(); jbtn12 = new javax.swing.JButton(); jbtn13 = new javax.swing.JButton(); jbtn14 = new javax.swing.JButton(); jbtn15 = new javax.swing.JButton(); jbtn16 = new javax.swing.JButton(); jbtn17 = new javax.swing.JButton(); jbtn18 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); display.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N display.setHorizontalAlignment(javax.swing.JTextField.RIGHT); jbtn1.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn1.setText("1"); jbtn1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn1ActionPerformed(evt); } }); jbtn2.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn2.setText("2"); jbtn2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn2ActionPerformed(evt); } }); jbtn3.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn3.setText("3"); jbtn3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn3ActionPerformed(evt); } }); jbtn4.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn4.setText("4"); jbtn4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn4ActionPerformed(evt); } }); jbtn5.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn5.setText("5"); jbtn5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn5ActionPerformed(evt); } }); jbtn6.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn6.setText("6"); jbtn6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn6ActionPerformed(evt); } }); jbtn7.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn7.setText("7"); jbtn7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn7ActionPerformed(evt); } }); jbtn8.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn8.setText("8"); jbtn8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn8ActionPerformed(evt); } }); jbtn9.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn9.setText("9"); jbtn9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn9ActionPerformed(evt); } }); jbtn10.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn10.setText("0"); jbtn10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn10ActionPerformed(evt); } }); jbtn11.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn11.setText("+"); jbtn11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn11ActionPerformed(evt); } }); jbtn12.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn12.setText("-"); jbtn12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn12ActionPerformed(evt); } }); jbtn13.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn13.setText("*"); jbtn13.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn13ActionPerformed(evt); } }); jbtn14.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn14.setText("/"); jbtn14.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn14ActionPerformed(evt); } }); jbtn15.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn15.setText("+/-"); jbtn15.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn15ActionPerformed(evt); } }); jbtn16.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn16.setText("."); jbtn16.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn16ActionPerformed(evt); } }); jbtn17.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn17.setText("C"); jbtn17.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn17ActionPerformed(evt); } }); jbtn18.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N jbtn18.setText("="); jbtn18.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtn18ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(display) .addGroup(layout.createSequentialGroup() .addComponent(jbtn1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jbtn5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jbtn9, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn10, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn11, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn12, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup() .addComponent(jbtn13, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn14, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jbtn17, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jbtn18, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jbtn15, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jbtn16, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap(20, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(17, 17, 17) .addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jbtn1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn3, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn4, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jbtn5, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn6, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn7, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn8, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jbtn9, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn10, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn11, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn12, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jbtn13, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn14, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn15, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn16, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jbtn17, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jbtn18, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(21, Short.MAX_VALUE)) ); pack(); }// private void jbtn18ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String answer; secondNumber = Double.parseDouble(display.getText()); if(operations == "+") { result = firstNumber + secondNumber ; answer = String.format("%.0f",result); display.setText(answer); } else if(operations == "-") { result = firstNumber - secondNumber ; answer = String.format("%.0f",result); display.setText(answer); } else if(operations == "*") { result = firstNumber * secondNumber ; answer = String.format("%.0f",result); display.setText(answer); } else if(operations == "/") { result = firstNumber / secondNumber ; answer = String.format("%.0f",result); display.setText(answer); } } private void jbtn3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn3.getText(); display.setText(EnterNumber); } private void jbtn1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn1.getText(); display.setText(EnterNumber); } private void jbtn2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn2.getText(); display.setText(EnterNumber); } private void jbtn4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn4.getText(); display.setText(EnterNumber); } private void jbtn5ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn5.getText(); display.setText(EnterNumber); } private void jbtn6ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn6.getText(); display.setText(EnterNumber); } private void jbtn7ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn7.getText(); display.setText(EnterNumber); } private void jbtn8ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn8.getText(); display.setText(EnterNumber); } private void jbtn9ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn9.getText(); display.setText(EnterNumber); } private void jbtn10ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn10.getText(); display.setText(EnterNumber); } private void jbtn11ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: firstNumber = Double.parseDouble(display.getText()); display.setText(""); operations = "+"; } private void jbtn12ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: firstNumber = Double.parseDouble(display.getText()); display.setText(""); operations = "-"; } private void jbtn13ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: firstNumber = Double.parseDouble(display.getText()); display.setText(""); operations = "*"; } private void jbtn14ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: firstNumber = Double.parseDouble(display.getText()); display.setText(""); operations = "/"; } private void jbtn16ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String EnterNumber = display.getText() + jbtn16.getText(); display.setText(EnterNumber); } private void jbtn15ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: double ops = Double.parseDouble(String.valueOf(display.getText())); ops = ops*(-1); display.setText(String.valueOf(ops)); } private void jbtn17ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: display.setText(""); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* 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(calculators.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(calculators.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(calculators.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(calculators.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new calculators().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextField display; private javax.swing.JButton jbtn1; private javax.swing.JButton jbtn10; private javax.swing.JButton jbtn11; private javax.swing.JButton jbtn12; private javax.swing.JButton jbtn13; private javax.swing.JButton jbtn14; private javax.swing.JButton jbtn15; private javax.swing.JButton jbtn16; private javax.swing.JButton jbtn17; private javax.swing.JButton jbtn18; private javax.swing.JButton jbtn2; private javax.swing.JButton jbtn3; private javax.swing.JButton jbtn4; private javax.swing.JButton jbtn5; private javax.swing.JButton jbtn6; private javax.swing.JButton jbtn7; private javax.swing.JButton jbtn8; private javax.swing.JButton jbtn9; // End of variables declaration }
Submitted by Salvia Fatma (SalviaFatma123)
Download packets of source code on Coders Packet
Comments