Reputation: 23
New to Java Here.
I looked on the Internet and I used examples I found. But they did not work and I can't find out why. I also made searches on the oracle website and on stackoverflow.
I get these errors I can't resolve.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HelloWorld {
public static void main(String[] args) {
JLabel label = new JLabel("This is a label");
JPanel panel = new JPanel();
JFrame frame = new JFrame("Window Title");
frame.setSize(800, 500);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.add(label);
frame.setVisible(true);
}
}
Here are the errrors:
The project was not built since its build path is incomplete. Cannot find the class file for javax.swing.JComponent. Fix the build path then try building this project
The type javax.swing.JComponent cannot be resolved. It is indirectly referenced from required .class files
They appear at these lines:
frame.add(panel);
panel.add(label);
I can't add the label to the panel, and the panel to the frame.
Upvotes: 1
Views: 435
Reputation: 677
Your problem is not with your code, but with your project setup, somehow.
I just ran your code and it works fine
Make sure you've got something like this in your package explorer for the project.
EDIT: I assumed you were using an IDE of some sort, but I suppose its possible you're not. Make sure you have Java installed, and that you have the JDK installed.
Can you execute a Hello World? If not, what error messages do you get?
Probably the easiest solution will be for you to reinstall your IDE (I'm assuming its Eclipse because of probability). Get eclipse here.
If the problem persists after that, try reinstalling Java (make sure you get the JDK). Get it here. It it offers to uninstall older versions of Java, let it. You don't need 3 versions of Java cluttering up the system.
Upvotes: 1