资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java倒计时十秒代码 java倒计时

用java编写一个倒计时器代码。

import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 201306211111L; private JTextField screen = new JTextField("0"); private JButton start = new JButton("开始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("计时器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("计时器"); this.timeBetween = 100; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { panel.setLayout(new GridLayout()); panel.add(start); panel.add(reset); start.addActionListener(this); reset.addActionListener(this); screen.setFont(new Font("幼圆", Font.BOLD, 60)); screen.setHorizontalAlignment(JTextField.CENTER); screen.setEditable(false); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.SOUTH); c.add(screen, BorderLayout.CENTER); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new TimerDemo(1);// 设定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("开始")) { start.setText("暂停"); isRunning = true; } else if (start.getText().equals("暂停")) { start.setText("开始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("开始"); screen.setText("0"); isRunning = false; time = 0; } new Thread(new TimeZone()).start(); } class TimeZone implements Runnable { @Override public void run() { while (isRunning) { time++; if (time = Integer.MAX_VALUE) { screen.setText("ERROR"); JOptionPane.showMessageDialog(null, "ERROR"); isRunning = false; } screen.setText(String.valueOf(time)); try { Thread.sleep(timeBetween); } catch (Exception e) { e.printStackTrace(); } } } }}

目前成都创新互联已为近1000家的企业提供了网站建设、域名、虚拟主机成都网站托管、企业网站设计、加查网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

怎么用java写个倒计时?

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);

}

}

怎样用JAVA写一个10钟倒计时程序?

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);

}

}


新闻名称:java倒计时十秒代码 java倒计时
转载注明:http://www.cdkjz.cn/article/ddcsdjj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220