Hello Everyone,
I am experiencing errors when trying to compile a project in Java 8. The errors are caused by swing components. The code I am using is the following:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main2 {
public Main2(){
JFrame f = new JFrame ();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(100, 100);
JPanel p = new JPanel();
JLabel l = new JLabel("Test");
p.add(l);
f.add(p);
f.setVisible(true);
}
public static void main(String[] args) {
new Main2();
}
}
The problems are caused by the p.add(l) and the f.add(p). They throw a The method add(JLabel) is undefined for the type JPanel
and a The method add(Component) in the type Container is not applicable for the arguments (JPanel)
error respectively. I have tried cleaning the project, and I have searched through all the solutions online and none of them worked. If I change f.add(p) to f.getRootPane().add(l) then it throws a The type javax.swing.JComponent cannot be resolved. It is indirectly referenced from required .class files
. I have tried reinstalling the JDK, but I still have no success.
If I change the version of the JDK to 1.7 then the code works prefectly fine. The error is just with Java 8.
The exact versions of JDK I am using is 1.8.0_45 and 1.7.0_11
Thank you for any help.