
Akisha L.
asked 02/09/21Programming GUI
A. Name and Address
Design a GUI program that displays your name and address when a button is clicked. The program’s
window should appear as the sketch on the left side of Figure 17-9 when it runs. When the user clicks the
Show Info button, the program should display your name and address, as shown in the sketch on the right
side of the figure.
Figure 17-9 Name and Address program
B. Miles-per-Gallon Calculator
Design a GUI program that calculates a car’s gas mileage. The program’s window should have text boxes
that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven
on a full tank. When a Calculate MPG button is clicked, the program should display the number of miles
that the car may be driven per gallon of gas. Use the following formula to calculate milesper-gallon:
MPG = Miles/Gallons
C. Design an event handler that will execute when the showNameButton component is clicked. The event
handler should perform the following:
Store your first name in a label component named firstNameLabel.
Store your middle name in a label component named middleNameLabel.
Store your last name in a label component named lastNameLabel.
(Remember, to store a value in a label component, you must store the value in the component’s Text property.)
1 Expert Answer

Patrick B. answered 02/09/21
Math and computer tutor/teacher
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Akisha extends JPanel {
private JButton cmdButShow;
private JButton cmdButCalculateMPG;
private JLabel labelFirstName;
private JLabel labelMiddleName;
private JLabel labelLastName;
private JLabel labelAddress;
private JLabel labelCity;
private JLabel labelState;
private JLabel labelZipCode;
private JLabel labelMiles;
private JTextField txtMiles;
private JLabel labelGallons;
private JTextField txtGallons;
private JLabel labelMPG;
private JLabel labelDivider;
private JButton cmdButHide;
private JButton cmdButClear;
public Akisha() {
//construct components
cmdButShow = new JButton ("DISPLAY");
cmdButCalculateMPG = new JButton ("CALCULATE MPG");
labelFirstName = new JLabel ("First Name");
labelMiddleName = new JLabel ("MiddleName");
labelLastName = new JLabel ("Last Name");
labelAddress = new JLabel ("123 Your Street Name");
labelCity = new JLabel ("Your City Name");
labelState = new JLabel ("Your State Name");
labelZipCode = new JLabel ("Your Zip Code");
labelMiles = new JLabel ("# of Miles");
txtMiles = new JTextField (5);
labelGallons = new JLabel ("# of Gallons");
txtGallons = new JTextField (5);
labelMPG = new JLabel ("MPG: ???????");
labelDivider = new JLabel ("");
cmdButHide = new JButton ("HIDE");
cmdButClear = new JButton ("CLEAR");
//adjust size and set layout
setPreferredSize (new Dimension (567, 393));
setLayout (null);
//add components
add (cmdButShow);
add (cmdButCalculateMPG);
add (labelFirstName);
add (labelMiddleName);
add (labelLastName);
add (labelAddress);
add (labelCity);
add (labelState);
add (labelZipCode);
add (labelMiles);
add (txtMiles);
add (labelGallons);
add (txtGallons);
add (labelMPG);
add (labelDivider);
add (cmdButHide);
add (cmdButClear);
//set component bounds (only needed by Absolute Positioning)
cmdButShow.setBounds (415, 25, 105, 35);
cmdButCalculateMPG.setBounds (325, 245, 140, 20);
labelFirstName.setBounds (20, 25, 100, 25);
labelMiddleName.setBounds (170, 30, 100, 25);
labelLastName.setBounds (300, 30, 100, 25);
labelAddress.setBounds (120, 60, 145, 30);
labelCity.setBounds (60, 90, 100, 25);
labelState.setBounds (235, 95, 100, 25);
labelZipCode.setBounds (260, 130, 100, 25);
labelMiles.setBounds (20, 220, 100, 25);
txtMiles.setBounds (145, 225, 145, 25);
labelGallons.setBounds (25, 270, 100, 25);
txtGallons.setBounds (150, 265, 140, 30);
labelMPG.setBounds (335, 295, 100, 25);
labelDivider.setBounds (0, 170, 565, 15);
cmdButHide.setBounds (415, 65, 105, 35);
cmdButClear.setBounds(325,275,140,20);
labelFirstName.setVisible(false);
labelLastName.setVisible(false);
labelMiddleName.setVisible(false);
labelAddress.setVisible(false);
labelCity.setVisible(false);
labelState.setVisible(false);
labelZipCode.setVisible(false);
labelDivider.setOpaque(true);
labelDivider.setBackground(Color.black);
cmdButHide.setEnabled(false);
cmdButShow.setEnabled(true);
cmdButHide.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
labelFirstName.setVisible(false);
labelLastName.setVisible(false);
labelMiddleName.setVisible(false);
labelAddress.setVisible(false);
labelCity.setVisible(false);
labelState.setVisible(false);
labelZipCode.setVisible(false);
cmdButHide.setEnabled(false);
cmdButShow.setEnabled(true);
} //actionPerformed
} //newActionListener
); //addActionListener
cmdButShow.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
labelFirstName.setVisible(true);
labelLastName.setVisible(true);
labelMiddleName.setVisible(true);
labelAddress.setVisible(true);
labelCity.setVisible(true);
labelState.setVisible(true);
labelZipCode.setVisible(true);
cmdButHide.setEnabled(true);
cmdButShow.setEnabled(false);
} //actionPerformed
} //newActionListener
); //addActionListener
cmdButCalculateMPG.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
String inbuff = txtMiles.getText();
double miles = Double.parseDouble(inbuff.trim());
inbuff = txtGallons.getText();
double gallons = Double.parseDouble(inbuff.trim());
double mpg = 0;
if ((gallons>0) && (miles>0))
{
mpg= miles/gallons;
labelMPG.setText("MPG: " + mpg);
}
} //actionPerformed
} //newActionListener
); //addActionListener
cmdButClear.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
txtMiles.setText("");
txtGallons.setText("");
labelMPG.setText("MPG:???");
txtMiles.requestFocus();
} //actionPerformed
} //newActionListener
); //addActionListener
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Akisha");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new Akisha());
frame.pack();
frame.setVisible (true);
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESROUCES section under this link... No diagram given... therefore, problems A and C are identical02/09/21