Question

GUI Programming Run and review kiloconverter.java. Then answer the following questions: . What is the window...

GUI Programming

Run and review kiloconverter.java. Then answer the following questions:

. What is the window title?

. What type of object is calcButton?

. What is the name of the JPanel variable in buildPanel?

. What three components get added to the panel?

. How does the program show its results: a message dialog, or on the console?

. What variable is kiloTextField.getText() assigned to?

. What method is used to convert the input string to a Double?

. What method gets called to display the window? (What method makes the window visible?)

. What class does KiloConverter extend?

. What interface does calcButtonListener implement?

// Code courtesy of Gaddis, Getting Started with Java, 6th Edition
import javax.swing.*; // Needed for Swing classes
import java.awt.event.*; // Needed for ActionListener Interface

/**
The KiloConverter class displays a JFrame that
lets the user enter a distance in kilometers. When
the Calculate button is clicked, a dialog box is
displayed with the distance converted to miles.
*/

public class KiloConverter extends JFrame
{
private JPanel panel; // To reference a panel
private JLabel messageLabel; // To reference a label
private JTextField kiloTextField; // To reference a text field
private JButton calcButton; // To reference a button
private final int WINDOW_WIDTH = 310; // Window width
private final int WINDOW_HEIGHT = 100; // Window height

/**
Constructor
*/

public KiloConverter()
{
// Set the window title.
setTitle("Kilometer Converter");

// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

// Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Build the panel and add it to the frame.
buildPanel();

// Add the panel to the frame's content pane.
add(panel);

// Display the window.
setVisible(true);
}

/**
The buildPanel method adds a label, text field, and
and a button to a panel.
*/

private void buildPanel()
{
// Create a label to display instructions.
messageLabel = new JLabel("Enter a distance " +
"in kilometers");

// Create a text field 10 characters wide.
kiloTextField = new JTextField(10);

// Create a button with the caption "Calculate".
calcButton = new JButton("Calculate");

// Add an action listener to the button.
calcButton.addActionListener(new CalcButtonListener());

// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel();

// Add the label, text field, and button
// components to the panel.
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(calcButton);
}

/**
CalcButtonListener is an action listener class for
the Calculate button.
*/

private class CalcButtonListener implements ActionListener
{
/**
The actionPerformed method executes when the user
clicks on the Calculate button.
@param e The event object.
*/

public void actionPerformed(ActionEvent e)
{
final double CONVERSION = 0.6214;
String input; // To hold the user's input
double miles; // The number of miles

// Get the text entered by the user into the
// text field.
input = kiloTextField.getText();

// For debugging, display the text entered, and
// its value converted to a double.
System.out.println("Reading " + input +
" from the text field.");
System.out.println("Converted value: " +
Double.parseDouble(input));

// Convert the input to miles.
miles = Double.parseDouble(input) * CONVERSION;

// Display the result.
JOptionPane.showMessageDialog(null, input +
" kilometers is " + miles + " miles.");

// For debugging, display a message indicating
// the application is ready for more input.
System.out.println("Ready for the next input.");
}
} // End of CalcButtonListener class

/**
The main method creates an instance of the
KiloConverter class, which displays
its window on the screen.
*/

public static void main(String[] args)
{
new KiloConverter();
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

KiloConverter.java:

// Code courtesy of Gaddis, Getting Started with Java, 6th Edition
import javax.swing.*; // Needed for Swing classes
import java.awt.event.*; // Needed for ActionListener Interface

/**
The KiloConverter class displays a JFrame that
lets the user enter a distance in kilometers. When
the Calculate button is clicked, a dialog box is
displayed with the distance converted to miles.
*/

public class KiloConverter extends JFrame
{
private JPanel panel; // To reference a panel
private JLabel messageLabel; // To reference a label
private JTextField kiloTextField; // To reference a text field
private JButton calcButton; // To reference a button
private final int WINDOW_WIDTH = 310; // Window width
private final int WINDOW_HEIGHT = 100; // Window height

/**
Constructor
*/

public KiloConverter()
{
// Set the window title.
setTitle("Kilometer Converter");

// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

// Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Build the panel and add it to the frame.
buildPanel();

// Add the panel to the frame's content pane.
add(panel);

// Display the window.
setVisible(true);
}

/**
The buildPanel method adds a label, text field, and
and a button to a panel.
*/

private void buildPanel()
{
// Create a label to display instructions.
messageLabel = new JLabel("Enter a distance " +
"in kilometers");

// Create a text field 10 characters wide.
kiloTextField = new JTextField(10);

// Create a button with the caption "Calculate".
calcButton = new JButton("Calculate");

// Add an action listener to the button.
calcButton.addActionListener(new CalcButtonListener());

// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel();

// Add the label, text field, and button
// components to the panel.
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(calcButton);
}

/**
CalcButtonListener is an action listener class for
the Calculate button.
*/

private class CalcButtonListener implements ActionListener
{
/**
The actionPerformed method executes when the user
clicks on the Calculate button.
@param e The event object.
*/

public void actionPerformed(ActionEvent e)
{
final double CONVERSION = 0.6214;
String input; // To hold the user's input
double miles; // The number of miles

// Get the text entered by the user into the
// text field.
input = kiloTextField.getText();

// For debugging, display the text entered, and
// its value converted to a double.
System.out.println("Reading " + input +
" from the text field.");
System.out.println("Converted value: " +
Double.parseDouble(input));

// Convert the input to miles.
miles = Double.parseDouble(input) * CONVERSION;

// Display the result.
JOptionPane.showMessageDialog(null, input +
" kilometers is " + miles + " miles.");

// For debugging, display a message indicating
// the application is ready for more input.
System.out.println("Ready for the next input.");
}
} // End of CalcButtonListener class

/**
The main method creates an instance of the
KiloConverter class, which displays
its window on the screen.
*/

public static void main(String[] args)
{
new KiloConverter();
}
}

Output:

1). What is the window title?

Answer:

The title of the window in the above program is Kilometer Converter. setTitle("Kilometer Converter");

2). What type of object is calcButton?

Answer   calcButton is a JButton object.

3). What is the name of the JPanel variable in buildPanel?

Answer:The name of the JPanel variable in buildPanel is panel

4). What three components get added to the panel?

Answer:

The three components that get added to the panel are: JButton, JTextField, and JLabel

5). How does the program show its results: a message dialog, or on the console?

Answer:

The program shows it results in a Message dialog box

6). What variable is kiloTextField.getText() assigned to?

Answer:

The variable that is assigned to kiloTextField.getText() is "input"

7). What method is used to convert the input string to a Double?

Answer:

The method that is used to convert the input string to a Double is Double.parseDouble()

8). What method gets called to display the window? (What method makes the window visible?)

Answer:

The method that gets called to display the window is buildPanel();

9). What class does KiloConverter extend?

Answer:

The KiloConverter class extends JFrame Class.

10). What interface does calcButtonListener implement?

Answer:

The interface that calcButtonListener implements is: ActionListener

Add a comment
Know the answer?
Add Answer to:
GUI Programming Run and review kiloconverter.java. Then answer the following questions: . What is the window...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT