This is a school project submitted last year, and I've been looking over it and trying to figure out what I could improve on. I know I used celsiusInput
and fahrenheitOutput
the wrong way around in Fahrenheit conversion, that was just laziness at that point and not wanting to break something before handing it in. I would like some advice on improvements I can make, or bad habits I should stay away from in the future.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class window{
static boolean refresh, celsius=true, fahrenheit=true;
protected static JTextField celsiusInput;
protected static JTextArea fahrenheitOutput;
private static void createAndShowGUIfahrenheit() {
celsiusInput = new JTextField(8);
fahrenheitOutput = new JTextArea(1, 8);
fahrenheitOutput.setEditable(false);
JFrame window = new JFrame("Temperature Conversion");
JButton convert = new JButton ("Convert");
JLabel label = new JLabel("Fahrenheit to Celsius");
JPanel convert1 = new JPanel();
convert1.add(convert);
JPanel celsius = new JPanel();
celsius.add(celsiusInput);
JPanel fahrenheit = new JPanel();
fahrenheit.add(fahrenheitOutput);
window.setSize(350, 150);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
window.setLocation(1235, 465);
window.add(convert1, BorderLayout.CENTER);
window.add(celsius, BorderLayout.WEST);
window.add(fahrenheit, BorderLayout.EAST);
window.add(label, BorderLayout.NORTH);
convert.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent ae){
refresh = !refresh;
System.out.println("toggled");
if(refresh==true){
float celTemp = Float.parseFloat(celsiusInput.getText());
float T = celTemp - 32;
T = T * 5/9;
fahrenheitOutput.setText(String.valueOf(T));
System.out.println("true");
refresh=false;
}
}
});
}
private static void createAndShowGUIcelsius() {
celsiusInput = new JTextField(8);
fahrenheitOutput = new JTextArea(1, 8);
fahrenheitOutput.setEditable(false);
JFrame window = new JFrame("Temperature Conversion");
JButton convert = new JButton ("Convert");
JLabel label = new JLabel("Celsius to Fahrenheit");
JPanel convert1 = new JPanel();
convert1.add(convert);
JPanel celsius = new JPanel();
celsius.add(celsiusInput);
JPanel fahrenheit = new JPanel();
fahrenheit.add(fahrenheitOutput);
window.setSize(350, 150);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
window.setLocation(385, 465);
window.add(convert1, BorderLayout.CENTER);
window.add(celsius, BorderLayout.WEST);
window.add(fahrenheit, BorderLayout.EAST);
window.add(label, BorderLayout.NORTH);
convert.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent ae){
refresh = !refresh;
System.out.println("toggled");
if(refresh==true){
float celTemp = Float.parseFloat(celsiusInput.getText());
float T = celTemp * 9/5;
T = T + 32;
fahrenheitOutput.setText(String.valueOf(T));
System.out.println("true");
refresh=false;
}
}
});
}
public static void main( String[] args){
JFrame mainwindow = new JFrame("Temperature Conversion");
JButton quitbtn = new JButton("Exit");
JButton cels = new JButton ("Celsius to Fahrenheit");
JButton fahr = new JButton ("Fahrenheit to Celsius");
JLabel label = new JLabel("Choose an option");
mainwindow.setSize(350, 150);
mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainwindow.setVisible(true);
mainwindow.setResizable(false);
mainwindow.setLocation(785, 265);
mainwindow.add(quitbtn, BorderLayout.SOUTH);
mainwindow.add(cels, BorderLayout.WEST);
mainwindow.add(fahr, BorderLayout.EAST);
mainwindow.add(label, BorderLayout.NORTH);
quitbtn.addActionListener(e -> System.exit(0));
cels.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent ae){
celsius = false;
System.out.println("CELSIUS TRIGGERED");
if (celsius==false){
createAndShowGUIcelsius();
System.out.println("new window celsius");
celsius = true;
}
}
});
fahr.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent ae){
fahrenheit = false;
System.out.println("FAHRENHEIT TRIGGERED");
if (fahrenheit==false){
createAndShowGUIfahrenheit();
System.out.println("new window fahrenheit");
fahrenheit = true;
}
}
});
}
}