如何在此代码中在JWindow上显示jpeg?

时间:2016-01-21 20:49:30

标签: java jwindow

我正在尝试在应用程序启动时显示带有欢迎图像的简介窗口。一切都加载得很好,但图像不显示。加载应用程序时,这就是我see

public class Window extends JWindow
{
  //java.net.URL imgIntro = getClass().getResource("/images/intro.jpg");
  ImageIcon imIntro = new ImageIcon("/images/intro.jpg");


  //java.net.URL imgRegles = getClass().getResource("/images/rules.jpg");
  ImageIcon imRules = new ImageIcon("/images/rules.jpg");

  static Thread t = new Thread();
  static int thread = 0;
  static JButton bouton;
  static int X;
  static int Y;



  public Window( int X, int Y, int type) {

    super();
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(X,Y);
    setLocation( (dim.width - X)/2 , (dim.height - Y)/2);
    setVisible(true);
    Container fen = getContentPane();

    if (type == 1 ) bouton = new JButton(imIntro);
    else            bouton = new JButton(imRules);
    bouton.setPreferredSize(new Dimension(X, Y) );
    fen.add( bouton);
    bouton.setVisible( true );

    show();

    if( type == 1 ) {
        try {
            Thread.sleep(1000);
            thread = 1;
        }
        catch( java.lang.InterruptedException ex ) {
            JOptionPane.showMessageDialog(null, "error");
            }
        dispose();

    }

    else {
        bouton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                     dispose();
                }
        });
    }

 }
}

1 个答案:

答案 0 :(得分:0)

代码丢失

getClass().getResource

然后

ImageIcon imIntro = new ImageIcon(getClass().getResource("/images/Lintro.jpg"));

完整示例Here