
正文
java停止倒计时代码 java倒计时程序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 窗口 倒计时 关闭
package mainWindow;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.IOException;
import java.awt.event.*;
import javax.swing.event.*;
/*程序文件:ShutDownWindow.java
*程序功能:程序java停止倒计时代码的定时关机窗体
*更新时间:2009.10.26
*/
public class ShutDownWindow extends JFrame implements ActionListener,DocumentListener{
/**
*
*/
private static final long serialVersionUID = 2084958924713854452L;
JTextField 时,分,秒;
JLabel 现在时间,设置时,设置分,设置秒;
JButton 关机,重启,注销,清空,取消;
Box 左,中,右,中部; //Box是使用 BoxLayout 对象作为其布局管理器的一个轻量级容器
JPanel 底部;
Container 容器;
int 标志,nh,nm,ns,wh,wm,ws;
ShutDownWindow(String s){
super(s);
setVisible(true);
setResizable(false); //设置窗体是否允许用户调整窗体大小
setSize(300,200);
Dimension 屏幕=Toolkit.getDefaultToolkit().getScreenSize(); //获得屏幕大小
setLocation(屏幕.width-300,0);
现在时间=new JLabel();
现在时间.setForeground(Color.RED);
现在时间.setText(nowTime());
时=new JTextField(4);
时.setForeground(Color.BLUE);
时.setText(null);
分=new JTextField(4);
分.setForeground(Color.BLUE);
分.setText(null);
秒=new JTextField(4);
秒.setForeground(Color.BLUE);
秒.setText(null);
设置时=new JLabel("设定小时");
设置分=new JLabel("设定分钟");
设置秒=new JLabel("设定秒钟");
关机=new JButton("关机");
重启=new JButton("重启");
注销=new JButton("注销");
取消=new JButton("取消");
清空=new JButton("清空");
关机.setEnabled(false);
重启.setEnabled(false);
取消.setEnabled(false);
注销.setEnabled(false);
清空.setEnabled(false);//按钮不可用
中部=Box.createHorizontalBox(); //创建一个从左到右显示其组件的 Box
validate();
容器=getContentPane();
容器.setLayout(new BorderLayout());
左=Box.createVerticalBox(); //创建一个从左到右显示其组件的 Box
中=Box.createVerticalBox();
右=Box.createVerticalBox();
左.add(设置时);
左.add(Box.createVerticalStrut(20)); //创建一个不可见的、固定高度的组件
左.add(设置分);
左.add(Box.createVerticalStrut(20));
左.add(设置秒);
中.add(时);
中.add(Box.createVerticalStrut(9));
中.add(分);
中.add(Box.createVerticalStrut(9));
中.add(秒);
右.add(关机);
右.add(Box.createVerticalStrut(10));
右.add(重启);
右.add(Box.createVerticalStrut(10));
右.add(注销);
validate(); //使用 validate 方法会使容器再次布置其子组件
中部.add(左);
中部.add(Box.createHorizontalStrut(15));
中部.add(中);
中部.add(Box.createHorizontalStrut(15));
中部.add(右);
中部.validate();
底部=new JPanel();
底部.setBackground(Color.GREEN);
底部.add(清空);
底部.add(取消);
底部.validate();
容器.add(现在时间,BorderLayout.NORTH);
容器.add(中部,BorderLayout.CENTER);
容器.add(底部,BorderLayout.SOUTH);
容器.validate();
容器.setBackground(Color.GRAY);
关机.addActionListener(this); //为各按钮标签添加监听对象
重启.addActionListener(this);
注销.addActionListener(this);
取消.addActionListener(this);
清空.addActionListener(this);
时.getDocument().addDocumentListener(this);
分.getDocument().addDocumentListener(this);
秒.getDocument().addDocumentListener(this);
//******************* 匿名线程,实现时间动态显示 *******************//
new Thread(new Runnable() {
public void run()
{
while(true) //为java停止倒计时代码了实时监听所以得用死循环
{
try
{
Thread.sleep(1000); //条件合适后线程休眠1秒钟
}
catch(Exception e)
{
e.printStackTrace(); //输入异常到指定地方
}
nowTime(); //调用nowTime()方法获得当前时间
}
}
}).start(); //调用start()后线程处于就绪状态
}
//******************** 得到当前系统时间并显示 *********************//
public String nowTime(){
Calendar 日历=Calendar.getInstance(); //使用默认时区和语言环境获得一个日历
int ny=日历.get(Calendar.YEAR);
int nmo=日历.get(Calendar.MONTH)+1; //因为月是从0开始算起的java停止倒计时代码,所以得加上1
int nd=日历.get(Calendar.DAY_OF_MONTH);
nh=日历.get(Calendar.HOUR_OF_DAY);
nm=日历.get(Calendar.MINUTE);
ns=日历.get(Calendar.SECOND);
String ss=new String(" 现在是"+ny+"年"+nmo+"月"+nd+"日 "+nh+"时"+nm+"分"+ns+"秒");
现在时间.setText(ss); //把获得的时间赋给“现在时间”标签
return ss;
}
//************** 响应Document事件,需重写三个方法 *************//
public void changedUpdate(DocumentEvent e){
if(时.getText().equals("")||分.getText().equals("")||秒.getText().equals(""))
{
关机.setEnabled(false);
重启.setEnabled(false);
清空.setEnabled(false);
注销.setEnabled(false);
取消.setEnabled(false);
}
else
{
关机.setEnabled(true);
重启.setEnabled(true);
清空.setEnabled(true);
注销.setEnabled(true);
}
}
public void removeUpdate(DocumentEvent e){
changedUpdate(e);
}
public void insertUpdate(DocumentEvent e){
changedUpdate(e);
}
//*********** 响应事件要执行的方法 *****************//
public void 关机程序(){ //关机
try{Runtime.getRuntime().exec("shutdown.exe -s -t 5");}
catch(IOException e){
JOptionPane.showMessageDialog(this,"执行失败!");
}
}
public void 重启程序(){ //重启
try{Runtime.getRuntime().exec("shutdown.exe -r -t 5");}
catch(IOException e){
JOptionPane.showMessageDialog(this,"执行失败!");
}
}
public void 注销程序(){ //注销
try{Runtime.getRuntime().exec("shutdown.exe -l");}
catch(IOException e){
JOptionPane.showMessageDialog(this,"执行失败!");
}
}
public void 取消程序(){ //取消
try{Runtime.getRuntime().exec("shutdown.exe -a");}
catch(IOException e){
JOptionPane.showMessageDialog(this,"执行失败!");
}
}
//*****************时间到就执行的方法**************//
public void 到时执行(){
if(nh==wh nm==wm ns==ws){ //如果设定的时间和现在时间相同就执行
if(标志==1){
关机程序();
}
if(标志==2){
重启程序();
}
if(标志==3){
注销程序();
}
}
}
//****************** 响应各按钮的单击事件 ****************//
public void actionPerformed(ActionEvent e)
{
wh=Integer.parseInt(时.getText());
wm=Integer.parseInt(分.getText());
ws=Integer.parseInt(秒.getText());
if(e.getSource()==关机){
关机.setEnabled(false);
重启.setEnabled(false);
注销.setEnabled(false);
标志=1;
if(wh=0wh=23wm=0wm=59ws=0ws=59){
动作();
}
else{
JOptionPane.showMessageDialog(this,"时间设置满足:0=小时24,0=分钟60,0=秒60!","温馨提示",JOptionPane.WARNING_MESSAGE);
时.requestFocus(); //获得焦点
}
}
if(e.getSource()==重启){
关机.setEnabled(false);
重启.setEnabled(false);
注销.setEnabled(false);
标志=2;
if(wh=0wh=23wm=0wm=59ws=0ws=59){
动作();
}
else
{
JOptionPane.showMessageDialog(this,"时间设置满足:0=小时24,0=分钟60,0=秒60!","温馨提示",JOptionPane.WARNING_MESSAGE);
时.requestFocus(); //获得焦点
}
}
if(e.getSource()==注销){
关机.setEnabled(false);
重启.setEnabled(false);
注销.setEnabled(false);
标志=3;
if(wh=0wh=23wm=0wm=59ws=0ws=59){
动作();
}
else
{
JOptionPane.showMessageDialog(this,"时间设置满足:0=小时24,0=分钟60,0=秒60!","温馨提示",JOptionPane.WARNING_MESSAGE);
时.requestFocus(); //获得焦点
}
}
if(e.getSource()==取消){
取消程序();
取消.setEnabled(false);
关机.setEnabled(true);
重启.setEnabled(true);
注销.setEnabled(true);
}
if(e.getSource()==清空){
时.setText("");
分.setText("");
秒.setText("");
时.requestFocus();
清空.setEnabled(false);
}
}
public void 监听()//时间一到就执行动作
{
new Thread(new Runnable()
{
public void run()
{
while(true)
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
到时执行();//关机或重启或注销
}
}
}).start();
}
public void 动作() //检查用户输入是否正确,在正确情况下时间到便执行
{
if(Test()==false)
{
JOptionPane.showMessageDialog(this,"时间设定错误,应设定在现在之后!");
时.requestFocus();
}
else{
JOptionPane.showMessageDialog(this,"时间设定成功!");
取消.setEnabled(true); //定义时间成功后取消按钮可点击
监听();
}
}
public boolean Test(){
boolean b=true;
if(whnh) //判断当前小时是否小于设定小时
b=false;
else if(wh==nh)
{
if(wmnm) //判断当前分钟是否小于设定分钟
b=false;
else if(wm==nm)
{
if(wsns+5) //要有延迟
b=false;
}
}
return b;
}
}
相关问答
Q1: java编程利用按钮实现时钟的停止和开始功能(原代码)
如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.geom.*;
import java.util.*;
class Clock extends Canvas
implements ActionListener{
static JButton jb=new JButton("开始");
static JButton jb1=new JButton("暂停");
Date date;
Timer secondTime;
int hour,munite,second;
Line2D secondLine,muniteLine,hourLine;
int a,b,c;
double pointSX[]=new double[60],//用来表示秒针端点坐标java停止倒计时代码的数组
pointSY[]=new double[60],
pointMX[]=new double[60], //用来表示分针端点坐标java停止倒计时代码的数组
pointMY[]=new double[60],
pointHX[]=new double[60], //用来表示时针端点坐标java停止倒计时代码的数组
pointHY[]=new double[60];
Clock()
{ secondTime=new Timer(1000,this);
pointSX[0]=0; //12点秒针位置
pointSY[0]=-100;
pointMX[0]=0; //12点分针位置
pointMY[0]=-90;
pointHX[0]=0; //12点时针位置
pointHY[0]=-70;
double angle=6*Math.PI/180; //刻度为6度
for(int i=0;i59;i++) //计算出各个数组中的坐标
{ pointSX[i+1]=pointSX[i]*Math.cos(angle)-Math.sin(angle)*pointSY[i];
pointSY[i+1]=pointSY[i]*Math.cos(angle)+pointSX[i]*Math.sin(angle);
pointMX[i+1]=pointMX[i]*Math.cos(angle)-Math.sin(angle)*pointMY[i];
pointMY[i+1]=pointMY[i]*Math.cos(angle)+pointMX[i]*Math.sin(angle);
pointHX[i+1]=pointHX[i]*Math.cos(angle)-Math.sin(angle)*pointHY[i];
pointHY[i+1]=pointHY[i]*Math.cos(angle)+pointHX[i]*Math.sin(angle);
}
for(int i=0;i60;i++)
{ pointSX[i]=pointSX[i]+120; //坐标平移
pointSY[i]=pointSY[i]+120;
pointMX[i]=pointMX[i]+120; //坐标平移
pointMY[i]=pointMY[i]+120;
pointHX[i]=pointHX[i]+120; //坐标平移
pointHY[i]=pointHY[i]+120;
}
secondLine=new Line2D.Double(0,0,0,0);
muniteLine=new Line2D.Double(0,0,0,0);
hourLine=new Line2D.Double(0,0,0,0);
secondTime.start(); //秒针开始计时
}
public void paint(Graphics g)
{ for(int i=0;i60;i++) //绘制表盘上的小刻度和大刻度
{ int m=(int)pointSX[i];
int n=(int)pointSY[i];
if(i%5==0)
{ g.setColor(Color.red);
g.fillOval(m-4,n-4,8,8);
}
else
{ g.setColor(Color.cyan);
g.fillOval(m-2,n-2,4,4);
}
}
g.fillOval(115,115,10,10); //钟表中心的实心圆
Graphics2D g_2d=(Graphics2D)g;
g_2d.setColor(Color.red);
g_2d.draw(secondLine);
BasicStroke bs=
new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.blue);
g_2d.draw(muniteLine);
bs=new BasicStroke(6f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.green);
g_2d.draw(hourLine);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==secondTime){
date=new Date();
String s=date.toString();
hour=Integer.parseInt(s.substring(11,13));
munite=Integer.parseInt(s.substring(14,16));
second=Integer.parseInt(s.substring(17,19)); //获取时间中的秒
int h=hour%12;
a=second; //秒针端点的坐标
b=munite; //分针端点的坐标
c=h*5+munite/12; //时针端点的坐标
secondLine.setLine(120,120,(int)pointSX[a],(int)pointSY[a]);
muniteLine.setLine(120,120,(int)pointMX[b],(int)pointMY[b]);
hourLine.setLine(120,120,(int)pointHX[c],(int)pointHY[c]);
repaint();
} if(e.getSource()==jb){
secondTime.start();
}if(e.getSource()==jb1){
secondTime.stop();
}
}
public static void main(String args[]){
JFrame win=new JFrame("时钟");
JPanel jp=new JPanel();
jp.add(jb);
jp.add(jb1);
Clock clock=new Clock();
jb.addActionListener(clock);
jb1.addActionListener(clock);
win.add(clock,BorderLayout.CENTER);
win.add(jp,"South");
win.setVisible(true);
win.setSize(246,300);
win.setDefaultCloseOperation(3) ;
win.validate();
}
}
运行截图:
有问题就追问java停止倒计时代码,满意请采纳。
Q2: java定时器怎么停止
1、scheduleUpdate
加入当前节点后,程序会每帧都会自动执行一次默认的Update函数。(注:一定是Update函数哦,若想调用其他自己命名的函数则使用schedule)
看例子,走起。
首先在HelloWord类的头文件中声明Update函数:
[cpp] view plain copy print?
void update(float dt); //注意参数类型
然后在HelloWorld类源文件中实现函数Update:
[cpp] view plain copy print?
void HelloWorld::update(float dt)
{
CCLOG("baibai");
}
现在我们可以调用了,在需要他不断执行的地方加入调用的代码就ok:
[cpp] view plain copy print?
this-scheduleUpdate(); //this是当前节点,如layer,所以可以省略啦。
运行之后你将会看到不断有baibai被打印出来
2、scheduleUpdate
可以没隔几秒执行某个自定义的函数,来看代码:
首先还是在HelloWorld中声明所要执行的函数:
Q3: 用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(); } } } }}
java停止倒计时代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java倒计时程序、java停止倒计时代码的信息别忘了在本站进行查找喔。








