Witam. Ostatnio dużo zastanawiałem się nad tym jak używać poprawnie static i final w kodzie. Program, który chcę zrobić ma konwertować stopnie Celsiusza na Fahrenheita i z powrotem. Klasa CelsiusToFahrenheit zawiera main i wyświetla okno. Jeśli dobrze rozumiem, to w niej jeśli już powinny się znajdować zmienne final o ile jakieś bym zadeklarował. Listener_CelToFahr i Listener_FahrToCel to dwie klasy obsługujące dwa ActionListenery zgodnie z tym co mi tutaj doradzano. Wymyśliłem sobie, że żeby nie powtarzać deklaracji zmiennych w klasie main, w każdym ActionListenerze i nie tworzyć zbyt długich konstruktorów, to dobrze byłoby te zmienne przerzucić do jednej klasy (Variables) i w niej zadeklarować je jako static. Chciałbym prosić o ocenę, czy to wygląda tak jak należy:
public class CelsiusToFahrenheit extends JFrame{
public CelsiusToFahrenheit() {
setSize(400, 200);
setTitle(Variables.title);
setLayout(null);
Variables.lCelsius = new JLabel("Set data:");
Variables.lCelsius.setBounds(20, 20, 150, 20);
add(Variables.lCelsius);
Variables.tInput = new JTextField("");
Variables.tInput.setBounds(170, 20, 150, 20);
add(Variables.tInput);
Variables.tInput.setToolTipText("Enter the temperature");
Variables.lFahrenheit = new JLabel("Score:");
Variables.lFahrenheit.setBounds(20, 70, 150, 20);
add(Variables.lFahrenheit);
Variables.tOutput = new JTextField("");
Variables.tOutput.setBounds(170, 70, 150, 20);
add(Variables.tOutput);
Variables.bConvert = new JButton("Convert");
Variables.bConvert.setBounds(100, 120, 150, 20);
add(Variables.bConvert);
Variables.bgSize = new ButtonGroup();
Variables.CelToFahr = new JRadioButton("Celsius to Fahrenheit", true);
Variables.CelToFahr.setBounds(20, 150, 180, 20);
Variables.bgSize.add(Variables.CelToFahr);
add(Variables.CelToFahr);
Variables.FahrToCel = new JRadioButton("Fahrenheit to Celsius", false);
Variables.FahrToCel.setBounds(200, 150, 180, 20);
Variables.bgSize.add(Variables.FahrToCel);
add(Variables.FahrToCel);
Listener_CelToFahr lctf = new Listener_CelToFahr();
Listener_FahrToCel lftc = new Listener_FahrToCel();
Variables.bConvert.addActionListener(lctf);
Variables.bConvert.addActionListener(lftc);
}
public static void main(String[] args) {
CelsiusToFahrenheit ceToFahr = new CelsiusToFahrenheit();
ceToFahr.setVisible(true);
ceToFahr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Variables {
static JLabel lCelsius, lFahrenheit;
static JButton bConvert;
static ButtonGroup bgSize;
static JRadioButton CelToFahr, FahrToCel;
static JTextField tInput, tOutput;
static double tempCelsius, tempFahrenheit;
static String title = "Temperature";
}
public class Listener_CelToFahr extends Variables implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (CelToFahr.isSelected())
{
tempCelsius = Double.parseDouble(tInput.getText());
tempFahrenheit = 32.0 + (9.0 / 5.0) * tempCelsius;
tOutput.setText(String.valueOf(tempFahrenheit));
}
}
}
public class Listener_FahrToCel extends Variables implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(!CelToFahr.isSelected()) {
tempFahrenheit = Double.parseDouble(tInput.getText());
tempCelsius = (tempFahrenheit-32)/1.8;
tOutput.setText(String.valueOf(tempCelsius));
}
}
}