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