Question
Write a Java program to create a GUI using JFrame, JTextField, JLabel and JButton objects. This is continuation of Project 7 which used the text-based
Write a Java program to create a GUI using JFrame, JTextField, JLabel and JButton objects. This is continuation of Project 7 which used the text-based menu driven model.
Write two value-returning methods called farToCel() and celToFar(). These two methods will convert temperatures from Fahrenheit to Celsius and Celsius to Fahrenheit respectively. They will each take a single int parameter and return the converted value as an int. (These should already be done and can be taken from Project 7. No other code from Project 7 will be of any consequence.)
Here is a picture of what you will create:
In your window constructor, do the following:
Create two labels Temperature: and Results: (text centered).
Create two text fields:
The temperature text field is 10 columns wide and should be initially set to zero.
The results text field is 40 columns wide, has no initial value and should not be an editable field.
Create two buttons Celsius to Fahrenhiet and Fahrenheit to Celsius.
Set the window title to Celsius and Fahrenheit Conversion
Create the Container for the content pane:
Set the layout to the Grid layout of 3 rows by 2 columns.
Add the temperature label and text field.
Add the buttons.
Add the result label and text field.
Assign the button handlers (discussed below).
Set the window size to 375 pixels wide by 120 pixels high.
Set the default close operation to EXIT_ON_CLOSE and set the window visible.
For the button handlers, recall that you will need to create additional classes that implement the ActionListener interface. For each actionPerformed() method, you should:
Read the temperature text field value.
Convert the String temperature to an integer.
Convert the temperature from one scale to the other.
Display the relative information in the results text field.
The main() method should have only one line of code:
p8 window = new p8(); |
Which creates a new p8 object and assigns it to the reference variable window.
For 5 points of extra credit, read the try/catch section of Chapter 12 and do not allow non-numeric quantities for Temperature text field and display an error in the Result text field like so:
You must include this in the program.
Celsius and Fahrenheit Conversion CEOX Temperature: Celsius to Fahrenheit Fahrenheit to Celsius Results: 0 Celsius is 32 FahrenheitStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started