用Java程序Jframe和Jpanel实现抽奖(例1-40数字滚动),鼠标左键点击实现开始和停止?

用Java程序Jframe和Jpanel实现抽奖(例1-40数字滚动),鼠标左键点击实现开始和停止?,第1张

public class MouseEventDemo ... implements MouseListener {

        //where initialization occurs:

        //Register for mouse events on blankArea and the panel.

        blankArea.addMouseListener(this)

        addMouseListener(this)

    ...

    public void mousePressed(MouseEvent e) {

       saySomething("Mouse pressed # of clicks: "

                    + e.getClickCount(), e)

    }

    public void mouseReleased(MouseEvent e) {

       saySomething("Mouse released # of clicks: "

                    + e.getClickCount(), e)

    }

    public void mouseEntered(MouseEvent e) {

       saySomething("Mouse entered", e)

    }

    public void mouseExited(MouseEvent e) {

       saySomething("Mouse exited", e)

    }

   铅纤 public void mouseClicked(MouseEvent e) {

       saySomething("Mouse clicked (# of clicks: "

               早激帆     + e.getClickCount() + ")", e)

    }

    void saySomething(String eventDescription, MouseEvent e) {

        textArea.append(eventDescription + " detected on "

           陆雹             + e.getComponent().getClass().getName()

                        + "." + newline)

    }

}

一定要用线程,首先,我们先来分析一下如何才能做到让屏幕上的数字不停的变化,这就需要使用线程了,当我槐模行们开始抽奖的按钮的时候,线程启动,此时屏幕铅哗上的数字开始滚动。当我们点击结束按钮的时候,线程终码段止,此时就可以显示出抽奖结果了。

import java.util.Scanner

/**

 *

 */

public class f {

    public static void main(String args[]){

        Scanner scan = new Scanner(System.in)

        System.out.print("请输入抽奖号码上限:")

      渣卖蔽  int max = scan.nextInt()

        System.out.print("请输入抽奖次数:"配碧)

        int n = scan.nextInt()

        System.out.print("中奖号码依次为:")

        for(int i=0i<ni++){

            System.out.print((int)(Math.random()*max+1)+" ")

      如州  }

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12566264.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-26
下一篇2025-08-26

发表评论

登录后才能评论

评论列表(0条)

    保存