2016-10-07 2 views
0

JButton을 통해 특정 사진을 다운로드하려고합니다.이미지를 저장하는 JLabel을 업데이트하는 방법

양식이 시작되면 ID를 쓰는 JTextBox가 있습니다. 버튼이 사진을 가져와야하지만 실제로 이미지를 저장하고 표시하는 데 사용하는 JLabel을 업데이트하는 방법을 알지 못합니다.

기본 URL을 입력 했으므로 시작할 때 항상 동일한 그림을 표시하기 위해 goint가 시작됩니다.

JLabel을 사용하면 이미지를 저장하고 표시하는 가장 좋은 방법일까요?

사진의 크기를 조정할 수 있습니까?

감사


package fotoMatricula; 

import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

public class FotoMatricula { 

private JFrame frame; 
private JTextField textField; 
private String foto; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       FotoMatricula window = new FotoMatricula(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public FotoMatricula() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
public void initialize() { 
    Image image = null; 
    try { 
     URL url = new URL("https://charal.unacar.mx/fotos/103522.jpg"); 
     image = ImageIO.read(url); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    frame = new JFrame(); 
    frame.setBounds(100, 100, 391, 396); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JLabel lblIntroduceTuMatrcula = new JLabel("Introduce tu matr\u00EDcula"); 
    lblIntroduceTuMatrcula.setFont(new Font("Tahoma", Font.PLAIN, 17)); 
    lblIntroduceTuMatrcula.setBounds(104, 11, 174, 24); 
    frame.getContentPane().add(lblIntroduceTuMatrcula); 

    textField = new JTextField(); 
    textField.setBounds(139, 46, 94, 20); 
    frame.getContentPane().add(textField); 
    textField.setColumns(10); 

    JLabel lblNewLabel = new JLabel(new ImageIcon(image)); 
    lblNewLabel.setBounds(89, 77, 202, 189); 
    frame.getContentPane().add(lblNewLabel); 


    JButton btnBuscar = new JButton("Buscar"); 
    btnBuscar.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      foto = textField.getText(); 
      lblNewLabel.setText(foto); 
     } 
    }); 
    btnBuscar.setBounds(139, 303, 89, 23); 
    frame.getContentPane().add(btnBuscar); 
    } 
} 
+0

는 JLabel의의 아이콘을 변경하려면 라벨의'setIcon (...)'메소드. – camickr

+0

사실 10 분 전에 버튼을 클릭하면 기본 이미지가 사라지고 새로운 이미지가로드되지 않습니다. ( URL이 정확한지 확인하기 위해 System.out.println을 넣었습니다. 그리고 그것은 –

답변

1

이 그것을 할 당신이 저점 초기화 그랬던 것처럼 ... 당신의 actionListener이 시도 : 당신이 사용할 수

try 
{ 
    URL url2 = new URL (textField.getText()); 
    lblNewLabel.setIcon(new ImageIcon(ImageIO.read(url2))); 
} 
catch (Exception e) 
{ 

} 
+0

10 분 전처럼 실제로 해결했습니다! 답장을 보내 주셔서 감사합니다. 이미지 크기를 조정하려면 이미지를 새 이미지 아이 콘 (imagen.getScaledInstance (324, 455, imagen.SCALE_SMOOTH))) ; –

+0

@ David : 문제 없습니다! 이 답변을 수락하는 것을 고려해보십시오. 문제가 해결 되었기 때문에 (스스로 해결했다고해도). 그것은 다른 사람들을 도울지도 모른다. .. – Tim

관련 문제