import java.awt.*; import javax.swing.*;public class Welcome extends JFrame { private JLabel textJLabel; // JLabel that displays text private JLabel pictureJLabel; // JLabel that displays an image // no-argument constructor public Welcome() { createUserInterface(); } // create and position GUI components; register event handlers private void createUserInterface() { // get content pane and set layout to null Container contentPane = getContentPane(); contentPane.setBackground( Color.YELLOW ); contentPane.setLayout( null );
// set up textJLabel textJLabel = new JLabel(); textJLabel.setText( "Welcome to Java Programming!" ); textJLabel.setLocation( 35, 0 ); textJLabel.setSize( 550, 88 ); textJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 ) ); textJLabel.setHorizontalAlignment( JLabel.CENTER ); contentPane.add( textJLabel );
// set up pictureJLabel pictureJLabel = new JLabel(); pictureJLabel.setIcon( new ImageIcon( "bug.png" ) ); pictureJLabel.setBounds( 54, 120, 500, 250 ); pictureJLabel.setHorizontalAlignment( JLabel.CENTER ); contentPane.add( pictureJLabel ); // set properties of application's window setTitle( "Welcome" ); // set JFrame's title bar string setSize( 608, 413 ); // set width and height of JFrame setVisible( true ); // display JFrame on screen } // end method createUserInterface // main method public static void main( String[] args ) { Welcome application = new Welcome(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main} // end class Welcome 代码是书上的例子应该是没问题的, 1L的方法试过了还是同样的错误
import javax.swing.*;public class Welcome extends JFrame
{
private JLabel textJLabel; // JLabel that displays text
private JLabel pictureJLabel; // JLabel that displays an image // no-argument constructor
public Welcome()
{
createUserInterface();
} // create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane and set layout to null
Container contentPane = getContentPane();
contentPane.setBackground( Color.YELLOW );
contentPane.setLayout( null );
// set up textJLabel
textJLabel = new JLabel();
textJLabel.setText( "Welcome to Java Programming!" );
textJLabel.setLocation( 35, 0 );
textJLabel.setSize( 550, 88 );
textJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 ) );
textJLabel.setHorizontalAlignment( JLabel.CENTER );
contentPane.add( textJLabel );
// set up pictureJLabel
pictureJLabel = new JLabel();
pictureJLabel.setIcon( new ImageIcon( "bug.png" ) );
pictureJLabel.setBounds( 54, 120, 500, 250 );
pictureJLabel.setHorizontalAlignment( JLabel.CENTER );
contentPane.add( pictureJLabel ); // set properties of application's window
setTitle( "Welcome" ); // set JFrame's title bar string
setSize( 608, 413 ); // set width and height of JFrame
setVisible( true ); // display JFrame on screen } // end method createUserInterface // main method
public static void main( String[] args )
{
Welcome application = new Welcome();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main} // end class Welcome
代码是书上的例子应该是没问题的,
1L的方法试过了还是同样的错误
在项目名上右键 -> Builder Path -> Configure Build Path -> 选择Source面板 再点Add Folder, 把源代码所在的包路径的上层目录加进来,而且如果你是把两个类写在一个文件里的话,你在右键选择Run As JavaAppication 的时候,要把光标至于包括main方法的类上
顶你一下!!!!!
哈哈
只要勾选Use project folder as root for sources and class files就行了