java计时代码完成时间 java程序计时( 二 )


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中如何实现计时的这样一个功能,比如在限定时间完成游戏 , 没有完成就退出,怎么限定的时间呢?//可以启动一个线程来做时间计算
//也可以用timer来实现定时 timer使用wait 我这里用sleep
//关键不是用什么而是用线程来解决问题
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Runnable r = new Runnable() {
final long start = System.currentTimeMillis() ;
public void run() {
try {
while(true) {
Thread.sleep(1000);
long now = System.currentTimeMillis();
if(now-start=10000) {
System.out.println("打字时间结束了");
System.exit(0);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
System.out.println("可以开始打字了,10秒后就不能打了");
Scanner s = new Scanner(System.in);
Thread t = new Thread(r);
t.start();
while(s.hasNext()) {
System.out.println(s.nextLine());
}
}
}
关于java计时代码完成时间和java程序计时的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读