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

时间:2020-05-03 21:00:21

标签: java eclipse swing

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

    }

它说:“ Type容器在frame.getContentPane()周围不可见”

1 个答案:

答案 0 :(得分:1)

以下完整代码对我有用。请检查您的导入并向我们显示您的完整代码,以便我们为您提供帮助!

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

}