有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将JLabel添加到JPanel,将JPanel添加到JFrame

这里是Java新手

我在网上查了一下,用了我找到的例子。但它们不起作用,我也不知道为什么。 我还搜索了甲骨文网站和stackoverflow

我遇到了这些我无法解决的错误

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); 
  }

}

以下是错误:

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

它们出现在以下几行:

frame.add(panel);    
panel.add(label);

我无法将标签添加到面板,无法将面板添加到框架


共 (0) 个答案