Java based Calculator problem

Joined
Sep 19, 2010
Messages
5
Reaction score
0
Hello, Im new on this site... Im just a college student and I'm in need of the logical formulas for the ActionListener for this calculator thing.. Could someone please show and explain to me how?

As you can see the action listner is still blank.. I just want to know how can I make use of the declared variables to that listener

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame
{
JTextField screen = new JTextField(13);
FlowLayout layout = new FlowLayout();
JButton buttonOne = new JButton(" 1 ");
JButton buttonTwo = new JButton(" 2 ");
JButton buttonThree = new JButton(" 3 ");
JButton buttonFour = new JButton(" 4 ");
JButton buttonFive = new JButton(" 5 ");
JButton buttonSix = new JButton(" 6 ");
JButton buttonSeven = new JButton(" 7 ");
JButton buttonEight = new JButton(" 8 ");
JButton buttonNine = new JButton(" 9 ");
JButton buttonZero = new JButton(" 0 ");
JButton buttonPlus = new JButton(" + ");
JButton buttonMinus = new JButton(" - ");
JButton buttonMultiply = new JButton(" * ");
JButton buttonDivide = new JButton(" / ");
JButton buttonEquals = new JButton(" = ");
JButton buttonPoint = new JButton(" . ");
JButton buttonSign = new JButton(" +/- ");
JButton buttonClear = new JButton(" c ");
JButton buttonOff = new JButton(" off ");

String container = "";
String containerTwo = "";

public Calculator()
{
setTitle("Calculator");
setSize(360,380);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(layout);

layout.setAlignment(FlowLayout.CENTER);

add(screen);
add(buttonSeven);
add(buttonEight);
add(buttonNine);
add(buttonPlus);
add(buttonFour);
add(buttonFive);
add(buttonSix);
add(buttonMinus);
add(buttonOne);
add(buttonTwo);
add(buttonThree);
add(buttonMultiply);
add(buttonPoint);
add(buttonZero);
add(buttonSign);
add(buttonDivide);
add(buttonEquals);
add(buttonClear);
add(buttonOff);

screen.setFont(new Font("Arial", Font.BOLD, 30));
screen.setHorizontalAlignment(JTextField.RIGHT);
screen.setPreferredSize(new Dimension(70,40));

buttonZero.setFont(new Font("Arial", Font.BOLD, 18));
buttonZero.setPreferredSize(new Dimension(70,40));
buttonZero.addActionListener(new Listener());

buttonOne.setFont(new Font("Arial", Font.BOLD, 18));
buttonOne.setPreferredSize(new Dimension(70,40));
buttonOne.addActionListener(new Listener());

buttonTwo.setFont(new Font("Arial", Font.BOLD, 18));
buttonTwo.setPreferredSize(new Dimension(70,40));
buttonTwo.addActionListener(new Listener());

buttonThree.setFont(new Font("Arial", Font.BOLD, 18));
buttonThree.setPreferredSize(new Dimension(70,40));
buttonThree.addActionListener(new Listener());

buttonFour.setFont(new Font("Arial", Font.BOLD, 18));
buttonFour.setPreferredSize(new Dimension(70,40));
buttonFour.addActionListener(new Listener());

buttonFive.setFont(new Font("Arial", Font.BOLD, 18));
buttonFive.setPreferredSize(new Dimension(70,40));
buttonFive.addActionListener(new Listener());

buttonSix.setFont(new Font("Arial", Font.BOLD, 18));
buttonSix.setPreferredSize(new Dimension(70,40));
buttonSix.addActionListener(new Listener());

buttonSeven.setFont(new Font("Arial", Font.BOLD, 18));
buttonSeven.setPreferredSize(new Dimension(70,40));
buttonSeven.addActionListener(new Listener());

buttonEight.setFont(new Font("Arial", Font.BOLD, 18));
buttonEight.setPreferredSize(new Dimension(70,40));
buttonEight.addActionListener(new Listener());

buttonNine.setFont(new Font("Arial", Font.BOLD, 18));
buttonNine.setPreferredSize(new Dimension(70,40));
buttonNine.addActionListener(new Listener());

buttonPoint.setFont(new Font("Arial", Font.BOLD, 18));
buttonPoint.setPreferredSize(new Dimension(70,40));
buttonPoint.addActionListener(new Listener());

buttonSign.setFont(new Font("Arial", Font.BOLD, 18));
buttonSign.setPreferredSize(new Dimension(70,40));
buttonSign.addActionListener(new Listener());

buttonPlus.setFont(new Font("Arial", Font.BOLD, 20));
buttonPlus.setPreferredSize(new Dimension(70,40));
buttonPlus.addActionListener(new Listener());

buttonMinus.setFont(new Font("Arial", Font.BOLD, 20));
buttonMinus.setPreferredSize(new Dimension(70,40));
buttonMinus.addActionListener(new Listener());

buttonMultiply.setFont(new Font("Arial", Font.BOLD, 20));
buttonMultiply.setPreferredSize(new Dimension(70,40));
buttonMultiply.addActionListener(new Listener());

buttonDivide.setFont(new Font("Arial", Font.BOLD, 20));
buttonDivide.setPreferredSize(new Dimension(70,40));
buttonDivide.addActionListener(new Listener());

buttonEquals.setFont(new Font("Arial", Font.BOLD, 20));
buttonEquals.setPreferredSize(new Dimension(145,40));
buttonEquals.addActionListener(new Listener());

buttonClear.setFont(new Font("Arial", Font.BOLD, 20));
buttonClear.setPreferredSize(new Dimension(70,40));
buttonClear.addActionListener(new Listener());

buttonOff.setFont(new Font("Arial", Font.BOLD, 18));
buttonOff.setPreferredSize(new Dimension(70,40));
buttonOff.addActionListener(new Listener());
}

private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

}
}

