Advertisement
thotfrnk

SwingTemperatureConverter.java

Jun 7th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. // school work
  2. import java.awt.*; //using awt's layouts
  3. import java.awt.event.*; //using awt's event classes and listener interfaces
  4. import javax.swing.*; //using swing's components and container
  5.  
  6. //a swing application extends from javax.swing.JFrame
  7. public class SwingTemperatureConverter extends JFrame {
  8.     private JTextField tfCelsius, tfFahrenheit;
  9.     private float celsius, fahrenheit;
  10.  
  11.     public SwingTemperatureConverter() {
  12.         //swing components should be added to the content-pane of the JFrame
  13.         Container cp = getContentPane();
  14.         //set this container to grid layout of 2 rows and 2 columns
  15.         cp.setLayout(new GridLayout(2, 2, 10, 3));
  16.  
  17.         //components are added left-to-right, top-to-bottom
  18.         cp.add(new JLabel("Celsius "));
  19.         tfCelsius = new JTextField(10);
  20.         tfCelsius.setHorizontalAlignment(JTextField.RIGHT);
  21.         cp.add(tfCelsius);
  22.  
  23.         tfCelsius.addActionListener(new ActionListener() {
  24.             @Override
  25.             public void actionPerformed(ActionEvent evt) {
  26.                 celsius = Float.parseFloat(tfCelsius.getText());
  27.  
  28.                 fahrenheit = (float) (9.0 / 5.0 * (celsius + 32));
  29.  
  30.                 //tfFahrenheit.setText(fahrenheit + "");
  31.                 //String.format("%.1f", fahrenheit);
  32.                 tfFahrenheit.setText(String.format("%.1f", fahrenheit));
  33.             }
  34.         });
  35.  
  36.         cp.add(new JLabel("Fahrenheit "));
  37.         tfFahrenheit = new JTextField(10);
  38.         tfFahrenheit.setHorizontalAlignment(JTextField.RIGHT);
  39.         cp.add(tfFahrenheit);
  40.  
  41.         tfFahrenheit.addActionListener(new ActionListener() {
  42.             @Override
  43.             public void actionPerformed(ActionEvent e) {
  44.                 fahrenheit = Float.parseFloat(tfFahrenheit.getText());
  45.  
  46.                 celsius = (float) (5.0 / 9.0 * (fahrenheit - 32));
  47.  
  48.                 //tfCelsius.setText(celsius + "");
  49.                 tfCelsius.setText(String.format("%.1f", celsius));
  50.             }
  51.         });
  52.  
  53.         setDefaultCloseOperation(EXIT_ON_CLOSE); // for the "window-close" button
  54.         setTitle("Swing Temperature Converter");
  55.         setSize(300, 170);
  56.         setVisible(true);
  57.     }
  58.  
  59.     // The entry main() method
  60.     public static void main(String[] args) {
  61.         // For thread safety, use the event-dispatching thread to construct UI
  62.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  63.             @Override
  64.             public void run() {
  65.                 new SwingTemperatureConverter(); // Let the constructor does the job
  66.             }
  67.         });
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement