0
@Chillivanilli is my inspiration
This is my first project
Download : SimpleCalculator
Source Code
Main.java
CalculatorForm.javaCode:/* * 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 Ebina */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here CalculatorForm form = new CalculatorForm(); form.setVisible(true); } }
Code:import javax.swing.JOptionPane; /* * 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 Ebina */ public class CalculatorForm extends javax.swing.JFrame { /** * Creates new form CalculatorForm */ public CalculatorForm() { 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() { FirstNumberText = new javax.swing.JTextField(); SecondNumberText = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); PlusButton = new javax.swing.JButton(); MinusButton = new javax.swing.JButton(); AnswerLabel = new javax.swing.JLabel(); MultiButton = new javax.swing.JButton(); DivideButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Simple Calculator by Einhver A2S.in"); SecondNumberText.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SecondNumberTextActionPerformed(evt); } }); jLabel1.setText("First number"); jLabel2.setText("Second number"); PlusButton.setText("+"); PlusButton.setToolTipText("Plus"); PlusButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PlusButtonActionPerformed(evt); } }); MinusButton.setText("-"); MinusButton.setToolTipText("Minus"); MinusButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MinusButtonActionPerformed(evt); } }); AnswerLabel.setText("Answer is :"); MultiButton.setText("*"); MultiButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MultiButtonActionPerformed(evt); } }); DivideButton.setText("/"); DivideButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { DivideButtonActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(AnswerLabel) .addGroup(layout.createSequentialGroup() .addComponent(PlusButton) .addGap(18, 18, 18) .addComponent(MinusButton) .addGap(18, 18, 18) .addComponent(MultiButton) .addGap(18, 18, 18) .addComponent(DivideButton)) .addComponent(jLabel2) .addComponent(jLabel1)) .addGap(0, 144, Short.MAX_VALUE)) .addComponent(FirstNumberText, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(SecondNumberText)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(FirstNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(24, 24, 24) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(SecondNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(37, 37, 37) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(PlusButton) .addComponent(MinusButton) .addComponent(MultiButton) .addComponent(DivideButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE) .addComponent(AnswerLabel) .addGap(52, 52, 52)) ); jLabel2.getAccessibleContext().setAccessibleDescription(""); pack(); }// </editor-fold> private void SecondNumberTextActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void PlusButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int number1, number2; try { number1 = Integer.parseInt( this.FirstNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad first number!", "Error", JOptionPane.ERROR_MESSAGE); return; } try { number2 = Integer.parseInt( this.SecondNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad second number!", "Error", JOptionPane.ERROR_MESSAGE); return; } int answer = number1 + number2; this.AnswerLabel.setText( "Answer is : "+ answer); } private void MinusButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int number1, number2; try { number1 = Integer.parseInt( this.FirstNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad first number!", "Error", JOptionPane.ERROR_MESSAGE); return; } try { number2 = Integer.parseInt( this.SecondNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad second number!", "Error", JOptionPane.ERROR_MESSAGE); return; } int answer = number1 - number2; this.AnswerLabel.setText( "Answer is : " + answer); } private void MultiButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int number1, number2; try { number1 = Integer.parseInt( this.FirstNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad first number!", "Error", JOptionPane.ERROR_MESSAGE); return; } try { number2 = Integer.parseInt( this.SecondNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad second number!", "Error", JOptionPane.ERROR_MESSAGE); return; } int answer = number1 * number2; this.AnswerLabel.setText( "Answer is : "+ answer); } private void DivideButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int number1, number2; try { number1 = Integer.parseInt( this.FirstNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad first number!", "Error", JOptionPane.ERROR_MESSAGE); return; } try { number2 = Integer.parseInt( this.SecondNumberText.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Bad second number!", "Error", JOptionPane.ERROR_MESSAGE); return; } int answer = number1 / number2; this.AnswerLabel.setText( "Answer is : "+ answer); } /** * @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(CalculatorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CalculatorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CalculatorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CalculatorForm.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 CalculatorForm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel AnswerLabel; private javax.swing.JButton DivideButton; private javax.swing.JTextField FirstNumberText; private javax.swing.JButton MinusButton; private javax.swing.JButton MultiButton; private javax.swing.JButton PlusButton; private javax.swing.JTextField SecondNumberText; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; // End of variables declaration }
Bookmarks