Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with Java GUI Javafx Converts Celsius to Fahrenheit By using this program:

ID: 3686882 • Letter: H

Question

Help with Java GUI Javafx Converts Celsius to Fahrenheit

By using this program:

import javafx.application.*;

import javafx.scene.*;

import javafx.stage.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.event.*;

import javafx.geometry.*;

public class TextFieldDemo extends Application {

  

TextField tf;

Label response;

Button btnGetText;

  

  

public static void main(String[] args) {

  

launch(args);

  

}

  

public void start(Stage myStage) {

  

myStage.setTitle("TextField Demo");

  

FlowPane rootNode = new FlowPane(10,10);

  

// Center the controls in the scene

rootNode.setAlignment(Pos.CENTER);

  

// Create a scene

Scene myScene = new Scene(rootNode, 230, 140);

  

// Set the scene on the stage.

myStage.setScene(myScene);

  

// Create a label that will display the string

response = new Label("You entered: ");

  

// Create a button that gets the text.

btnGetText = new Button("Get String");

  

// Create a text field

tf = new TextField();

  

// Set the prompt

tf.setPromptText("Enter a String");

  

// Set the preferred column count

tf.setPrefColumnCount(15);

  

// when ENTER is pressed

tf.setOnAction(new EventHandler() {

public void handle(ActionEvent ae) {

response.setText("You entered: " + tf.getText());

}

});

  

// get text when button is pressed

btnGetText.setOnAction(new EventHandler() {

public void handle(ActionEvent ae) {

response.setText("You entered: " + tf.getText());

}

});

  

  

  

  

  

// Use a separator to better organize the layout.

Separator separator = new Separator();

separator.setPrefWidth(200);

  

// Add controls to the scene graph.

rootNode.getChildren().addAll(tf, btnGetText, separator, response);

  

  

// Show the stage and its scene.

myStage.show();

  

}

}

Explanation / Answer

import java.awt.event.ActionEvent; 002 importjava.awt.event.ActionListener; 003 import javax.swing.JButton; 004 import javax.swing.JFrame; 005 import javax.swing.JLabel; 006 import javax.swing.JPanel; 007 import javax.swing.JTextField; 008 009    010 /* 011 012 * To change this license header, choose License Headers in Project Properties. 013 014 * To change this template file, choose Tools | Templates 015 016 * and open the template in the editor. 017 018 */ 019 020    021 /** 022 023 * 024 025 * @author callawad 026 027 */ 028 029 public class CtoFConverter 030 { 031     private static ActionListener QuitListener; 032     private static ActionListener ConvertListener; 033     public static voidmain(String[] args) 034 035     { 036         final int WIDTH = 500; 037         final int HEIGHT = 100; 038         JFrame frame; 039         JButton quitBtn, convertBtn; 040         JLabel farenLbl, celsiusLbl; 041         final JTextField farenTf, celsiusTf; 042         JPanel panel; 043         final ActionListener quitListener, convertListener; 044 045         //create the frame 046         frame = new JFrame ("My C to F Converter");    047 048         //create the panel 049         panel = new JPanel(); 050        051         //create the componets 052         farenLbl = newJLabel("Fahrenheit Value: "); 053         farenTf = newJTextField(10);  // 10 is the field width in characters 054    055 056         celsiusLbl = newJLabel("Celsius Value: "); 057         celsiusTf = newJTextField(10); 058             059         convertBtn = newJButton("Convert Celsius to Fahrenheit!"); 060         quitBtn = newJButton("Quit"); 061 062           063         // create and add the actionListeners 064         class QuitListenerimplements ActionListener 065         { 066             public voidactionPerformed(ActionEvent event) 067             { 068                 System.exit(0); // 0 means quit with no error 069 070             } 071 072         } 073           074         quitListener = newQuitListener();  // Create one instance of the class 075         quitBtn.addActionListener(quitListener); 076 077            078 079         // create and add the ConvertListener 080         class ConvertListenerimplements ActionListener 081         { 082                public voidactionPerformed(ActionEvent event) 083                { 084 085                    // Fetch the C value from the TextField 086                    String cVal = celsiusTf.getText(); 087 088                    // convert string val from GUI to numeric 089                    double cDeg = Double.parseDouble(cVal);                    090 091                    // calculate the F value from the inputted C val 092                    double fDeg = (cDeg * 1.80) + 32; 093 094                    // Display the Calculate C val 095                    farenTf.setText(fDeg + ""); 096 097                } 098 099         } 100 101         ActionListener Listener =new ConvertListener(); 102         convertBtn.addActionListener(ConvertListener); 103           104         // add components to the panel 105         panel.add(farenLbl); 106         panel.add(farenTf); 107         panel.add(celsiusLbl); 108         panel.add(celsiusTf); 109         panel.add(convertBtn); 110         panel.add(quitBtn); 111 112           113         // add the panel to the frame   114         frame.add(panel); 115         frame.setSize(WIDTH, HEIGHT); 116         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 117         frame.setVisible(true); 118 119         120     } 121       122 }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote