Question
I need to write a program that creates a numeric keypad like on a phone and displays the numbers picked on the screen, also with
I need to write a program that creates a numeric keypad like on a phone and displays the numbers picked on the screen, also with a functioning clear button. Here is my code. I have all the elements I just don't know how to make the buttons work and display numbers.
import java.awt.*; import javax.swing.*; import javax.swing.border.*;
/*public class Keypad extends JPanel{ private JPanel keypadButton, numberButton, clearButton;*/ //public class Keypad extends JPanel { public class Keypad {
public static void main (String[] args){ JFrame frame= new JFrame ("Phone Dial"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel= new JPanel (); panel.setLayout(new BorderLayout(100,20)); panel.setBackground(Color.white); panel.setBorder(BorderFactory.createEmptyBorder(8,8,8,8)); JPanel screen= new JPanel(); screen.setLayout(new BorderLayout(100,50)); screen.setBackground(Color.green); screen.setBorder(BorderFactory.createEmptyBorder(80,80,20,20)); panel.add(screen,BorderLayout.NORTH); JPanel keypad= new JPanel(); keypad.setLayout(new GridLayout(5,15)); keypad.setBorder(BorderFactory.createLineBorder(Color.blue)); JButton n1= new JButton ("1"); JButton n2= new JButton ("2"); JButton n3= new JButton ("3"); JButton n4= new JButton ("4"); JButton n5= new JButton ("5"); JButton n6= new JButton ("6"); JButton n7= new JButton ("7"); JButton n8= new JButton ("8"); JButton n9= new JButton ("9"); JButton n10= new JButton ("="); JButton n11= new JButton ("0"); JButton n12= new JButton ("#"); keypad.add(n1); keypad.add(n2); keypad.add(n3); keypad.add(n4); keypad.add(n5); keypad.add(n6); keypad.add(n7); keypad.add(n8); keypad.add(n9); keypad.add(n10); keypad.add(n11); keypad.add(n12); panel.add(keypad); JPanel clearB=new JPanel(); clearB.setLayout(new GridLayout(5,15)); clearB.setPreferredSize(new Dimension (320,30)); clearB.setBorder(BorderFactory.createLineBorder(Color.red)); JButton clear= new JButton ("Clear"); panel.add(clear,BorderLayout.EAST);
frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }
/*private class KeypadListener implements ActionListener { public void actionPerformed (ActionEvent ae) { String str= ae.getActionCommand(); if (str.equals("Clear")) numbers.setText ("NUmber: "); else numbers.setText(numbers.getText()+str); } }*/ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started