Image image = GenerateImage.toImage(true); //this generates an image file
JLabel thumb = new JLabel();
thumb.setIcon(image)
risposta
È necessario fornire a JLabel un'implementazione Icon
(ad esempio ImageIcon
). Si può fare esso attraverso il metodo setIcon
, come nella tua domanda, o tramite il costruttore JLabel
:
Image image=GenerateImage.toImage(true); //this generates an image file
ImageIcon icon = new ImageIcon(image);
JLabel thumb = new JLabel();
thumb.setIcon(icon);
vi consiglio di leggere il Javadoc per JLabel
, Icon
e ImageIcon
. Inoltre, è possibile controllare lo How to Use Labels Tutorial, per ulteriori informazioni.
per ottenere un'immagine da un URL possiamo utilizzare il seguente codice:
ImageIcon imgThisImg = new ImageIcon(PicURL));
jLabel2.setIcon(imgThisImg);
Funziona tutto per me. PicUrl è una variabile stringa che strorizza l'URL dell'immagine.
(Se si utilizza NetBeans IDE) Basta creare una cartella nel progetto ma fuori dalla cartella src. Chiamato la cartella Immagini. E poi metti l'immagine nella cartella Immagini e scrivi il codice qui sotto.
// Import ImageIcon
ImageIcon iconLogo = new ImageIcon("Images/YourCompanyLogo.png");
// In init() method write this code
jLabelYourCompanyLogo.setIcon(iconLogo);
Ora esegui il tuo programma.
semplice codice che si può scrivere in main (String [] args) funzione
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
il codice più breve è:
JLabel jLabelObject = new JLabel();
jLabelObject.setIcon(new ImageIcon(stringPictureURL));
stringPictureURL è PATH di Immagine .
- 1. Come aggiungere JLabel a JEditorPane?
- 2. Java aggiungendo ImageIcon a JLabel
- 3. Come aggiungere un comportamento di selezione a JLabel
- 4. Come aggiungere una nuova riga a JLabel senza utilizzare l'HTML
- 5. Rotate a Swing JLabel
- 6. Aggiungere e rimuovere un'icona su una JLabel
- 7. Come aggiungere una nuova riga in una JLabel con variabili
- 8. Come disegnare su una JLabel?
- 9. Come aggiungere un ImageIcon a un JFrame?
- 10. Come aggiungere mnemonici in java swing?
- 11. Non è possibile aggiungere JTextField a JFrame
- 12. JLabel cambiamento di colore
- 13. Come aggiungere a DataOutputStream in Java?
- 14. Java: come aggiungere secondi a Timestamp?
- 15. Come posso inserire html in una JLabel in java?
- 16. NetBeans: come aggiungere ScrollBar a JPanel
- 17. Visualizza BMP in JLabel
- 18. Allineare il testo in JLabel a destra
- 19. Sovrapponi un JButton su JLabel in Java Swing?
- 20. Come riassumere testo in JLabel?
- 21. Come cambiare la luminosità dell'immagine in JLabel?
- 22. Come modificare dinamicamente una JLabel
- 23. Sottolineato JLabel
- 24. Collegamenti ipertestuali in JLabel
- 25. Due icone in una JLabel?
- 26. Rendere dinamico il testo in una JLabel
- 27. JLabel Java, interrompi il testo alla riga successiva?
- 28. Modifica carattere JLabel predefinito
- 29. Testo allineato in JLabel
- 30. JLabel get Image