我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java增加计时代码,java增加时间

写一个计时器 JAVA代码是什么?

应该用线程里面的Timer来控制package com.sy.game.test;

荷塘ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

import java.util.Timer;

import java.util.TimerTask;

public class TimeTask {

public static void main(String[] args) {

TimeTask tTask=new TimeTask();

tTask.timeVoid();

}

public void timeVoid(){

final Timer timer = new Timer();

TimerTask tt=new TimerTask() {

@Override

public void run() {

System.out.println("到点啦!");

timer.cancel();

}

};

timer.schedule(tt, 3000);

}

}

整合的:

/*

* java倒计时器

* shiyang

* */

package com.sy.game.test;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.Timer;

@SuppressWarnings("unused")

public class TimeController extends JFrame implements ActionListener {

private static final long serialVersionUID = 4603262282860990473L;

private static final int DEFAULT_WIDTH = 200;

private static final int DEFAULT_HEIGHT = 100;

private static final int width = Toolkit.getDefaultToolkit()

.getScreenSize().width;

private static final int height = Toolkit.getDefaultToolkit()

.getScreenSize().height;

private Container container;

private JButton btn;

private JTextField jtfTime;

private Timer tmr;

public TimeController() {

initComponents();

Timer tmr = new Timer(1000, this);

this.tmr = tmr;

setVisible(true);

}

private void initComponents() {

this.setTitle("SY秒表");

this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

this.setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocation((width - DEFAULT_WIDTH) / 2,

(height - DEFAULT_HEIGHT) / 2);

jtfTime = new JTextField("10");

btn = new JButton("开始倒计时");

container = getContentPane();

JPanel panel = new JPanel();

panel.add(btn);

panel.add(jtfTime);

this.add(panel);

btn.addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == btn) {

jtfTime.setText("10");

tmr.start();

} else {

int t;

t = Integer.parseInt(jtfTime.getText());

t--;

jtfTime.setText("" + t);

if (t = 0) {

tmr.stop();

}

}

}

public static void main(String[] args) {

TimeController timeController = new TimeController();

}

}

用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编写一条计时器代码。

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import java.io.*;

import java.util.*;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.plaf.OptionPaneUI;

public class Demo {

static boolean isRuning=false;

static boolean isFirst=true;

@SuppressWarnings("unchecked")

public static void main(String[] args) throws Exception {

JFrame form1 = new JFrame("Form1");

JTextField jTextField = new JTextField(10);

jTextField.setSize(10, 10);

jTextField.setText("0");

jTextField.setEditable(false);

JButton jButton = new JButton("开始");

jButton.setSize(10, 10);

Thread thread = new Thread(new Runnable() {

@Override

public void run() {

while (true) {

while(isRuning){

Integer counter = Integer.parseInt(jTextField.getText().trim());

counter++;

jTextField.setText(counter.toString());

try {

Thread.sleep(1000);

} catch (Exception e2) {

}

}

}

}

});

jButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

   String text=jButton.getText().equals("开始")?"暂停":"开始";

   jButton.setText(text);

   isRuning=!isRuning;

   if(isFirst){

   thread.start();

   isFirst=false;

   }

}

});

JPanel panel = new JPanel();

panel.setSize(200, 200);

panel.add(jTextField, BorderLayout.NORTH);

panel.add(jButton, BorderLayout.CENTER);

form1.add(panel);

form1.setBounds(200, 100, 250, 150);

form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

form1.addWindowListener(new WindowListener() {

@Override

public void windowOpened(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowIconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeiconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeactivated(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowClosing(WindowEvent e) {

// 窗口关闭前取出文本框的数字保存到外部文件,代码在此处写

JOptionPane.showMessageDialog(null, "Are you sure closing?");

}

@Override

public void windowClosed(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowActivated(WindowEvent e) {

// TODO Auto-generated method stub

}

});

form1.setVisible(true);

}

}


分享文章:java增加计时代码,java增加时间
文章URL:http://shouzuofang.com/article/hspdjd.html

其他资讯