I'm trying to create a Swing GUI in Eclipse by following the guide in Oracle's documentation. However, they use Netbeans in the tutorial and I believe that might be the cause of the problem.
I've tried to create a temperature converter but the label for the fahrenheit number is repeatedly complaining that it cannot be resolved. Here is the source code:
private JPanel contentPane;
private JTextField tempTextField; // Not sure if I need the other components here as well?
// Could be needed in order to be used in the
// actionPerformed method.
...
JButton convertButton = new JButton("Convert");
convertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Parse degrees Celsius as a double and convert to Fahrenheit.
int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
* 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " Fahrenheit"); // fahrenheitLabel is what's
} //highlighted in Eclipse.
});
convertButton.setBounds(6, 38, 134, 29);
contentPane.add(convertButton);
JLabel fahrenheitLabel = new JLabel("Fahrenheit");
fahrenheitLabel.setBounds(152, 43, 78, 16);
contentPane.add(fahrenheitLabel);
The link to the Oracle documentation is here: http://docs.oracle.com/javase/tutorial/uiswing/learn/creatinggui.html
I should point out the error persists even when fahrenheitLabel is declared prior to the method - so it definitely can't be the ordering of the code that matters (I think).
If it's anything to do with the components having incorrect names I would be surprised, but here is a screenshot:
Many thanks to anyone who can give suggestions on the matter!
EDIT: Thank you to those who have already given suggestions to fiddle with the ordering of the code. However, it then tells me I need to make the fahrenheitLabel final, which I would have thought conflicts with the ability to dynamically change upon the user entering different inputs.
EDIT 2: Apparently making the variable final is valid. Thank you to all for your suggestions.