Jframe JLabel,仅当我调整窗口大小时才刷新图像

时间:2014-01-02 20:05:23

标签: java swing jframe

我正在尝试使用缓冲区将其显示在JLabel中并在JFrame中使用它。更改图像时出现问题:只有在调整窗口大小时图像才会发生变化。

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;

}

1 个答案:

答案 0 :(得分:6)

要更新JFrame,您必须使用

revalidate(); 
repaint();

因此,在更改GUI中的任何内容之后,您需要重新验证()JFrame,然后重新绘制它的内容。

祝你好运!