How to make a simple calculator with GUI in JAVA (using Netbeans)?
First of all create a new package , then create a simple class.
Then create a new JFrame Form class.
On the right side you will find every tool needed like buttons textfield radio buttons etc.
We'll need the buttons and text field and a JLabel
Drag and drop required buttons you want to appear on the calculator
eg.
1,2,3,4,5,6,7,8,9,0,+,-,*,/,=,AC,OFF
arrange them all
then on double clicking any button you will enter the source code tab,
for digits 0-9
you have to write this code:
String Number =answer.getText() + one.getText();
answer.setText(Number);
Here answer is the jTextFields variable name you can right click the text field and click change variable name.
similarly you have to set the code for every digit then
for operations you have to write the code:
for "+" operation:
firstnumber = Double.parseDouble(answer.getText());
jLabel1.setText(answer.getText()+"+");
answer.setText("");
operations="+";
for others change the "+" with required operation:
SOME SCREENSHOTS:
HERES THE FULL CODE IF YOU WANT TO COPY:
Code:
Then create a new JFrame Form class.
On the right side you will find every tool needed like buttons textfield radio buttons etc.
We'll need the buttons and text field and a JLabel
Drag and drop required buttons you want to appear on the calculator
eg.
1,2,3,4,5,6,7,8,9,0,+,-,*,/,=,AC,OFF
arrange them all
then on double clicking any button you will enter the source code tab,
for digits 0-9
you have to write this code:
String Number =answer.getText() + one.getText();
answer.setText(Number);
Here answer is the jTextFields variable name you can right click the text field and click change variable name.
similarly you have to set the code for every digit then
for operations you have to write the code:
for "+" operation:
firstnumber = Double.parseDouble(answer.getText());
jLabel1.setText(answer.getText()+"+");
answer.setText("");
operations="+";
for others change the "+" with required operation:
SOME SCREENSHOTS:
HERES THE FULL CODE IF YOU WANT TO COPY:
Code:
/* * 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. */ package calculatorgui; import javax.swing.JFrame; import javax.swing.JOptionPane; /** * * @author Umair */ public class Calculator extends javax.swing.JFrame { double firstnumber; double secondnumber; double result; String operations; /** * Creates new form JFrameGui */ public Calculator() { 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(); answer = new javax.swing.JTextField(); divide = new javax.swing.JButton(); one = new javax.swing.JButton(); minus = new javax.swing.JButton(); plus = new javax.swing.JButton(); four = new javax.swing.JButton(); zero = new javax.swing.JButton(); eight = new javax.swing.JButton(); seven = new javax.swing.JButton(); two = new javax.swing.JButton(); multiply = new javax.swing.JButton(); decimal = new javax.swing.JButton(); nine = new javax.swing.JButton(); six = new javax.swing.JButton(); five = new javax.swing.JButton(); three = new javax.swing.JButton(); equal = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); about = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); About = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Calculator"); setLocation(new java.awt.Point(500, 250)); setResizable(false); jPanel1.setBackground(new java.awt.Color(204, 204, 255)); jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); answer.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N answer.setHorizontalAlignment(javax.swing.JTextField.RIGHT); answer.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); answer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { answerActionPerformed(evt); } }); divide.setBackground(new java.awt.Color(255, 153, 0)); divide.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N divide.setText("/"); divide.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { divideActionPerformed(evt); } }); one.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N one.setText("1"); one.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { oneFocusGained(evt); } }); one.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { oneActionPerformed(evt); } }); minus.setBackground(new java.awt.Color(255, 153, 0)); minus.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N minus.setText("-"); minus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { minusActionPerformed(evt); } }); plus.setBackground(new java.awt.Color(255, 153, 0)); plus.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N plus.setText("+"); plus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { plusActionPerformed(evt); } }); four.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N four.setText("4"); four.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fourActionPerformed(evt); } }); zero.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N zero.setText("0"); zero.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { zeroActionPerformed(evt); } }); eight.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N eight.setText("8"); eight.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { eightActionPerformed(evt); } }); seven.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N seven.setText("7"); seven.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sevenActionPerformed(evt); } }); two.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N two.setText("2"); two.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { twoActionPerformed(evt); } }); multiply.setBackground(new java.awt.Color(255, 153, 0)); multiply.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N multiply.setText("*"); multiply.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { multiplyActionPerformed(evt); } }); decimal.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N decimal.setText("."); decimal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { decimalActionPerformed(evt); } }); nine.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N nine.setText("9"); nine.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nineActionPerformed(evt); } }); six.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N six.setText("6"); six.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sixActionPerformed(evt); } }); five.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N five.setText("5"); five.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fiveActionPerformed(evt); } }); three.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N three.setText("3"); three.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { threeActionPerformed(evt); } }); equal.setBackground(new java.awt.Color(255, 153, 0)); equal.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N equal.setText("="); equal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { equalActionPerformed(evt); } }); jButton1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N jButton1.setText("AC"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N jButton2.setText("OFF"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 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(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(zero, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE) .addComponent(one, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(equal, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE) .addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap()) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(answer, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0)))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(answer, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(8, 8, 8) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(equal, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(13, Short.MAX_VALUE)) ); about.setText("File"); about.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { aboutActionPerformed(evt); } }); jMenuItem1.setText("Clear"); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); about.add(jMenuItem1); About.setText("About"); About.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AboutActionPerformed(evt); } }); about.add(About); jMenuItem2.setText("Exit"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); about.add(jMenuItem2); jMenuBar1.add(about); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> private void aboutActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(null, "This app is made by Umair Saifullah", "", 1); } private void AboutActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(null, "This calculator is made by Umair Saifullah", "", 1); } private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { answer.setText(""); } private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setText(""); answer.setText(""); } private void equalActionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setText(""); String answer2; secondnumber = Double.parseDouble(answer.getText()); if(operations == "+"){ result = firstnumber + secondnumber; answer2 = String.format("%.0f", result); answer.setText(answer2); } else if(operations == "-"){ result = firstnumber - secondnumber; answer2 = String.format("%.0f", result); answer.setText(answer2); } else if(operations == "*"){ result = firstnumber * secondnumber; answer2 = String.format("%.0f", result); answer.setText(answer2); } else if(operations == "/"){ result = firstnumber / secondnumber; answer2 = String.format("%.0f", result); answer.setText(answer2); } } private void threeActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + three.getText(); answer.setText(Number); } private void fiveActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + five.getText(); answer.setText(Number); } private void sixActionPerformed(java.awt.event.ActionEvent evt) { String Number =answer.getText() + six.getText(); answer.setText(Number); } private void nineActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + nine.getText(); answer.setText(Number); } private void decimalActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + decimal.getText(); answer.setText(Number); } private void multiplyActionPerformed(java.awt.event.ActionEvent evt) { firstnumber = Double.parseDouble(answer.getText()); jLabel1.setText(answer.getText()+"*"); answer.setText(""); operations="*"; } private void twoActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + two.getText(); answer.setText(Number); } private void sevenActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + seven.getText(); answer.setText(Number); } private void eightActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + eight.getText(); answer.setText(Number); } private void zeroActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String Number =answer.getText() + zero.getText(); answer.setText(Number); } private void fourActionPerformed(java.awt.event.ActionEvent evt) { String Number = answer.getText() + four.getText(); answer.setText(Number); } private void plusActionPerformed(java.awt.event.ActionEvent evt) { firstnumber = Double.parseDouble(answer.getText()); jLabel1.setText(answer.getText()+"+"); answer.setText(""); operations="+"; } private void minusActionPerformed(java.awt.event.ActionEvent evt) { firstnumber = Double.parseDouble(answer.getText()); jLabel1.setText(answer.getText()+"-"); answer.setText(""); operations="-"; } private void oneActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String Number =answer.getText() + one.getText(); answer.setText(Number); } private void oneFocusGained(java.awt.event.FocusEvent evt) { // TODO add your handling code here: } private void divideActionPerformed(java.awt.event.ActionEvent evt) { firstnumber = Double.parseDouble(answer.getText()); jLabel1.setText(answer.getText()+"/"); answer.setText(""); operations="/"; } private void answerActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @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(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Calculator().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JMenuItem About; private javax.swing.JMenu about; private javax.swing.JTextField answer; private javax.swing.JButton decimal; private javax.swing.JButton divide; private javax.swing.JButton eight; private javax.swing.JButton equal; private javax.swing.JButton five; private javax.swing.JButton four; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JPanel jPanel1; private javax.swing.JButton minus; private javax.swing.JButton multiply; private javax.swing.JButton nine; private javax.swing.JButton one; private javax.swing.JButton plus; private javax.swing.JButton seven; private javax.swing.JButton six; private javax.swing.JButton three; private javax.swing.JButton two; private javax.swing.JButton zero; // End of variables declaration }
Comments
Post a Comment