简体   繁体   中英

Jframe JLabel, image refresh only if I resize the window

I was trying to do a buffer to display it in a JLabel and use it in a JFrame . I have a problem when I change the image: the image changes only when I resize the window.

package buffer;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import javax.swing.JLabel;



public class principal extends javax.swing.JFrame {


private JLabel person=new JLabel();
public static final int tCuadrito=60;
private URL url=Buffer.class.getClass().getResource("/imagenes/persona.png");
private ImageIcon imagenIcon=new ImageIcon();
private BufferedImage gato=new BufferedImage(120,60,BufferedImage.BITMASK);

public principal() {
    initComponents();
    dibujar();
}

public void dibujar(){
    try{

    gato=ImageIO.read(url);
    }catch(IOException e){
        System.out.print("ERROR imagen no leida: "+e.toString());
    }

    imagenIcon.setImage(gato.getSubimage(0, 0, 60, 60));



    person.setBounds(0*tCuadrito, 0*tCuadrito, tCuadrito, tCuadrito);
    person.setVisible(true);
    person.setIcon(imagenIcon);
    contenedor.add(person);

}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    contenedor = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            formKeyReleased(evt);
        }
    });

    javax.swing.GroupLayout contenedorLayout = new javax.swing.GroupLayout(contenedor);
    contenedor.setLayout(contenedorLayout);
    contenedorLayout.setHorizontalGroup(
        contenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    contenedorLayout.setVerticalGroup(
        contenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(contenedor, javax.swing.GroupLayout.DEFAULT_SIZE,                           javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(contenedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                                                      

private void formKeyReleased(java.awt.event.KeyEvent evt) {                                 
    switch(evt.getKeyCode()){
        case KeyEvent.VK_RIGHT:

            imagen=cat.getSubimage(60, 0, 60, 60);
            imagenIcon.setImage(imagen);
            person.setIcon(imagenIcon);

            break;

    }
}                                


public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info :    javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
          java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new principal().setVisible(true);
        }
    });
}

private javax.swing.JPanel contenedor;

}

To update a JFrame you must use

revalidate(); 
repaint();

So after you change anything in your GUI you would want to revalidate() the JFrame and then repaint the contents of it.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM