gcp Logo

Let's get started with a Temperature Conversion application!

  • Login to your Repl.It account.
  • Click on: Languages
  • In the GUI Development section choose: Java Swing: A Java GUI widget toolkit
  • Click the pencil icon next to your repl name.
  • In the Default name: input box type: TempConvertGUI
  • Delete all the lines of code provided as an example.
  • Copy the TempConvertGui.java Template (Java) code (below)
  • Paste that code into your new TempConvertGUI class in the editor
  • Change the @author put-your-username-here
  • TempConvertGUI.java Template (Java)

    /*
     * File: TempConvert.java
     *
     * This class displays two (2) temperatures, then increments the temperatures
     * and displays the new temperatures.
     *
     */
    /**
     *
     * @author put-your-name-here
     *
     */
    
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Font;
    import javax.swing.JButton;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    //import java.text.NumberFormat;
    import java.text.DecimalFormat;
    
    public class Main extends JFrame {
    
    	private JPanel contentPane;
    	private JTextField tempStr;
    	private JTextField farenheitUser;
    	private JTextField celsiusOut;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Main frame = new Main();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public Main() {
    		setTitle("Temperature Conversion App");
    		DecimalFormat myFormat = new DecimalFormat("###.0");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.PINK);
    		contentPane.add(panel, BorderLayout.CENTER);
    		panel.setLayout(null);
    		
    		JLabel lblTemperature = new JLabel("Temperature:");
    		lblTemperature.setBackground(Color.BLUE);
    		lblTemperature.setForeground(Color.WHITE);
    		lblTemperature.setFont(new Font("Calibri", Font.BOLD, 18));
    		lblTemperature.setBounds(20, 11, 110, 30);
    		panel.add(lblTemperature);
    		
    		tempStr = new JTextField();
    		tempStr.setBounds(140, 11, 26, 32);
    		panel.add(tempStr);
    		tempStr.setColumns(10);
    		
    		JButton btnConvertToCentigrade = new JButton("Convert to Centigrade");
    		btnConvertToCentigrade.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				double celsius = 0.0, farenheit = 0.0;
    				farenheit = Double.parseDouble(tempStr.getText());
    				farenheitUser.setText(tempStr.getText());
    				celsius = (farenheit - 32) / 9 * 5;
    				celsiusOut.setText(myFormat.format(celsius)); 
    			}
    		});
    		btnConvertToCentigrade.setBounds(20, 52, 146, 23);
    		panel.add(btnConvertToCentigrade);
    		
    		farenheitUser = new JTextField();
    		farenheitUser.setBounds(20, 86, 47, 32); // x, y w, h
    		panel.add(farenheitUser);
    		farenheitUser.setColumns(10);
    		
    		JLabel lblNewLabel = new JLabel("Farenheit converts to");
    		lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 18));
    		lblNewLabel.setBounds(77, 85, 400, 32);
    		panel.add(lblNewLabel);
    		
    		celsiusOut = new JTextField();
    		celsiusOut.setBounds(20, 150, 47, 32);
    		panel.add(celsiusOut);
    		celsiusOut.setColumns(10);
    		
    		JLabel lblCelcius = new JLabel("Celsius");
    		lblCelcius.setFont(new Font("Calibri", Font.BOLD, 18));
    		lblCelcius.setBounds(77, 150, 200, 32);
    		panel.add(lblCelcius);
    	}
    }