Application with GUI

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The JTextField for Fahrenheit termperatue input.

Application with GUI

GUI of the application

Here is the program (not yet complete). It is based on the example in the previous chapter, so most of it should be understandable. Decide what belongs in the program then click on the button to see an anwser. Look at the picture as you decide in what order to add components.

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*; 

public class FahrConvert extends  JFrame  implements  
{
  JLabel heading   = new JLabel("Convert Fahrenheit to Celsius");
  JLabel inLabel   = new JLabel("Fahrenheit    ");
  JLabel outLabel  = new JLabel("Celsius ");

  JTextField inFahr = new JTextField( 7 );
  JTextField outCel = new JTextField( 7 );

  int fahrTemp ;  // input  
  int celsTemp ;  // result 
  
  public FahrConvert()   // constructor 
  {  
     // choose the layout manager 
     setLayout( new  () ) ; 

     inFahr.addActionListener( this );
     
     add(   ) ;
     add(   ) ;
     
     add( outLabel );   
     add( inFahr );   
     add( outCel );   
     outCel.setEditable( ) ;
     
     setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
  }

   . . . . . . 

}

The GUI is nearly complete, but more work is needed:

  • There should be code to deal with the user's input.
  • The application needs to be called to process the data.
  • The result should be displayed.

QUESTION 5:

What method is used to get the text from the input JTextField?