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);
}
}
는 JLabel의의 아이콘을 변경하려면 라벨의'setIcon (...)'메소드. – camickr
사실 10 분 전에 버튼을 클릭하면 기본 이미지가 사라지고 새로운 이미지가로드되지 않습니다. ( URL이 정확한지 확인하기 위해 System.out.println을 넣었습니다. 그리고 그것은 –