Java怎么写一个倒计时程序 从10秒开始 到7秒的时候输出一句话 然后继续倒数到1停下输出一句话

Java怎么写一个倒计时程序 从10秒开始 到7秒的时候输出一句话 然后继续倒数到1停下输出一句话,第1张

public class Timer implements Runnable {

int i=10

public static void main(String[] args) {

Timer t=new Timer()

Thread th=new Thread(t)

th.start()

}

@Override

public void run() {

try {

while(true){

i--

Thread.sleep(1000)

System.out.println(i)

if(i==7){

System.out.println("还缺碧桥剩7秒爆炸")

}

if(i==1){

System.out.println("差点就挂了伏猛慧汪")

return

}

}

} catch (Exception e) {

e.printStackTrace()

}

}

}

应该是这样

import java.awt.Color

import java.util.*

import java.awt.*

import java.applet.*

public class Clock extends Applet implements Runnable

{

Thread timer=null

Label label

int lastxs=50,lastys=30,lastxm=50,lastym=30,lastxh=50,lastyh=30

public void init()

{

label=new Label(" ")

setBackground(Color.white)

add(label)

}

public void paint(Graphics g)

{

int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter

Date rightnow=new Date()

String today=rightnow.toLocaleString()

label.setText(today)

s=rightnow.getSeconds()

m=rightnow.getMinutes()

h=rightnow.getHours()

xcenter=100

ycenter=80

xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45+xcenter)

ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45+ycenter)

xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*45+xcenter)

ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*45+ycenter)

xh=(int)(Math.cos((h*30+m*2)*3.14f/180-3.14f/2)*30+xcenter)

yh=(int)(Math.sin((h*30+m*2)*3.14f/180-3.14f/2)*30+ycenter)

g.setFont(new Font("悉销TimesToman",Font.PLAIN,14))

g.setColor(Color.orange)

g.fill3DRect(xcenter-50,ycenter-50,100,100,true)

g.setColor(Color.darkGray)

g.drawString("睁核游12"氏销,xcenter-5,ycenter-37)

g.drawString("3",xcenter+40,ycenter+3)

g.drawString("6",xcenter-3,ycenter+45)

g.drawString("9",xcenter-45,ycenter+3)

g.setColor(Color.orange)

if(xs!=lastxs||ys!=lastys)

{

g.drawLine(xcenter,ycenter,lastxs,lastys)

}

if(xm!=lastxm||ym!=lastym)

{

g.drawLine(xcenter,ycenter-1,lastxm,lastym)

g.drawLine(xcenter-1,ycenter,lastxm,lastym)

}

if(xh!=lastxh||yh!=lastyh)

{

g.drawLine(xcenter,ycenter-1,lastxh,lastyh)

g.drawLine(xcenter-1,ycenter,lastxh,lastyh)

}

g.setColor(Color.red)

g.drawLine(xcenter,ycenter,xs,ys)

g.drawLine(xcenter,ycenter-1,xm,ym)

g.drawLine(xcenter-1,ycenter,xm,ym)

g.drawLine(xcenter,ycenter-1,xh,yh)

g.drawLine(xcenter-1,ycenter,xh,yh)

lastxs=xs

lastys=ys

lastxm=xm

lastym=ym

lastxh=xh

lastyh=yh

}

public void start()

{

if(timer==null)

{

timer=new Thread(this)

timer.start()

}

}

public void stop()

{

timer=null

}

public void run()

{

while(timer!=null)

{

try

{

Thread.sleep(1000)

}catch(InterruptedException ie){}

repaint()

}

timer=null

}

public void update(Graphics g)

{

paint(g)

}

}

import javax.swing.*

public class Test extends JFrame

{

JButton button

JPanel panel

Test1DaoJiShi2()

{

super("10秒友孙计数器")

button=new JButton()

panel=new JPanel()

add(panel)

panel.add(button)

setSize(300,200)

setVisible(true)

ButtonAction tt=new ButtonAction()

tt.run()

}

class ButtonAction extends Thread

{

int i=10

public void run() //线程在好衫链此开始执行

{

while(i!=0)

{

try

{

sleep(1000)

button.setText( "还有塌拆 " +i+ "秒 ")

i--

}

catch(Exception eee)

{}

}

if(i==0)

{

JOptionPane.showMessageDialog(null, " 时间到!")

}

}

}

public static void main(String args[])

{

JFrame frm = new Test()

frm.dispose()

System.exit(0)

}

}


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

原文地址: https://outofmemory.cn/yw/12391441.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存