public static void main(String[] args)
{
Calculator calcu = new Calculator();
}
}
 
Joined
Sep 19, 2010
Messages
5
Reaction score
0
The following are 2 programs given to us by our instructor to help us build that calculator program.. She said that its related to it and more likely just combine these 2 and edit some parts and the java calculator will be working

1.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Compute extends JFrame
{
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();

FlowLayout layout1 = new FlowLayout();

JTextField field1 = new JTextField("0",10);
JTextField field2 = new JTextField("0",10);

JButton button1 = new JButton("+");
JButton button2 = new JButton("-");
JButton button3 = new JButton("*");
JButton button4 = new JButton("/");
JButton button5 = new JButton("c");

public Compute()
{
setTitle("Calculator");
setSize(200,300);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(layout1);

layout1.setAlignment(FlowLayout.CENTER);

label1.setText("First Number:");
label1.setFont(new Font("Tahoma", Font.BOLD + Font.PLAIN, 15));

label2.setText("Second Number:");
label2.setFont(new Font("Tahoma", Font.BOLD + Font.PLAIN, 15));

label3.setForeground(Color.RED);
label3.setFont(new Font("Comic Sans MS", Font.BOLD + Font.ITALIC, 15));

field1.setForeground(Color.BLUE);
field1.setBackground(Color.WHITE);
field1.setFont(new Font("Arial", Font.BOLD, 20));

field2.setForeground(Color.BLUE);
field2.setBackground(Color.WHITE);
field2.setFont(new Font("Arial", Font.BOLD, 20));

button1.setForeground(Color.WHITE);
button1.setBackground(Color.BLACK);
button1.setFont(new Font("Lucida Console", Font.BOLD, 15));
button1.addActionListener(new Listener1());

button2.setForeground(Color.WHITE);
button2.setBackground(Color.BLACK);
button2.setFont(new Font("Lucida Console", Font.BOLD, 15));
button2.addActionListener(new Listener2());

button3.setForeground(Color.WHITE);
button3.setBackground(Color.BLACK);
button3.setFont(new Font("Lucida Console", Font.BOLD, 15));
button3.addActionListener(new Listener3());

button4.setForeground(Color.WHITE);
button4.setBackground(Color.BLACK);
button4.setFont(new Font("Lucida Console", Font.BOLD, 15));
button4.addActionListener(new Listener4());

button5.setForeground(Color.WHITE);
button5.setBackground(Color.BLACK);
button5.setFont(new Font("Lucida Console", Font.BOLD, 15));
button5.addActionListener(new Listener5());
add(label1);
add(field1);
add(label2);
add(field2);
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(label3);
}

private class Listener1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String num1 = field1.getText();
String num2 = field2.getText();
double one,two;
one = Double.parseDouble(num1);
two = Double.parseDouble(num2);
label3.setText("The sum is " + (one + two));
}
}

