Jump to content
Parabot - Bot for Ikov, DreamScape, SoulPlay, RuneWild, and more
Sign in to follow this  
Regex

Basic Calculator

Recommended Posts

just incase anyone was wanting to learn, here you go :)

package org.util.tools;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Calculator extends javax.swing.JFrame implements ActionListener {

    public Calculator() {

	    initComponents();

    }

    static int total = 0;

    static char method = 'n';

    static boolean updaterequired;

    @SuppressWarnings("unchecked")

    private void initComponents() {

	    out = new javax.swing.JTextField();

	    one = new javax.swing.JButton();

	    two = new javax.swing.JButton();

	    three = new javax.swing.JButton();

	    times = new javax.swing.JButton();

	    divide = new javax.swing.JButton();

	    six = new javax.swing.JButton();

	    five = new javax.swing.JButton();

	    four = new javax.swing.JButton();

	    plus = new javax.swing.JButton();

	    nine = new javax.swing.JButton();

	    eight = new javax.swing.JButton();

	    seven = new javax.swing.JButton();

	    minus = new javax.swing.JButton();

	    calc = new javax.swing.JButton();

	    clear = new javax.swing.JButton();

	    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	    setTitle("Calculator | Regex");

	    out.setEditable(false);

	    one.setText("1");

	    one.addActionListener(this);

	    two.setText("2");

	    two.addActionListener(this);

	    three.setText("3");

	    three.addActionListener(this);

	    times.setText("X");

	    times.addActionListener(this);

	    divide.setText("÷");

	    divide.addActionListener(this);

	    six.setText("6");

	    six.addActionListener(this);

	    five.setText("5");

	    five.addActionListener(this);

	    four.setText("4");

	    four.addActionListener(this);

	    plus.setText("+");

	    plus.addActionListener(this);

	    nine.setText("9");

	    nine.addActionListener(this);

	    eight.setText("8");

	    eight.addActionListener(this);

	    seven.setText("7");

	    seven.addActionListener(this);

	    minus.setText("-");

	    minus.addActionListener(this);

	    calc.setText("=");

	    calc.addActionListener(this);

	    clear.setText("C");

	    clear.addActionListener(this);

	    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()

											    .addComponent(out, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)

											    .addGap(0, 0, Short.MAX_VALUE))

									    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

											    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

													    .addGroup(layout.createSequentialGroup()

															    .addComponent(one)

															    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

															    .addComponent(two)

															    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

															    .addComponent(three))

													    .addGroup(layout.createSequentialGroup()

															    .addComponent(four)

															    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

															    .addComponent(five)

															    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

															    .addComponent(six)))

											    .addGap(18, 18, 18)

											    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

													    .addComponent(times, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

													    .addComponent(divide, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

									    .addGroup(layout.createSequentialGroup()

											    .addComponent(seven)

											    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

											    .addComponent(eight)

											    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

											    .addComponent(nine)

											    .addGap(18, 18, 18)

											    .addComponent(plus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

									    .addGroup(layout.createSequentialGroup()

											    .addComponent(clear)

											    .addGap(51, 51, 51)

											    .addComponent(calc)

											    .addGap(18, 18, 18)

											    .addComponent(minus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

							    .addContainerGap())

	    );

	    layout.setVerticalGroup(

			    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

					    .addGroup(layout.createSequentialGroup()

							    .addContainerGap()

							    .addComponent(out, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

							    .addGap(18, 18, 18)

							    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

									    .addComponent(one)

									    .addComponent(two)

									    .addComponent(three)

									    .addComponent(times))

							    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

							    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

									    .addComponent(four)

									    .addComponent(five)

									    .addComponent(six)

									    .addComponent(divide))

							    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

							    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

									    .addComponent(seven)

									    .addComponent(eight)

									    .addComponent(nine)

									    .addComponent(plus))

							    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

							    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

									    .addComponent(clear)

									    .addComponent(calc)

									    .addComponent(minus))

							    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

	    );

	    pack();

    }

    public static void main(String args[]) {

	    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);

	    }

	    java.awt.EventQueue.invokeLater(new Runnable() {

		    public void run() {

			    new Calculator().setVisible(true);

		    }

	    });

    }

    private javax.swing.JButton calc;

    private javax.swing.JButton clear;

    private javax.swing.JButton divide;

    private javax.swing.JButton eight;

    private javax.swing.JButton five;

    private javax.swing.JButton four;

    private javax.swing.JButton minus;

    private javax.swing.JButton nine;

    private javax.swing.JButton one;

    private javax.swing.JTextField out;

    private javax.swing.JButton plus;

    private javax.swing.JButton seven;

    private javax.swing.JButton six;

    private javax.swing.JButton three;

    private javax.swing.JButton times;

    private javax.swing.JButton two;

    @Override

    public void actionPerformed(ActionEvent e) {

	    Object s = e.getSource();

	    if (s.equals(calc)) {

		    switch(method) {

			    case 't':

				    out.setText(Integer.toString(total * Integer.parseInt(out.getText())));

				    updaterequired = true;

				    break;

			    case 'd':

				    out.setText(Integer.toString(total / Integer.parseInt(out.getText())));

				    updaterequired = true;

				    break;

			    case 'p':

				    out.setText(Integer.toString(total + Integer.parseInt(out.getText())));

				    updaterequired = true;

				    break;

			    case 'm':

				    out.setText(Integer.toString(total - Integer.parseInt(out.getText())));

				    updaterequired = true;

				    break;

			    default:

				    out.setText("Error grabbing method, Please click 'C' and try again!");

				    break;

		    }

	    } else if (s.equals(clear)) {

		    out.setText("");

		    handleUpdate();

		    total = 0;

		    method = 'n';

	    } else if (s.equals(divide)) {

		    handleMethods();

		    method = 'd';

		    updaterequired = true;

	    } else if (s.equals(eight)) {

		    handleUpdate();

		    out.setText(out.getText() + "8");

	    } else if (s.equals(five)) {

		    handleUpdate();

		    out.setText(out.getText() + "5");

	    } else if (s.equals(four)) {

		    handleUpdate();

		    out.setText(out.getText() + "4");

	    } else if (s.equals(minus)) {

		    handleMethods();

		    method = 'm';

		    updaterequired = true;

	    } else if (s.equals(nine)) {

		    handleUpdate();

		    out.setText(out.getText() + "9");

	    } else if (s.equals(one)) {

		    handleUpdate();

		    out.setText(out.getText() + "1");

	    } else if (s.equals(plus)) {

		    handleMethods();

		    method = 'p';

		    updaterequired = true;

	    } else if (s.equals(seven)) {

		    handleUpdate();

		    out.setText(out.getText() + "7");

	    } else if (s.equals(six)) {

		    handleUpdate();

		    out.setText(out.getText() + "6");

	    } else if (s.equals(three)) {

		    handleUpdate();

		    out.setText(out.getText() + "3");

	    } else if (s.equals(times)) {

		    handleMethods();

		    method = 't';

		    updaterequired = true;

	    } else if (s.equals(two)) {

		    handleUpdate();

		    out.setText(out.getText() + "2");

	    }

    }

    public void handleUpdate() {

	    if (updaterequired) {

		    out.setText("");

	    }

    }

    public void handleMethods() {

	    if (total != 0) {

		    if (method == 'd') {

			    total /= Integer.parseInt(out.getText());

			    out.setText(Integer.toString(total));

		    } else if (method == 't') {

			    total *= Integer.parseInt(out.getText());

			    out.setText(Integer.toString(total));

		    } else if (method == 'p') {

			    total += Integer.parseInt(out.getText());

			    out.setText(Integer.toString(total));

		    } else if (method == 'm') {

			    total -= Integer.parseInt(out.getText());

			    out.setText(Integer.toString(total));

		    } else {

			    return;

		    }

	    } else {

		    total = Integer.parseInt(out.getText());

	    }

    }

}

Share this post


Link to post
Share on other sites

You're doing it the hard way.

 

 

 

import javax.swing.JButton;
 

 

 

Instead of having to do 

 

 

 

javax.swing.JButton button = null;
 

 

 

You can do 

 

 

 

JButton button = null;
 

 

Also instead of doing

 

 

 

JButton button1 = new JButton();
button1.setText("1");
 

Also one more thing when your 'tab' spacing.

 

Do instead

 

 

 

JButton button1 = new JButton("1");
 

I could be picky but I can't be bothered :P Nice work though!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...