Exercise 8: 8. Write an application that models a telephone keypad. Use a JPanel
ID: 3689076 • Letter: E
Question
Exercise 8:
8. Write an application that models a telephone keypad. Use a JPanel panel to hold twelve buttons—1, 2, 3, 4, 5, 6, 7, 8, 9, *, 0, # —in the center of a grid layout. Place a blank label in the south location. As each number is pressed, append that digit to the text of the label.
The first number in the number cannot be 0. If the user types 0 as the first number, do nothing with it.
Format the number using dashes, as follows:
1-000-000-0000 if the first digit entered is a 1.
(000) 000-0000 if ten digits are entered.
000-0000 if seven digits are entered.
Do not accept extra digits.
Instructions 1. Name your java program “TelephoneKeypad.java”.
Explanation / Answer
import java.awt.*; import javax.swing.*; public class Telephone { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(250, 200)); frame.setTitle("Telephone"); frame.setLayout(new BorderLayout()); // the main phone buttons JPanel centerPanel = new JPanel(new GridLayout(4, 3)); for (int i = 1; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.