JakeSteirer21
JakeSteirer21

Reputation: 23

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()"

Upvotes: 0

Views: 38

Answers (1)

markspace
markspace

Reputation: 11020

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

}

Upvotes: 1

Related Questions