我想为图像实现 DragAndDrop,但似乎无法让 Swing 重绘功能在特定图像上工作。
代码:
public class playerFrame extends JFrame{
...
private void destroyerImageMouseDragged(java.awt.event.MouseEvent evt)
}
repaintCurrentPosition(evt);
}
public void repaintCurrentPosition(MouseEvent e){
this.setLocation(e.getX(), e.getY());
this.repaint();
}
this.repaint <- 此函数重绘整个帧,而不仅仅是我希望它重绘的图像,其大小约为 50x50。 如何在不创建新类的情况下重新绘制特定的 JPEG 图像?
谢谢。
最佳答案
this.repaint
将强制重绘父框架。仅在包含图像的控件上调用 repaint
。
示例:刷新加载到 JLabel 上的图像:
ImageIcon icon = createImageIcon("images/middle.gif");
label = new JLabel("Image and Text", icon, JLabel.CENTER);
你这样做:
label.repaint();
关于Java Swing 重绘图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8593925/