private class Listener2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String num1 = field1.getText();
String num2 = field2.getText();
double one,two;
one = Double.parseDouble(num1);
two = Double.parseDouble(num2);
label3.setText("The difference is " + (one - two));

}
}

private class Listener3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String num1 = field1.getText();
String num2 = field2.getText();
double one,two;
one = Double.parseDouble(num1);
two = Double.parseDouble(num2);
label3.setText("The product is " + (one * two));

}
}

private class Listener4 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String num1 = field1.getText();
String num2 = field2.getText();
double one,two;
one = Double.parseDouble(num1);
two = Double.parseDouble(num2);
label3.setText("The quotient is " + (one / two));

}
}

private class Listener5 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
field1.setText("0");
field2.setText("0");
label3.setText("");
}
}

public static void main(String[] args)
{

Compute calculator = new Compute();
}



}

@@@@@@@@@@@@@@@@@

2.

@@@@@@@@@@@@@@@@@

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Hello extends JFrame
{
JTextField screen = new JTextField(20);
JButton buttonHello = new JButton("Hello");
JButton buttonHi = new JButton("Hi");
JButton buttonClear = new JButton("Clear");
FlowLayout layout = new FlowLayout();
String greetings = "";

public Hello()
{
setTitle("Sample");
setSize(550,130);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(layout);

add(screen);
add(buttonHello);
add(buttonHi);
add(buttonClear);

screen.setFont(new Font("Arial", Font.BOLD, 30));
screen.setHorizontalAlignment(JTextField.RIGHT);

buttonHello.setPreferredSize(new Dimension(80,30));
buttonHello.setFont(new Font("Arial", Font.ITALIC + Font.BOLD, 15));
buttonHello.addActionListener(new Listener());

buttonHi.setPreferredSize(new Dimension(80,30));
buttonHi.setFont(new Font("Arial", Font.ITALIC + Font.BOLD, 15));
buttonHi.addActionListener(new Listener());

buttonClear.setPreferredSize(new Dimension(80,30));
buttonClear.setFont(new Font("Arial", Font.ITALIC + Font.BOLD, 15));
buttonClear.addActionListener(new Listener());
}

private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == buttonHello)
{
greetings = greetings + "Hello ";
screen.setText(greetings);
}

if (source == buttonHi)
{
greetings = greetings + "Hi ";
screen.setText(greetings);
}

if (source == buttonClear)
{
greetings = "";
screen.setText(greetings);
}
}
}
public static void main(String[] args)
{
Hello helloSample = new Hello();
}

}
 
Joined
Sep 19, 2010
Messages
5
Reaction score
0
Hey.. I had improvement.. This is the calclulator java thing ived made some parts of the action listner that works ived been brainstorming it while facebooking.. :p
I need the logical way of doing the mathematical processes like "+,-,*,/" and the "-/+" where if the ones displayed is negative itll be positive.. its like changing the signs..

Ide like to happen is when I click the plus,minus or any operation button itll display the current value on screen then when you press another number the screen will display that number/numbers you have pressed then when you press the "=" (equal sign) it will display the sum of the 2 containers...

I need the codes of the operation signs.. Someone please help??

private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

Object source = e.getSource();
if (source == buttonOne)
{
container = container + "1";
screen.setText(container);
}

if (source == buttonTwo)
{
container = container + "2";
screen.setText(container);
}

if (source == buttonThree)
{
container = container + "3";
screen.setText(container);
}

if (source == buttonFour)
{
container = container + "4";
screen.setText(container);
}

if (source == buttonFive)
{
container = container + "5";
screen.setText(container);
}

if (source == buttonSix)
{
container = container + "6";
screen.setText(container);
}

if (source == buttonSeven)
{
container = container + "7";
screen.setText(container);
}

if (source == buttonEight)
{
container = container + "8";
screen.setText(container);
}

if (source == buttonNine)
{
container = container + "9";
screen.setText(container);
}

if (source == buttonZero)
{
container = container + "0";
screen.setText(container);
}

if (source == buttonPoint)
{
container = container + ".";
screen.setText(container);
}

if (source == buttonPlus)
{
screen.setText(container);
container = container + containerTwo;
}


}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top