当前位置:编程学习 > JAVA >>

关于JPanel

如何在JPanel上弹出JDialog?跪求代码参考 --------------------编程问答--------------------

import java.awt.ImageCapabilities;
import java.util.Random;
 
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
 
/*
 * 产生移动的距离,移动
 * 记录距离的文本修改由线程完成
 * 到达一个数值之后,需要弹出对话框
 * */
public class Horse extends JLabel implements Runnable{
    private JTextField scores;
    private int index;
    public Horse(){}
    public Horse(JTextField scores,int index){
        this.scores=scores;
        this.index=index;
    }
    public void run(){
        //与对象
        int s=0;
        while(!RunHorse.state){
            int len =(int)(Math.random()*10);
            s+=len;
            this.setLocation(s,this.getY());
            //scro
            if(s>=500){
                RunHorse.state=true;
                 
                JOptionPane.showMessageDialog(null, "Congratulations!","RunHorse",JOptionPane.INFORMATION_MESSAGE,
                new ImageIcon(Horse.class.getResource((index+1)+"jpg")));
            }
            try {
                Thread.currentThread().sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
        }
    }
}

以后建议这种问题不要发在非技术区
补充:Java ,  非技术区
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,