请问我编写的这段代码,在按钮事件发生后for循环事发生了(for循环的目的是延时),但是在运行的时候为只有一次延时的效果?不能让里面的dy1一点一点的加。
package 贪吃蛇;import java.awt.BorderLayout;
import java.awt.Color;
import java.
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import javax.swing.*;
//import java.awt.Graphics;
public class fuben extends JFrame {
private int dx1=0, dy1=0;
//private static Thread t;
private JPanel jp1=new JPanel();
private JPanel jp2=new JPanel();
private JPanel jp0=new JPanel();
private Image img;
public static void main(String[] args) {
// TODO Auto-generated method stub
new fuben();
}
public fuben(){
setBounds(10,10,800,650);
setContentPane(getJPanel1());
URL url=tanchishe.class.getResource("./1.gif");//获取图片的URl
img = Toolkit.getDefaultToolkit().getImage(url);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private JButton getJButton2(){
JButton jb2=new JButton("下");
jb2.setSize(100, 20);
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
while(dy1<=500){
for(long x=0;x<5000;x++){
for(long y=0;y<1000;y++){
//for(long h=0;h<5000;h++){}
}
}
dy1 +=5;
DrawPanel DP=new DrawPanel();
DP.repaint();
// if(dy1>500){
// dy1=0;
// }
}
}
});
return jb2;
}
private JPanel getJPanel1(){
jp0.setLayout(new BorderLayout());
jp0.add(getJPanel2(),BorderLayout.SOUTH);
jp0.add(new DrawPanel());
return jp0;
}
private JPanel getJPanel2(){
//jp1.setLayout(new BorderLayout());
jp2.add(getJButton1(),new GridBagConstraints());
jp2.add(getJButton2(),new GridBagConstraints());
jp2.add(getJButton3(),new GridBagConstraints());
jp2.add(getJButton4(),new GridBagConstraints());
//jp2.add(jb5);
return jp2;
}
class DrawPanel extends JPanel{
public void paint(Graphics g){
super.paint(g);
//g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, this);
g.drawImage(img, dx1, dy1, 30, 30, this);
}
}
}