(The 2 programs are below) the main is fahrenheit.java and the class isfahrenhei
ID: 3613850 • Letter: #
Question
(The 2 programs are below)the main is fahrenheit.java and the class isfahrenheitpanel.java.
Modify the Fahrenheit program so that it displays a button that,when pressed, also causes the conversion calculation to take place.That is, the user will now have the option of pressing enter in thetext field or pressing the button. Have the listener that isalready defined for the text field also listen for the buttonpush.
fahrenheitpanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class fahrenheitpanel extends JPanel
{
private JLabel inputLabel, outputLabel,resultLabel;
private JTextField fahrenheit;
public fahrenheitpanel()
{
inputLabel = new JLabel("Enter Fahrenheit temperature:");
outputLabel = new JLabel("Temperature in Celsius: ");
resultLabel = new JLabel("---");
fahrenheit = new JTextField(5);
fahrenheit.addActionListener(new TempListener());
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (newDimension(300, 75));
setBackground(Color.yellow);
}
private class TempListener implementsActionListener
{
public void actionPerformed(ActionEvent event)
{
intfahrenheitTemp, celsiusTemp;
Stringtext = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}
fahrenheit.java
import javax.swing.JFrame;
public class fahrenheit
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Fahrenheit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
fahrenheitpanel panel = newfahrenheitpanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class fahrenheitpanel extends JPanel
{
private JLabel inputLabel, outputLabel,resultLabel;
private JTextField fahrenheit;
private JButton calculate;
public fahrenheitpanel()
{
inputLabel = new JLabel("Enter Fahrenheittemperature:");
outputLabel = new JLabel("Temperature in Celsius:");
resultLabel = new JLabel("---");
calculate=new JButton("Calculate");
fahrenheit = new JTextField(5);
fahrenheit.addActionListener (new TempListener());
calculate.addActionListener (new TempListener());
add (inputLabel);
add (fahrenheit);
add(calculate);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}
import javax.swing.JFrame;
public class fahrenheit
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Fahrenheit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
fahrenheitpanel panel = new fahrenheitpanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.