简体   繁体   中英

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM