bsc Logo

Let's get started with a Temperature Conversion application!

  1. Open Eclipse
  2. Right-Click on your edu.buffalostate.cis425.fa10.exercises..your-username package
  3. Choose: File -> new -> other
  4. Choose: WindowBuilder -> Swing Designer -> JFrame
  5. In the Name: textbox type: TempConvert
  6. Click: Finish
  7. Add this line after all the import lines:
    import java.text.DecimalFormat;
  8. Click the Design tab
  9. In the Frame -> Properties window, change the Title to: Temperature Conversion App
  10. In the Properties window, change the Layout drop-down to: GridBagLayout
  11. Click on the JPanel icon
  12. Move your cursor into the Frame and click to place the panel inside the frame
    wb window
  13. In the Properties window, change the Layout drop-down to: GridBagLayout
  14. Click on the JLabel icon
  15. Move your cursor into the Frame and click to place the label inside the panel
  16. Change the new Label to: Temperature:
  17. Click on the JTextField icon
  18. Move your cursor into the Frame and click to place the textbox next to the Temperature: label
  19. In the Properties window, change the Variable to: tempStr
  20. Click on the JButton icon
  21. Move your cursor into the Frame and click the mouse button to place the button next to the Temperature: label and textbox
  22. In the Properties window, change the variable to: clickBtn
  23. In the Properties window, change the text to: Click to Convert!
  24. Click on the JTextField icon
  25. Move your cursor into the Frame and click to place the textbox below the button
  26. In the Properties window, change the variable to: fOut
  27. In the Properties window, change the Columns to: 5
  28. Click on the JTextField icon
  29. In the Properties window, change the text to: Farenheit converts to
  30. Click on the JTextField icon
  31. In the Properties window, change the Columns to: 5
  32. In the Properties window, change the variable to: cOut
  33. Right-click on the button
  34. Choose: Add Event Handler -> Action -> Action Performed
  35. When the Source window opens, after this line:
    public void actionPerformed(ActionEvent e) {
    add these lines:
    DecimalFormat myFormat = new DecimalFormat("###.0");
  36. double celsius = 0.0, farenheit = 0.0;
    farenheit = Double.parseDouble(tempStr.getText());
    fOut.setText(tempStr.getText());
    celsius = (farenheit - 32) / 9 * 5;
    cOut.setText(myFormat.format(celsius));
  37. Add this line after the last import statement:
    import java.text.DecimalFormat;
  38. Click the Run icon
  39. Enter your name
  40. Click the button
    Convert Temp window
  41. Click the Design tab and change some of the font names, styles, and sizes, and some of the foreground and background colors
  42. Click the Run icon to view your changes

TempConvert.java Template (Java)

package edu.buffalostate.cis425.fa10.exercises..put-your-username-here;
/**
 * File: TempConvert.java*
 */

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 ConvertTemp 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 {
					ConvertTemp frame = new ConvertTemp();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConvertTemp() {
		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(3);
		
		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);
	}
}