2010-09-23 13 views
20
Image image = GenerateImage.toImage(true); //this generates an image file 
JLabel thumb = new JLabel(); 
thumb.setIcon(image) 

उत्तर

26

आपको जेएलएबल को Icon कार्यान्वयन (यानी ImageIcon) की आपूर्ति करनी है। आप इसे setIcon विधि गर्त उद्यम, अपने प्रश्न के रूप में, या JLabel निर्माता के माध्यम से कर सकते हैं:

Image image=GenerateImage.toImage(true); //this generates an image file 
ImageIcon icon = new ImageIcon(image); 
JLabel thumb = new JLabel(); 
thumb.setIcon(icon); 

मैं JLabel, Icon, और ImageIcon के लिए जावाडोक को पढ़ने के लिए सलाह देते हैं। इसके अलावा, आप अधिक जानकारी के लिए How to Use Labels Tutorial देख सकते हैं।

23

एक यूआरएल हम निम्नलिखित कोड का उपयोग कर सकते से एक छवि प्राप्त करने के लिए:

ImageIcon imgThisImg = new ImageIcon(PicURL)); 

jLabel2.setIcon(imgThisImg); 

यह पूरी तरह से मेरे लिए काम करता है। PicUrl एक स्ट्रिंग वेरिएबल है जो तस्वीर के यूआरएल को रोकता है।

11

(यदि आप नेटबीन आईडीई का उपयोग कर रहे हैं) बस अपनी प्रोजेक्ट में एक फ़ोल्डर बनाएं लेकिन src फ़ोल्डर के बाहर। फ़ोल्डर छवियों नामित। और उसके बाद छवि को छवियों फ़ोल्डर में रखें और नीचे कोड लिखें।

// Import ImageIcon  
ImageIcon iconLogo = new ImageIcon("Images/YourCompanyLogo.png"); 
// In init() method write this code 
jLabelYourCompanyLogo.setIcon(iconLogo); 

अब अपना प्रोग्राम चलाएं।

1

सरल कोड है कि आप (String [] args) में लिख सकते हैं समारोह

JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//application will be closed when you close frame 
    frame.setSize(800,600); 
    frame.setLocation(200,200); 

    JFileChooser fc = new JFileChooser(); 
    if(fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){ 
     BufferedImage img = ImageIO.read(fc.getSelectedFile());//it must be an image file, otherwise you'll get an exception 
     JLabel label = new JLabel(); 
     label.setIcon(new ImageIcon(img)); 
     frame.getContentPane().add(label); 
    } 

    frame.setVisible(true);//showing up the frame 
3

कम से कम कोड है:

JLabel jLabelObject = new JLabel(); 
jLabelObject.setIcon(new ImageIcon(stringPictureURL)); 

stringPictureURLपथ की है छवि

संबंधित मुद्दे