| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1266 人关注过本帖
标题:请问我编写的这段代码,在按钮事件发生后for循环事发生了(for循环的目的是 ...
只看楼主 加入收藏
大阜阳
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-4-16
收藏
 问题点数:0 回复次数:0 
请问我编写的这段代码,在按钮事件发生后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);
               
               
   }
        
   
  }
}
搜索更多相关主题的帖子: private package 贪吃蛇 public import 
2017-04-16 17:54
快速回复:请问我编写的这段代码,在按钮事件发生后for循环事发生了(for循环的目 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020660 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved