JAVA 编程 在一个窗体中加入一个按钮 和一个图片,监听这个按钮,当点击时 让图片移动。

求指导。。求代码。非常感谢。。。
2025-02-24 18:03:45
推荐回答(3个)
回答1:

图片以label为载体,给按钮注册actionlistener,在事件中重设label的位置就好了

回答2:

就像楼上说的,把图片放到label中,然后设置label的位置
private int x = jLabel1.getX();
private int y = jLabel1.getY();
jmove.setLocation(x+10, y);

回答3:

private int x=0,y=0;
private final Random random = new Random();

public void paintComponent(Graphics g){
g.drawImage(x,y, image, null);

}

button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
x = x + random.nextInt(10);
y = y + random.nextInt(10);
update();
}
}