Need help building a simple calculator
I am trying to get the number keys right now to post there number in the tf text field.
Right now I am getting a few errors and noting happens when you click one the buttons.
here my code
Code :
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BasicGUI extends JFrame implements ActionListener {
private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, plus, equal;
private JTextField tf;
private JPanel display, keypad;
private GridLayout bGrid;
private class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e){
JButton b = (JButton)e.getSource();
String output = "";
if (b == b0){
output += "0";
} else if (b == b1){
output += "1";
} else if (b == b2){
output += "2";
} else if (b == b3){
output += "3";
} else if (b == b4){
output += "4";
}else if (b == b5){
output += "5";
}else if (b == b6){
output += "6";
}else if (b == b7){
output += "7";
}else if (b == b8){
output += "8";
}else if (b == b9){
output += "9";
}
//tarea.setText(out);
tf.setText(output);
}
}
public BasicGUI() {
super("My First GUI Application");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bGrid = new GridLayout(4, 3, 2, 2);
keypad = new JPanel(bGrid);
display = new JPanel();
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
plus = new JButton("+");
equal = new JButton("=");
keypad.add(b0);
keypad.add(b1);
keypad.add(b2);
keypad.add(b3);
keypad.add(b4);
keypad.add(b5);
keypad.add(b6);
keypad.add(b7);
keypad.add(b8);
keypad.add(b9);
keypad.add(plus);
keypad.add(equal);
tf = new JTextField(20);
display.add(tf);
this.add(display, BorderLayout.NORTH);
this.add(keypad, BorderLayout.SOUTH);
this.pack();
}
/**
* @param args
*/
public static void main(String[] args) {
new BasicGUI().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Thank you for any help you can give.
Re: Need help building a simple calculator
What are your errors? Are the compiler errors or runtime errors? Post them here.
Re: Need help building a simple calculator
errors are fixed, but I am having problems register my buttons to post to the tf text field when you click on them.
Re: Need help building a simple calculator
lol, ok I see what i was doing wrong. sometimes a good night rest and a new look can solve many problems.
here is my new working code,
feel free to try it and use it
Code :
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BasicGUI extends JFrame implements ActionListener {
private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, plus, equal;
private JTextField tf;
private JPanel display, keypad;
private GridLayout bGrid;
String strIn = "";
String strOut = "";
String str = "";
int output = 0;
public BasicGUI() {
super("My First GUI Application");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bGrid = new GridLayout(4, 3, 2, 2);
keypad = new JPanel(bGrid);
display = new JPanel();
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
plus = new JButton("+");
equal = new JButton("=");
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 0 button");
strIn = strIn+"0";
tf.setText(strIn);
}});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 1 button");
strIn = strIn+"1";
tf.setText(strIn);
}});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 2 button");
strIn = strIn+"2";
tf.setText(strIn);
}});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 3 button");
strIn = strIn+"3";
tf.setText(strIn);
}});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 4 button");
strIn = strIn+"4";
tf.setText(strIn);
}});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 5 button");
strIn = strIn+"5";
tf.setText(strIn);
}});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 6 button");
strIn = strIn+"6";
tf.setText(strIn);
}});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 7 button");
strIn = strIn+"7";
tf.setText(strIn);
}});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 8 button");
strIn = strIn+"8";
tf.setText(strIn);
}});
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the 9 button");
strIn = strIn+"9";
tf.setText(strIn);
}});
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the plus button");
output = Integer.parseInt( strIn )+output;
str = strIn + "+" + str;
strOut = str;
strIn = "";
tf.setText(strOut);
}});
equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the equal button");
output = Integer.parseInt( strIn )+output;
str = str + strIn;
strOut = str;
strOut = strOut + "=" + Integer.toString(output);
tf.setText(strOut);
strIn = "";
strOut = "";
str = "";
output = 0;
}});
keypad.add(b0);
keypad.add(b1);
keypad.add(b2);
keypad.add(b3);
keypad.add(b4);
keypad.add(b5);
keypad.add(b6);
keypad.add(b7);
keypad.add(b8);
keypad.add(b9);
keypad.add(plus);
keypad.add(equal);
tf = new JTextField(20);
display.add(tf);
this.add(display, BorderLayout.NORTH);
this.add(keypad, BorderLayout.SOUTH);
this.pack();
}
/**
* @param args
*/
public static void main(String[] args) {
new BasicGUI().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Re: Need help building a simple calculator
I just improved my code, new code posted below
Code :
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BasicGUI extends JFrame implements ActionListener {
private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, plus, equal;
private JTextField tf;
private JPanel display, keypad;
private GridLayout bGrid;
String outputTf = "";
int output = 0;
String n = "";
String i = "";
int num = 0;
public void actionPerformed(ActionEvent e) { // event handler
JButton b = (JButton) e.getSource();
if (b == b0) {
i = Integer.toString(0);
n = n + Integer.toString(0);
} else if (b == b1) {
i = Integer.toString(1);
n = n + Integer.toString(1);
} else if (b == b2) {
i = Integer.toString(2);
n = n + Integer.toString(2);
} else if (b == b3) {
i = Integer.toString(3);
n = n + Integer.toString(3);
} else if (b == b4) {
i = Integer.toString(4);
n = n + Integer.toString(4);
} else if (b == b5) {
i = Integer.toString(5);
n = n + Integer.toString(5);
} else if (b == b6) {
i = Integer.toString(6);
n = n + Integer.toString(6);
} else if (b == b7) {
i = Integer.toString(7);
n = n + Integer.toString(7);
} else if (b == b8) {
i = Integer.toString(8);
n = n + Integer.toString(8);
} else if (b == b9) {
i = Integer.toString(9);
n = n + Integer.toString(9);
} else if (b == plus) {
num = num + Integer.parseInt(n);
n="";
i = "+";
} else if (b == equal) {
output = num + Integer.parseInt(n);
i = "="+ output;
}
outputTf = outputTf + i;
tf.setText(outputTf);
}
public BasicGUI() {
super("My First GUI Application");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bGrid = new GridLayout(4, 3, 2, 2);
keypad = new JPanel(bGrid);
display = new JPanel();
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
plus = new JButton("+");
equal = new JButton("=");
keypad.add(b0);
keypad.add(b1);
keypad.add(b2);
keypad.add(b3);
keypad.add(b4);
keypad.add(b5);
keypad.add(b6);
keypad.add(b7);
keypad.add(b8);
keypad.add(b9);
keypad.add(plus);
keypad.add(equal);
tf = new JTextField(20);
display.add(tf);
this.add(display, BorderLayout.NORTH);
this.add(keypad, BorderLayout.SOUTH);
this.pack();
// register the event listener with the buttons
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
plus.addActionListener(this);
equal.addActionListener(this);
}
/**
* @param args
*/
public static void main(String[] args) {
new BasicGUI().setVisible(true);
}
}