0

I have this simple calculator I just started on, and I'm having just a few problems. First of all, I don't know how to append text to a textfield. Maybe I would have to create a string buffere or something. Then, when the user clicks one of the buttons, the compiler goes crazy, and nothing is displayed.

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

public class Calculator extends JFrame implements ActionListener
{
	JButton[] btnNums;
	JButton btnBack;
	JButton btnClear;
	JButton btnCalculate;
	String[] strNames;
	
	JTextField txtDisplay;
	
	public Calculator()
	{
		Container content = getContentPane();
		setSize(210,250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		/*  construct a JPanel and the textfield that 
		 *  will show the inputed values and outputed 
		 *  results
		 */
		JPanel jpDisplay = new JPanel();
		jpDisplay.setLayout(new BorderLayout());
		txtDisplay = new JTextField(15);
		txtDisplay.setHorizontalAlignment(JTextField.RIGHT);
		jpDisplay.add(txtDisplay);
		
		/*  contstruct a JPanel that will contain a back
		 *  button and a clear button.
		 */
		JPanel jpRow2 = new JPanel();
		btnBack = new JButton("Backspace");
		btnClear = new JButton("Clear");
		btnClear.addActionListener(this);
		jpRow2.add(btnBack);
		jpRow2.add(btnClear);
		
		/*  construct a string array with all the names of the
		 *  buttons, then in a for loop create the new buttons
		 *  and add their name and an actionListener to them.
		 */
		String[] strNames = {"7","8", "9","/", "4", "5", "6","*", "1", "2", "3","+", "0", "+/-", ".", "-"};
		btnNums = new JButton[16];
		JPanel jpButtons = new JPanel();
		jpButtons.setLayout(new GridLayout(4,4));
		
		  for (int i = 0; i < 16; i++)
		  {
			  btnNums[i] = new JButton(strNames[i]);
			  btnNums[i].addActionListener(this);
			  jpButtons.add(btnNums[i]);
		  }
		  
		  /*  construct the final JPanel and add a 
		   *  calculate button to it.
		   */
		  JPanel jpLastRow = new JPanel();
		  btnCalculate = new JButton("Calculate");
		  btnCalculate.addActionListener(this);
		  jpLastRow.add(btnCalculate);
		  
		  /*  set the contentPane and create the layout
		   *  add the panels to the container
		   */
		  setContentPane(content);
		  setLayout(new FlowLayout());
		  setResizable(false);
		  content.add(jpDisplay, BorderLayout.NORTH);
		  content.add(jpRow2);
		  content.add(jpButtons);
		  content.add(jpLastRow);
		  setTitle("Mini Calculator");
		  setVisible(true); 
		  
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		for (int i =0; i < 16; i++)
		{
			if (ae.getSource() == btnNums[i])
			{
				txtDisplay.setText(strNames[i]);
			}
		}
		
	}
	
	public static void main(String[] args)
	{
		Calculator calc = new Calculator();
	}
}
1
Contributor
1
Reply
2
Views
12 Years
Discussion Span
Last Post by server_crash
0

never mind, I got it.

public void actionPerformed(ActionEvent ae)
	{
		for (int i =0; i < 16; i++)
		{
			if (ae.getSource() == btnNums[i])
			{
				txtDisplay.setText(txtDisplay.getText() + btnNums[i].getLabel());
			}
		}
		
	}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.