Jframe JLabel,仅当我调整窗口大小时才刷新图像 [英] Jframe JLabel, image refresh only if I resize the window
本文介绍了Jframe JLabel,仅当我调整窗口大小时才刷新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做一个缓冲区,将其显示在JLabel
中,并在JFrame
中使用它.更改图像时出现问题:仅在调整窗口大小时,图像才会更改.
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;
}
推荐答案
要更新JFrame,您必须使用
To update a JFrame you must use
revalidate();
repaint();
因此,在GUI中进行任何更改后,您都需要revalidate()JFrame,然后重新绘制其内容.
So after you change anything in your GUI you would want to revalidate() the JFrame and then repaint the contents of it.
祝你好运!
这篇关于Jframe JLabel,仅当我调整窗口大小时才刷新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文