无法获取要显示的图标图像

时间:2014-03-26 23:28:50

标签: java image swing icons jlabel

我已经尝试了几天才能将这张名为star1.png的图像显示出来。我试图从我的棍子男人“射击”它,但我甚至无法让图像显示出来。任何帮助将不胜感激。感谢

    package my.testing1;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    /**
    *
    *  @author Jamie
    */
   public class GUI extends javax.swing.JFrame 
   {

   JLabel label = new JLabel(new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png"));
   JLabel cords = new JLabel();
   JLabel bg = new JLabel(new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\background.png"));
   JLabel star = new JLabel(new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\star1.png"));

   int locx = 0; 
   int locy = 0;
 int i = 0;
 int x = label.getX();
 int y = label.getY();
 String cor= Integer.toString(x) +" "+ Integer.toString(y);
 boolean ph = true;
 boolean left = true;
 boolean Sph = true;

 public GUI()
     {   
     this.setSize(800,600);
     this.cords.setText(cor);
     label.setBounds(x,y,120,120);
     cords.setBounds(20,170,50,50);
     bg.setBounds(0,0,800,600);
     //star.setBounds(x,y,50,50);


     cor= Integer.toString(x) + " "+ Integer.toString(y);
     label.addKeyListener(new KeyAdapter()

         {

         @Override
         public void keyPressed(KeyEvent ke)
          {
             if (ke.getKeyCode() == KeyEvent.VK_S)
             {
                 //locx=label.getX();
                 //locy=label.getY();
                 ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\star1.png");
                 star.setIcon(newIcon);

                 star.setLocation(300,300);


             }
             if(ke.getKeyCode() == KeyEvent.VK_DOWN)
             {
              if (y>0 | y==0)
                 {
                     y=y+10;
                 }
               if (y>440)
                 {
                     y=y-10;
                 }
                 label.setLocation(x,y);
                 if(ph == false)
                 {
                    if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }

                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                 repaint();
                 cords.setLocation(20,170);
                 cor= Integer.toString(x) + " "+Integer.toString(y);
                 cords.setText(cor);
             }
             if(ke.getKeyCode() == KeyEvent.VK_UP)
             {
                if (y>440 | y>0)
                 {
                     y=y-10;
                 }
                if (y<0)
                {
                    y=y+10;
                }
                 label.setLocation(x,y);
                 if(ph == false)
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }
                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                repaint();
                cords.setLocation(20,170);
                cor= Integer.toString(x) +" "+ Integer.toString(y);
                cords.setText(cor);
             }
             if(ke.getKeyCode() == KeyEvent.VK_LEFT)
             {
                 if (x<0)
                 {
                     x=x+10;
                 }
                 if (x>0)
                 {
                     x=x-10;
                 }
                 label.setLocation(x,y);
                 left=true;
                 if(ph == false)
                 {

                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }
                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                 repaint();
                 cords.setLocation(20,170);
                 cor= Integer.toString(x) + " "+Integer.toString(y);
                 cords.setText(cor);
             }

             if(ke.getKeyCode() == KeyEvent.VK_RIGHT)
             {
                 if (x<690)
                 {
                     x=x+10;
                 }
                 if (x>690)
                 {
                     x=x-10;
                 }
                 label.setLocation(x,y);
                 left=false;
                 if(ph == false)
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }
                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                 repaint();
                 cords.setLocation(20,170);
                 cor= Integer.toString(x) + " "+Integer.toString(y);
                 cords.setText(cor);
             }
             //System.out.println(x+y);
         }
     });
     label.addMouseListener(new MouseAdapter()
         {
         //@Override
         public void mouseClicked(MouseEvent me)
             {
             if(me.getClickCount() == 1)
                 {
                 boolean dd = label.isOptimizedDrawingEnabled();
                 boolean ff = label.requestFocusInWindow();
                 repaint();
             }
         }

     });

     JPanel p = new JPanel();

     p.setSize(700,700);
     this.setDefaultCloseOperation(GUI.EXIT_ON_CLOSE );
     p.add(cords);
     p.setLayout(null);
     p.add(label);
     p.add(bg);
     p.add(star);
     JScrollPane js = new JScrollPane(p);
     getContentPane().add(js);
 }

/**
 * Creates new form GUI
 */
/*public GUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setAlwaysOnTop(true);
    setBackground(new java.awt.Color(0, 102, 0));
    setBounds(new java.awt.Rectangle(10, 10, 700, 700));
    setPreferredSize(new java.awt.Dimension(700, 700));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 604, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 376, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
   java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
    GUI frame = new GUI();        
    new GUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
// End of variables declaration                   
}

2 个答案:

答案 0 :(得分:1)

按下某个键时未触发KeyAdapter。 只需更换:

label.addKeyListener(new KeyAdapter()

使用:

addKeyListener(new KeyAdapter()
GUI()构造函数中

,以便框架处理KeyEvent而不是JLabel

答案 1 :(得分:1)

此后你会自杀,但这是你的问题

 p.add(label);
 p.add(bg);
 p.add(star);
你知道吗?你把火柴人放在上面,然后把背景放在下面,然后在底部加星,所以无论它在哪里,它都隐藏在背景图像后面。只需替换为

 p.add(label);
 p.add(star);
 p.add(bg);    

(或者如果您希望它们在共享位置时显示在其上方,则在标签前添加星号。)

顺便说一下,你可以写很多东西,而不是与你的问题有关。如果你想我可以发布它们。