2013年12月16日星期一

java in ImageIO usage and display pictures

in jar package I used to write a java program can run normally , but the picture after the program generates jar packets are not displayed. After using the Internet to find ImageIO to load pictures, but the program has been commissioning wrong, seeking guidance about the great God to help . Below is a picture of that part of the code . Error shows Type mismatch: cannot convert from BufferedImage to ImageIcon.
JLabel labellogo = new JLabel();
ImageIcon imagelogo = new ImageIcon();

try {
 
imagelogo=ImageIO.read(getClass().getClassLoader().getResource("./images/Logo.jpg"));

} catch (Exception e) {
  // TODO Auto-generated catch block
 e.printStackTrace();
      }

 labellogo.setIcon(imagelogo);
                 dialogPanel.add(labellogo,BorderLayout.EAST);

------ Solution ------------------------------------- -------
ImageIcon imagelogo = new ImageIcon();

change to
BufferedImage imagelogo = null;

------ Solution ------------------------------------- -------
Do naughty , quick problem solving            Upstairs is a positive solution        but it does not appear to run the entire form  

package test;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test {
public Test() {
JFrame frame = new JFrame();
JLabel labellogo = new JLabel();
URL url = Test.class.getResource("images/1.PNG");;
System.out.println(url);
ImageIcon icon = new ImageIcon(url);
System.out.println(icon);
labellogo.setIcon(icon);
frame.add(labellogo, BorderLayout.EAST);
frame.setSize(300, 300);
frame.setVisible(true);
}

public static void main(String[] args) {
Test t = new Test();
}
}

you refer to
------ For reference only -------------------------------- -------
If this is changed, under labellogo.setIcon (imagelogo); sentence there a mistake , there are red lines under setIcon , said the method setIcon (Icon) in the ; type JLabel is not applicable for the arguments (BufferedImage). Great God continue to seek help
------ For reference only ---------------------------------- -----
label.setIcon(new ImageIcon(imagelogo));

------ For reference only --------------------------- ------------

------ For reference only ---------------------------------- -----
this statement does not change is wrong , but do not pop the whole appeared. If you comment out the sentence , popups to appear , is the picture no .
------ For reference only -------------------------------------- -
not naughty , quick problem solving < br> ------ For reference only ---------------------------------------
not naughty , quick problem solving   Upstairs is a positive solution
------ For reference only ----------------------------- ----------
Do not naughty , quick problem solving        Upstairs is a positive solution   but it does not run the entire form appeared
------ For reference only ----------------------- ----------------
getClass().getClassLoader().getResource("./images/Logo.jpg")

read:
getClass().getClassLoader().getResource("Logo.jpg")

The Logo.jpg copy to class with you in a package path inside
------ For reference only ------------------- --------------------
Thank you for the code, but this code is running out to no avail , ah, after all notes line by line debugging , line 19 and before 23 lines can occur after a form, the form from the start line 20 does not appear. This is why
------ For reference only ---------------------------------- -----
Thank you for the code, but this code is running out to no avail , ah, after all notes line by line debugging , line 19 and line 23 after the previous one form can emerge from the start line 20 Forms will not appear. This is why   may be your Road King made ​​a mistake, or is not case sensitive . I tested this procedure , there is no problem
------ For reference only ---------------------------- -----------
thank you very much , problem solving , and worship it. I just started learning java, not sure what principle , I go back to study. Once again, big thanks to the help of God .
------ For reference only -------------------------------------- -
Thank you for the code, but this code is running out to no avail , ah, after all notes line by line debugging , after line 19 and line 23 can appear before a form, the form from the start line 20 is not appeared. This is why        may be your Road King made ​​a mistake, or is not case sensitive . I tested this procedure , there is no problem   understand, you are putting images into the bag , so you can run it. Thank you very much for your help, my problem solved.
------ For reference only -------------------------------------- -
Thank you for the code, but this code is running out to no avail , ah, after all notes line by line debugging , after line 19 and line 23 can appear before a form, the form from the start line 20 is not appeared. This is why            may be your Road King made ​​a mistake, or is not case sensitive . I tested this procedure , there is no problem        understand, you are putting images into the bag , so you can run it. Thank you very much for your help, my problem solved.   very smart thing , to give me the

没有评论:

发表评论