Java adding ImageIcon to JLabel

后端 未结 3 1538
悲&欢浪女
悲&欢浪女 2021-02-15 14:14

I am trying to make a very basic game with Java and I am having trouble displaying an image on a JFrame. It has worked in the past for me and now is not, i can\'t s

相关标签:
3条回答
  • 2021-02-15 14:45

    Your problem lies here:

       ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
       JLabel imagelabel = new JLabel(character);
    

    You create an ImageIcon "image" but you create your JLabel with "character".

    It should be:

    JLabel imagelabel = new JLabel(image);
    
    0 讨论(0)
  • 2021-02-15 14:48
    import javax.awt.*; 
    import java.awt.*; 
    import java.awt.event*; 
    
    //class name image 
    class image { 
        image() 
        //constructor { 
            Frame f=new Frame("Image"); 
            //Frame
            f.setSize(500,500); 
            f.setVisible(true); 
            Panel p =new Panel(); 
            //Panel 
            f.add(p); 
            p.addLayout(null); 
            ImageIcon ii=new ImageIcon("set your image path"); 
            //ImageIcon is used to image Display . 
            Label l =new Label(ii); 
            p.add(ii); 
            p.setBounds(set you bounds); 
            //Like that(20,20,500,40); 
        } 
    
        public static void main(String [] args) { 
            image obj = new 
        } 
    }
    
    0 讨论(0)
  • 2021-02-15 15:05

    Try,

    ImageIcon image = new ImageIcon("c:\\path\\image.png");
    imagelabel = new JLabel(character, image, JLabel.CENTER);
    frame.add(imagelabel);
    

    Take a look at Tutorial - How to use Icons

    0 讨论(0)
提交回复
热议问题