Let's get started with a Temperature Conversion application!
- Open Eclipse
- Right-Click on your
edu.buffalostate.cis425.sp24.your-usernamepackage - Choose:
File -> new -> other - Choose:
WindowBuilder -> Swing Designer -> JFrame - In the
Name:textbox type:TempConvert - Click:
Finish - Add this line after all the
importlines:
import java.text.DecimalFormat; - Click the
Designtab - In the Frame -> Properties window, change the Title to:
Temperature Conversion App - In the Properties window, change the Layout drop-down to:
GridBagLayout - Click on the
JPanelicon - Move your cursor into the Frame and click to place the panel inside the frame

- In the Properties window, change the Layout drop-down to:
GridBagLayout - Click on the
JLabelicon - Move your cursor into the Frame and click to place the label inside the panel
- Change the new Label to:
Temperature: - Click on the
JTextFieldicon - Move your cursor into the Frame and click to place the textbox next to the
Temperature:label - In the Properties window, change the Variable to:
tempStr - Click on the
JButtonicon - Move your cursor into the Frame and click the mouse button to place the button next to the
Temperature:label and textbox - In the Properties window, change the variable to:
clickBtn - In the Properties window, change the text to:
Click to Convert! - Click on the
JTextFieldicon - Move your cursor into the Frame and click to place the textbox below the button
- In the Properties window, change the variable to:
fOut - In the Properties window, change the Columns to:
5 - Click on the
JTextFieldicon - In the Properties window, change the text to:
Farenheit converts to - Click on the
JTextFieldicon - In the Properties window, change the Columns to:
5 - In the Properties window, change the variable to:
cOut - Right-click on the button
- Choose: Add Event Handler -> Action -> Action Performed
- When the
Sourcewindow opens, after this line:
public void actionPerformed(ActionEvent e) {
add these lines:
DecimalFormat myFormat = new DecimalFormat("###.0");
double celsius = 0.0, farenheit = 0.0; - Add this line after the last import statement:
import java.text.DecimalFormat; - Click the Run icon
- Enter your name
- Click the button

- Click the Design tab and change some of the font names, styles, and sizes, and some of the foreground and background colors
- Click the Run icon to view your changes
farenheit = Double.parseDouble(tempStr.getText());
fOut.setText(tempStr.getText());
celsius = (farenheit - 32) / 9 * 5;
cOut.setText(myFormat.format(celsius));
TempConvert.java Template (Java)
package edu.buffalostate.cis425.sp24.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); } }
