(java) eclipse (Question PP 6.4 from book) Modify the Farenheit program so that
ID: 3692379 • Letter: #
Question
(java) eclipse
(Question PP 6.4 from book) Modify the Farenheit program so that it displays a button that, when pressed, causes the conversion calculation to take place. That is, your modification will give the user the option of pressing Enter in the text field or pressing the button. Have the listener that is already defined for the text field also listen for the button push.
(Question PP 6.8 from book) Develop an application that implements a prototype user interface for composing an email message. The application should have text fields for the To, Cc, and Bcc address lists and subject line, and one for the message body. Include a button labeled Send. When the send button is pushed, the program should print the contents of all fields to standard output using println statements.
Explanation / Answer
PP 6.4:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FahrenheitPanel extends JPanel
{
private JButton But;
private JLabel inputLabel1, outputLabel1, resultLabel1;
private JTextField Fahrenheit;
public FahrenheitPanel( )
{
But=new JButton(“Enter”);
inputLabel1 = new JLAbel (“Enter Fahrenheit temperature:”);
outputLabel1 = new JLabel (“Temperature in Celsius:”);
resultLabel = new JLabel (“-----“);
farhrenheit = new JTextField (5);
farhrenheit.addActionListener (new TempListener ( ) );
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
add (But);
setPreferredSize (new Dimension (300, 75));
setBackground (Color.yellow);
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
double farhrenheitTemp, celsiusTemp;
String text = Fahrenheit.getText( );
fahrenheitTemp = Double.parseDouble (text);
celsiusTemp = (fahrenheitTemp-32)*5/9;
DecimalFormat df= new DecimalFormat (“##.##”);
resultLabel.setText (df.format (celsiusTemp));
}
}
}
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.getContentPanel( ).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.