Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In JAVA (not Javascript): Write a program that displays a numeric keypad that mi

ID: 3665983 • Letter: I

Question

In JAVA (not Javascript):

Write a program that displays a numeric keypad that might appear on a phone. Above the keypad buttons, show a label that displays the numbers once they are picked.

To the right of the keypad buttons, include another button to clear the display. Use a border layout to manage the keypad buttons. Put a border around the keypad buttons

to group them visually, and a border around the display too.

Here is the code to get you started, and a picture of what the code should roughly display once you run it.

NumericKeypad.java Author: Lewis/Loftus Numeric Keypad // Solution to Programming Project 7.13 import Javax. 3wing.uErame public class NumericKeypad // Creates and presents the program frame. public static void main (String [] args) JFrame quoteFrame new JFrame ("Numeric Keypad"); quoteFrame.setDefaultcloseoperation (JErame .EXIT_ON_CLOSE) quoteFrame.getContentPane () .add (new NumericKeypadPanel )) quoteFrame.pack ); quoteFrame.setVisible (true): Clear

Explanation / Answer

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;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Keypad extends JPanel implements ActionListener
{
JLabel display;
JButton numButton;
JButton clearButton;
String displayContent = "";
String[] numPadContent = {"1","2","3","4","5","6","7","8","9","*","0","#"};
ArrayList<JButton> buttonList;
public Keypad(Container pane) {
pane.setPreferredSize(new Dimension(320, 335));
display = new JLabel(displayContent);
display.setPreferredSize(new Dimension(320, 25));
display.setBorder(BorderFactory.createLoweredBevelBorder());
pane.add(display, BorderLayout.PAGE_START);
buttonList = new ArrayList<JButton>(12);
JPanel numberPanel = new JPanel();
numberPanel.setLayout(new GridLayout(4,3,0,0));
numberPanel.setPreferredSize(new Dimension(320,260));
for (int i = 0; i < numPadContent.length; i++)
{
numButton = new JButton(numPadContent[i]);
buttonList.add(numButton);
}
for (int n = 0; n < buttonList.size(); n++)
{
buttonList.get(n).addActionListener(this);
numberPanel.add(buttonList.get(n));
}
numberPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.black));
pane.add(numberPanel, BorderLayout.LINE_END);
clearButton = new JButton("Clear");
clearButton.setPreferredSize(new Dimension(320, 30));
clearButton.addActionListener(this);
pane.add(clearButton, BorderLayout.PAGE_END);
}
public void actionPerformed(ActionEvent e)
{
String textThere = display.getText();
String additionalText = "";
for (int a = 0; a < buttonList.size(); a++)
{
if (e.getSource().equals(buttonList.get(a)))
{
additionalText = buttonList.get(a).getText();
}
}
if (e.getSource().equals(clearButton))
{
textThere = "";
}
display.setText(textThere.concat(additionalText));
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Numeric Keyboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Keypad(frame));
frame.pack();
frame.setVisible(true);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote