Question: Hello Im looking for help with my Java Calactor. I am looking to add Keylistener fuctionality in to it now so if I type 1,2

Hello Im looking for help with my Java Calactor. I am looking to add Keylistener fuctionality in to it now so if I type 1,2 etc it will display on the screen and work that way as well.

I have most of the code done but I am looking for help with KeyListener part.

My code:

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList;

import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;

public class keypad extends JPanel implements ActionListener {

JLabel display; JButton numberbut; JButton clearbut; String displaypanel = ""; String[] numbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" }; ArrayList buttonarray;

// to store the last number on screen before we do add or subtract String lastArgument = "", lastAction = "";

public keypad(Container panel) { panel.setPreferredSize(new Dimension(320, 335)); display = new JLabel(displaypanel); display.setPreferredSize(new Dimension(320, 25)); display.setBorder(BorderFactory.createLoweredBevelBorder()); panel.add(display, BorderLayout.PAGE_START); buttonarray = new ArrayList(12); JPanel numberpanel = new JPanel(); numberpanel.setLayout(new GridLayout(5, 3, 0, 0)); numberpanel.setPreferredSize(new Dimension(320, 260));

for (int i = 0; i < numbers.length; i++) { numberbut = new JButton(numbers[i]); buttonarray.add(numberbut); }

buttonarray.add(new JButton("+")); buttonarray.add(new JButton("=")); buttonarray.add(new JButton("-"));

for (int n = 0; n < buttonarray.size(); n++) { buttonarray.get(n).addActionListener(this); numberpanel.add(buttonarray.get(n)); }

numberpanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.black)); panel.add(numberpanel, BorderLayout.LINE_END); clearbut = new JButton("Clear"); clearbut.setPreferredSize(new Dimension(320, 30)); clearbut.addActionListener(this); panel.add(clearbut, BorderLayout.PAGE_END); }

public void actionPerformed(ActionEvent e) { String textThere = display.getText();

if (e.getSource().equals(buttonarray.get(12))) { // add button lastArgument = textThere; lastAction = "+"; display.setText(""); return; } if (e.getSource().equals(buttonarray.get(14))) { // subtract button lastArgument = textThere; lastAction = "-"; display.setText(""); return; }

if (e.getSource().equals(buttonarray.get(13))) { // Equals button try { String thisArgument = textThere; int i = thisArgument.isEmpty() ? 0 : Integer .parseInt(thisArgument); int j = lastArgument.isEmpty() ? 0 : Integer .parseInt(lastArgument); if (lastAction.equals("+")) { display.setText(String.valueOf(i + j)); } else if (lastAction.equals("-")) { display.setText(String.valueOf(j - i)); } lastAction = ""; } catch (NumberFormatException e1) { }

return; }

String additionalText = "";

for (int a = 0; a < buttonarray.size(); a++) { if (e.getSource().equals(buttonarray.get(a))) { additionalText = buttonarray.get(a).getText(); } }

if (e.getSource().equals(clearbut)) { textThere = ""; }

display.setText(textThere.concat(additionalText)); }

public static void main(String[] args) { JFrame frame = new JFrame("Keypad"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new keypad(frame)); frame.pack(); frame.setVisible(true); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!