8. Write an application that models a telephone keypad. Use a JPanel panel to ho
ID: 3690525 • Letter: 8
Question
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
// ********************************************************************
// TelephonePanel.java
//
// Lays out a (functionless) GUI like a telephone keypad with a title
// Illustrates use of BorderLayout and GridLayout.
// ********************************************************************
import java.awt.*;
import javax.swing.*;
public class TelephonePanel extends JPanel {
public TelephonePanel() {
// set BorderLayout for this panel
setLayout(new BorderLayout());
// create Jlabel with "Your Telephone" title
JLabel title = new JLabel("Your Telephone");
// add title to north of this panel
add(title, BorderLayout.NORTH);
// create panel to hold keypad and give it a 4x3 GridLayout
GridLayout layout = new GridLayout(4, 3);
// add buttons representing keypanel
JPanel keypanel = new JPanel(layout);
// create the buttons
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttona = new JButton("*");
JButton button0 = new JButton("0");
JButton buttonp = new JButton("#");
// add key panel to center of this panel
keypanel.add(button1);
keypanel.add(button2);
keypanel.add(button3);
keypanel.add(button4);
keypanel.add(button5);
keypanel.add(button6);
keypanel.add(button7);
keypanel.add(button8);
keypanel.add(button9);
keypanel.add(buttona);
keypanel.add(button0);
keypanel.add(buttonp);
// add keypanel to center of the panel
add(keypanel, BorderLayout.CENTER);
// create a new panel for the title "Your Telephone"
JPanel titlePanel = new JPanel();
// add "Your Telephone" to title panel
titlePanel.add(title);
// add the titlePanel to the north of the panel
add(titlePanel, BorderLayout.NORTH);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Telephone");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TelephonePanel());
frame.pack();
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.