Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 jCalculator;
- /**
- *
- * @author fmi
- */
- public class JavaCalculator extends javax.swing.JFrame {
- private double total = 0;
- private char operation;
- /**
- * Creates new form JavaCalculator
- */
- public JavaCalculator() {
- 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() {
- txtDisplay = new javax.swing.JTextField();
- jPanel1 = new javax.swing.JPanel();
- btnOne = new javax.swing.JButton();
- btnTwo = new javax.swing.JButton();
- btnThree = new javax.swing.JButton();
- jPanel2 = new javax.swing.JPanel();
- btnAdd = new javax.swing.JButton();
- btnEquals = new javax.swing.JButton();
- btnClear = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- btnOne.setText("1");
- btnOne.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnOneActionPerformed(evt);
- }
- });
- btnTwo.setText("2");
- btnTwo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnTwoActionPerformed(evt);
- }
- });
- btnThree.setText("3");
- btnThree.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnThreeActionPerformed(evt);
- }
- });
- 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()
- .addComponent(btnOne)
- .addGap(18, 18, 18)
- .addComponent(btnTwo)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
- .addComponent(btnThree)
- .addContainerGap())
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btnOne)
- .addComponent(btnTwo)
- .addComponent(btnThree))
- .addContainerGap(170, Short.MAX_VALUE))
- );
- btnAdd.setText("+");
- btnAdd.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnAddActionPerformed(evt);
- }
- });
- btnEquals.setText("=");
- btnEquals.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnEqualsActionPerformed(evt);
- }
- });
- btnClear.setText("Clear");
- btnClear.setToolTipText("");
- btnClear.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnClearActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
- jPanel2.setLayout(jPanel2Layout);
- jPanel2Layout.setHorizontalGroup(
- jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
- .addContainerGap(22, Short.MAX_VALUE)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(btnEquals)
- .addComponent(btnAdd))
- .addGap(31, 31, 31))
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(btnClear)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- jPanel2Layout.setVerticalGroup(
- jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(btnAdd)
- .addGap(18, 18, 18)
- .addComponent(btnEquals)
- .addGap(18, 18, 18)
- .addComponent(btnClear)
- .addContainerGap(78, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(36, 36, 36)
- .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(25, 25, 25)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
- .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(28, 28, 28)
- .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(22, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String display = txtDisplay.getText() + "1";
- txtDisplay.setText(display);
- }
- private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String display = txtDisplay.getText() + "2";
- txtDisplay.setText(display);
- }
- private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String display = txtDisplay.getText() + "3";
- txtDisplay.setText(display);
- }
- private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- total += Double.parseDouble(txtDisplay.getText());
- txtDisplay.setText("");
- }
- private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- double current = Double.parseDouble(txtDisplay.getText());
- total += current;
- txtDisplay.setText(Double.toString(total));
- }
- private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- total = 0;
- txtDisplay.setText("");
- }
- /**
- * @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(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(JavaCalculator.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 JavaCalculator().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnAdd;
- private javax.swing.JButton btnClear;
- private javax.swing.JButton btnEquals;
- private javax.swing.JButton btnOne;
- private javax.swing.JButton btnThree;
- private javax.swing.JButton btnTwo;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JPanel jPanel2;
- private javax.swing.JTextField txtDisplay;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement