简体   繁体   English

我正在尝试在 Eclipse 中使用 Swing 但我不断收到此错误。 见下文

[英]I am trying to user Swing in Eclipse but I keep getting this error. See below

private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel lblSomeText = new JLabel("Hello, World!");
        frame.getContentPane().add(lblSomeText, BorderLayout.CENTER); //error here

    }

It says: "The Type container is not visible around the frame.getContentPane()"它说:“类型容器在 frame.getContentPane() 周围不可见”

The following complete code works for me.以下完整代码对我有用。 Please check your imports and show us your entire code so we can help you!请检查您的导入并向我们展示您的完整代码,以便我们为您提供帮助!

package stackoverflow;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingTest {

   public static void main( String[] args ) {
      JFrame frame = new JFrame();

      JLabel lblSomeText = new JLabel( "Hello, World!" );
      frame.getContentPane().add( lblSomeText );

      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.setSize( 640, 480 );  // use setSize() instead of setBounds
//      frame.pack();             // or call pack() instead
      frame.setLocationRelativeTo( null );
      frame.setVisible( true );
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试安装SVN连接器时,出现以下错误。 我用过日食霓虹灯 - When i trying to install SVN Connectors i got below error. I used eclipse neon 我正在尝试运行以下提到的代码,但出现错误 - I am trying to run the below mentioned code but getting an error 为什么会出现空指针错误。 - Why am I getting a nullpointer error.? 执行以下代码时,firefox显示错误。 我正在使用最新版本的java,eclipse,firefox和WebDrive jar文件 - When the below code is executed, firefox is showing error. I am using the latest version of java, eclipse, firefox, and WebDrive jar file 当我运行我的 Spring 引导应用程序时,我收到以下错误。 我正在使用 spring 启动版本 2.4.5 - While I am running my Spring Boot application, I am getting the below error. I am using spring boot version 2.4.5 当我运行 Spring 引导应用程序时,我收到以下错误。 我正在使用 spring 启动版本 2.3.1 - When I am running my Spring Boot application, I am getting the below error. I am using spring boot version 2.3.1 我正在创建一个2D游戏,我正在尝试导入一些字体,但我不断收到错误消息 - I am creating a 2d game, i am trying to import some fonts but i keep getting the error 我不断收到“扫描仪无法解析为类型”错误。 我正在使用 Java 和 Visual Studio,这是针对初学者的 Java 课程 - I keep getting an "Scanner cannot be resolved to a type" error. I am using Java and Visual Studio and this is for a beginner Java class 为什么我收到以下代码的错误; - why am i getting the error of below code ; 我正在尝试安装我克隆的apache-storm存储库。 并且正在收到以下错误。 如何解决呢? - Am trying to maven install the apache-storm repo i cloned. And am getting the following error. How to resolve this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM