[英]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.