java - 如何更新存储图像的 JLabel

标签 java forms image swing

我正在尝试通过 JButton 下载某些图片。

表单启动,有一个 JTextBox,我在其中写入 ID,因此按钮应该获取照片,但我真的不知道如何更新用于存储和显示图像的 JLabel。

我设置了默认 URL,因此启动时会始终显示相同的图片。

使用 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);
    }
}

最佳答案

这样做,就像你在初始化时所做的那样...... 在你的actionListener中试试这个:

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

 }

关于java - 如何更新存储图像的 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39909279/

相关文章:

java - 前台通知上的动态图像资源无法正常工作

java - Selenium : Unable to inspect element on datepicker from chrome/firefox

java - 无效操作 : result set is closed. ERRORCODE=-4470,SQLSTATE=null

javascript - 基于php ajax的表单显示空白页

jQuery 验证和表单插件

java - 哪些对象在 Java Card 中持久存在,何时存在?

php - 如何限制我的 Php 列表显示的行数

java - 如何将Android ImageButton的位图更改为R.Layout中存储的另一个位图

image - 无论如何,是否有图像作为 microsoft glee 中的节点

html - alt-tag 中的标题 (alt ='<h1>Test</h1>' )