Write a temperature conversion program. The GUI and event handling setup should
ID: 3620637 • Letter: W
Question
Write a temperature conversion program. The GUI and event handling setup should be done in the constructor of the class that implements the GUI.Your GUI class should contain a JFrame member variable. Do not use any of the GUI editing capabilities of Eclipse for this assignment.
The temperature conversion application should have a JLabel and JTextField where the user inputs a value. There should be a set of 6 JButtons on the display
representing the following temperature conversions:
F-to-C F: Fahrenheit
C-to-F C: Celcius
K-to-C K: Kelvin
C-to-K
K-to-F
F-to-K
Event handling should be setup so that clicking on any one of the 6 buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celcius, Kelvin to Fahrenheit, Celcius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display 3 digits after the decimal point.
NOTE: you can find conversion formulas at the following website:
http://library.thinkquest.org/20991/gather/formula/data/129.html
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ConvTemp {
JFrame frame;
JPanel top, center, bottom;
JButton f2c, c2f, k2c, c2k, k2f, f2k;
JLabel input, output;
JTextField tfin, tfout;
ConvTemp() {
input = new JLabel("Enter a temperature"); //
tfin = new JTextField(5); //
output = new JLabel("Result of conversion"); //
tfout = new JTextField(5); //
top = new JPanel(); //
center = new JPanel(new GridLayout(2,3,10,10)); //
bottom = new JPanel(); //
top.add(input); //
top.add(tfin); //
center.add(f2c); //
center.add(c2f); //
center.add(k2c); //
center.add(c2k); //
center.add(f2k); //
center.add(k2f); //
bottom.add(output); //
bottom.add(tfout); //
frame = new JFrame("Temperature Conversion Application"); //
frame.add(top, BorderLayout.NORTH); //
frame.add(center, BorderLayout.CENTER); //
frame.add(bottom, BorderLayout.SOUTH); //
frame.pack(); //
frame.setVisible(true); //
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
buttonHandler bh = new buttonHandler(); //
f2c.addActionListener(bh); //
c2f.addActionListener(bh); //
k2c.addActionListener(bh); //
c2k.addActionListener(bh); //
k2f.addActionListener(bh); //
f2k.addActionListener(bh); //
}
private class buttonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double out;
double in = Double.parseDouble(tfin.getText());
if (e.getSource() == f2c) {
out = 5.0 / 9.0 * (in - 32);
} else if (e.getSource() == c2f) {
out = 9.0 / 5.0 * in + 32;
} else if (e.getSource() == k2c) {
out = in - 273.15;
} else if (e.getSource() == c2k) {
out = in + 273.15;
} else if (e.getSource() == k2f) {
out = (in - 273.15) * 9.0/5.0 + 32;
} else { // f2k
out = 5.0/9.0 * (in - 32) + 273.15;
}
tfout.setText(String.format("%.3f",out));
}
}
public static void main(String[] args) {
ConvTemp app = new ConvTemp();
}
}
Explanation / Answer
/* Import classes for graphics components. This package contains the frame that the JFrame window, panels, labels, etc. A component is an object that defines a screen element to display information or allow the user to interact with a program in some way*/ import javax.swing.*; /* Older GUI-related class that that has been replaced by javax.swing. Contains important classes such as the Color class and other Graphics classes. Both packages are needed for GUI development, but the javax.swing package components are used whenever there is an option */ import java.awt.*; /* Usually for classes that handle user events such as mouse movement, keys being pressed on the keyboard, etc. Most GUI components generate events to indicate a user action related to that component*/ import java.awt.event.*; public class ConvTemp { /* Declares the main GUI frame. This is the main window to display the GUI components. The frame is usually displayed as a separate window with its own title bar. It can be repositioned on the screen and resized as needed by dragging it with the mouse*/ JFrame frame; /* Declares the panels that will be placed in the frame. Panels cannot be displayed on their own. The panels are where the buttons, labels and textboxes go. Its primary role is to help organize the other components in the GUI. There are three panels in the frame. */ JPanel top, center, bottom; /* Declares the buttons that will be placed in the panels. These are buttons that you can push. However, pushing these buttons won't do anything until you add some code that does something when you push them. */ JButton f2c, c2f, k2c, c2k, k2f, f2k; /* A label is a component that displays a line of text in the GUI. It can also display an image. Usually, labels are used to display information or identify other components in the GUI */ JLabel input, output; /* A component where the user can enter in text*/ JTextField tfin, tfout; ConvTemp() { /* Create a label called input that contains the text "Enter a temperature" */ input = new JLabel("Enter a temperature"); // /* Create a text field with space for 5 characters to be entered */ tfin = new JTextField(5); // /* Create a label called output that contains the text "Result of the conversion" */ output = new JLabel("Result of conversion"); // /* Create a text field called tfout with space for 5 characters to be entered */ tfout = new JTextField(5); // /* Create a panel contained called top. */ top = new JPanel(); // /* Create a new panel called center. A GridLayout creates a grid area of equal sized rectangles to put components in. JPanel(LayoutManager manager), where the layout manager is GridLayout, creates a panel with the grid layout. The GridLayout used here has a total of 2 rows and 3 columns. The grid cells will have a horizontal gap of 10 pixels and a vertical gap of 10 pixels */ center = new JPanel(new GridLayout(2,3,10,10)); // /* Create a new panel called bottom */ bottom = new JPanel(); // /* Put the label called input into the panel called top */ top.add(input); // /* Put the textfield called tfin into the panel called top */ top.add(tfin); // /* The next few lines of code puts the button components into a panel. Usually when you put a component into a panel they are lined up one by one starting at the top left hand corner and ending at the bottom right hand corner. */ /* Put the button called f2c into the panel called center */ center.add(f2c); // /* Put the button called c2f into the panel called center */ center.add(c2f); // /* Put the button called k2c into the panel called center */ center.add(k2c); // /* Put the button called c2k into the panel called center */ center.add(c2k); // /* Put the button called f2k into the panel called center */ center.add(f2k); // /* Put the button called k2f into the panel called center */ center.add(k2f); // /* Put the label called output into the panel called bottom */ bottom.add(output); // /* Put the textfield called tfout into the panel called bottom */ bottom.add(tfout); // /* Create the JFrame. The title of the frame, which will appear in the top left hand corner of the frame, will be "Temperature Conversion Application". */ frame = new JFrame("Temperature Conversion Application"); // /* When you put objects in a container using BorderLayout, it places objects in the direction specified. The directions are north, south, east, west and center. If you specify north, the object is place in the northern portion of the container. If you specify east, the objects are placed in the eastern portion of the container.*/ /* Here, the panel top is added to the northern portion of the frame. Remember the frame is the main GUI window were everything goes */ frame.add(top, BorderLayout.NORTH); // /* The panel center is added to the center portion of the frame */ frame.add(center, BorderLayout.CENTER); // /* The panel bottom is added to the southern portion of the frame */ frame.add(bottom, BorderLayout.SOUTH); // /* Packing the frame ensures that the GUI window resizes so that all of the components that you added to it fit and are viewable */ frame.pack(); // /* setVisible(boolean) ensures that the frame is visible. Setting setVisible to false will hide the frame */ frame.setVisible(true); // /* setDefaultCloseOperation() sets what will happen on default when the user attempts to close the frame. Setting the action to JFrame.EXIT_ON_CLOSE will ensure that the JFrame exits using System.exit and ends completely */ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // /* Create an event called bh that will handle the buttons so that they do something when they are clicked with the mouse */ buttonHandler bh = new buttonHandler(); // /* The next few lines of code ensure that the program is watching for when the user clicks one of the buttons. Each time any of the buttons are clicked, the function buttonhandler will be called so that something happens. */ /* Call buttonhandler when the button f2c is clicked */ f2c.addActionListener(bh); // /* Call buttonhandler when the button c2f is clicked */ c2f.addActionListener(bh); // /* Call buttonhandler when the button k2c is clicked */ k2c.addActionListener(bh); // /* Call buttonhandler when the button c2k is clicked */ c2k.addActionListener(bh); // /* Call buttonhandler when the button k2f is clicked */ k2f.addActionListener(bh); // /* Call buttonhandler when the button f2k is clicked */ f2k.addActionListener(bh); // } /* buttonhandler is an event listener that will perform an action when a button is clicked. buttonhandler implements the interface ActionListener which only has one method, actionPerformed. Therefore buttonhandler must implement actionPerformed. The component that generates the action event (in this case the button) will call the actionPerformed method when the event occurs, passing an ActionEvent object that represents the event.*/ private class buttonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { /* Declares a variable of type double called out */ double out; /* Instantiates a variable of type double called in. The variable in is set the value that the user types into the textfield called tfin */ double in = Double.parseDouble(tfin.getText()); /* If the button that called buttonhandler was f2c, then do a calculation */ if (e.getSource() == f2c) { /* Converts the user input from the textfield tfin from degrees fahrenheit to degrees celcius. Stores the solution in variable out. */ out = 5.0 / 9.0 * (in - 32); /* if the button that called the buttonhandler was c2f, then do something */ } else if (e.getSource() == c2f) { /* Converts the user input gathered from textfield tfin from degrees celcius to degrees fahrenheit. Stores the solution in variable out. */ out = 9.0 / 5.0 * in + 32; /* If the button that called the buttonhandler was k2c, then do something */ } else if (e.getSource() == k2c) { /* Converts the user input gathered from textfield tfin from degrees kelvin to degrees celcius. Stores the solution in variable out. */ out = in - 273.15; /* If the button that called the buttonhandler was c2k, then do something */ } else if (e.getSource() == c2k) { /* Converts the user input gathered from textfield tfin from degrees celcius to degrees kelvin. Stores the solution in variable out. */ out = in + 273.15; /* If the button that called the buttonhandler was k2f, then do something */ } else if (e.getSource() == k2f) { /* Converts the user input gathered from textfield tfin from degrees kelvin to degrees fahrenheit. Stores the solution in variable out. */ out = (in - 273.15) * 9.0/5.0 + 32; /* If it was none of the other buttons, then it must have been the button f2k */ } else { // f2k /* Converts the user input gathered from textfield tfin from degrees fahrenheit to degrees kelvin. Stores the solution variable out. */ out = 5.0/9.0 * (in - 32) + 273.15; } /* Set the value of the textfield tfout to display the results from the calculations into the format %.3f. The %.3f format specifies that out should be displayed as a floating-point decimal with 3 significant digits after the decimal point. */ tfout.setText(String.format("%.3f",out)); } } public static void main(String[] args) { /* Create the ConvTemp GUI object */ ConvTemp app = new ConvTemp(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.