向JPanel添加JLabel,向JFrame添加JPanel

时间:2023-01-28 15:57:35

New to Java Here.

这里的新Java。

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 *.

我在网上找了一些例子。但是他们没有工作,我也找不到原因。我还在oracle网站和*上进行了搜索。

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:

这是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.

我不能把标签加到面板上,把面板加到框架上。

1 个解决方案

#1


1  

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.

确保在项目的package explorer中有这样的东西。

向JPanel添加JLabel,向JFrame添加JPanel

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.

编辑:我以为你在使用某种IDE,但我猜你可能没有。确保您已经安装了Java,并且安装了JDK。

Can you execute a Hello World? If not, what error messages do you get?

你能创造一个Hello的世界吗?如果没有,您会得到什么错误消息?

Probably the easiest solution will be for you to reinstall your IDE (I'm assuming its Eclipse because of probability). Get eclipse here.

可能最简单的解决方案是重新安装IDE(我假设它的Eclipse是由于概率的原因)。让eclipse。

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.

如果问题在此之后继续存在,请尝试重新安装Java(确保您得到了JDK)。把它在这里。它提供卸载旧版本的Java,随它去吧。您不需要3个版本的Java把系统搞得一团糟。

#1


1  

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.

确保在项目的package explorer中有这样的东西。

向JPanel添加JLabel,向JFrame添加JPanel

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.

编辑:我以为你在使用某种IDE,但我猜你可能没有。确保您已经安装了Java,并且安装了JDK。

Can you execute a Hello World? If not, what error messages do you get?

你能创造一个Hello的世界吗?如果没有,您会得到什么错误消息?

Probably the easiest solution will be for you to reinstall your IDE (I'm assuming its Eclipse because of probability). Get eclipse here.

可能最简单的解决方案是重新安装IDE(我假设它的Eclipse是由于概率的原因)。让eclipse。

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.

如果问题在此之后继续存在,请尝试重新安装Java(确保您得到了JDK)。把它在这里。它提供卸载旧版本的Java,随它去吧。您不需要3个版本的Java把系统搞得一团糟。