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

Files Telephone.java and TelephonePanel.java contain the skeleton for a program

ID: 3677717 • Letter: F

Question

Files Telephone.java and TelephonePanel.java contain the skeleton for a program to lay out a GUI that looks like telephone

keypad with a title that says “Your Telephone!!”. Save these files to your directory. Telephone.java is complete, but

TelephonePanel.java is not.

1. Using the comments as a guide, add code to TelephonePanel.java to create the GUI. Some things to consider:

a. TelephonePanel (the current object, which is a JPanel) should get a BorderLayout to make it easy to

separate the title from the keypad. The title will go in the north area and the keypad will go in the center

area. The other areas will be unused.

b. You can create a JLabel containing the title and add it directly to the north section of the TelephonePanel.

However, to put the keypad in the center you will first need to create a new JPanel and add the keys (each a

button) to it, then add it to the center of the TelephonePanel. This new panel should have a 4×3 GridLayout.

c. Your keypad should hold buttons containing 1 2 3, 4 5 6, 7 8 9, * 0 # in the four rows respectively. So

you’ll create a total of 12 buttons.

2. Compile and run Telephone.java. You should get a small keypad and title. Grow the window (just drag the corner) and

see how the GUI changes - everything grows proportionately.

3. Note that the title is not centered, but it would look nicer if it were. One way to do this is to create a new JPanel, add the

title label to it, then add the new JPanel to the north area of the TelephonePanel (instead of adding the label directly).

This works because the default layout for a JPanel is a centered FlowLayout, and the JPanel itself will expand to fill the

whole north area. Modify your program in this way so that the title is centered.

//************************************************************

// Telephone.java

//

// Uses the TelephonePanel class to create a (functionless) GUI

// like a telephone keypad with a title.

// Illustrates use of BorderLayout and GridLayout.

//************************************************************

import javax.swing.*;

public class Telephone

{

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);

}

}

//************************************************************

// 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

//create a JLabel with "Your Telephone" title

//add title label to north of this panel

//create panel to hold keypad and give it a 4x3 GridLayout

//add buttons representing keys to key panel

//add key panel to center of this panel

}

}

please i need help with an answer. thanks

Explanation / Answer

import java.awt.*;
import javax.swing.*;
public class TelephonePanel extends JPanel
{
private static final long serialVersionUID = 1L;
public TelephonePanel()
{
setLayout(new BorderLayout());
JPanel titlePan = new JPanel();
JLabel title = new JLabel("Your Telephone",JLabel.CENTER);
titlePan.add(title);
add(titlePan, BorderLayout.NORTH);
JPanel numPanel = new JPanel();
add(numPanel,BorderLayout.CENTER);
JButton[] num = new JButton[12];
for (int i = 0;i <= 9;i++)
{
num[i] = new JButton("" + i);
}
num[10] = new JButton("*");
num[11] = new JButton("#");
numPanel.setLayout(new GridLayout(4, 3));
for (int i = 0; i < 12; i++)
{
numPanel.add(num[i]);
}
}
}

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