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("計(jì)時(shí)器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("計(jì)時(shí)器"); 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);// 設(shè)定 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(); } } } }}
專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)臨江免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
下面是改過的代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise6_41 extends JApplet implements ActionListener
{
Thread timeSub;//倒計(jì)時(shí)器
JButton buttonStart, buttonRetry; // 開始 和 結(jié)束按鈕
JLabel questionLabel, answerLabel, levelLabel, styleLabel;
JLabel TimeCount;// 問題,答案,等級和類型的標(biāo)簽
JTextField questionField, answerField, levelField, styleField;
public JTextField timeField;// 問題,回答,等級和類型 的輸入框
JTextArea outputArea; // 程序記錄域
JScrollPane scroller; // 載入記錄
int MAX = 20; // 規(guī)定了一套試題的題量
int level = 1; // 規(guī)定了試題的等級,1到4 分別代表一位數(shù),兩位數(shù),三位數(shù),四位數(shù)的運(yùn)算
int style = 3; // 規(guī)定了試題的類型,1到5分別代表加法,減法,乘法,求余數(shù),和混合類型
int answer; // 存儲(chǔ)用戶輸入的答案
int rightAnswer; // 存儲(chǔ)標(biāo)準(zhǔn)答案
int rightCounter = 0; // 已經(jīng)正確回答的問題 計(jì)數(shù)器
int totalCounter = 0; // 總的問題數(shù) 計(jì)數(shù)器
boolean presentQuestionAnswered = false; // 用來標(biāo)記系統(tǒng)所給出的當(dāng)前問題是否已被回答,false 代表未回答
public void init()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
levelLabel = new JLabel("請?jiān)俅溯斎胱鰩孜粩?shù)的運(yùn)算(1-4):");
container.add(levelLabel);
levelField = new JTextField(5);
levelField.addActionListener(this);
container.add(levelField);
styleLabel = new JLabel("請?jiān)俅溯斎朐囶}的類型(1:加法 2:減法 3:乘法 4:MOD 5:混合):");
container.add(styleLabel);
styleField = new JTextField(5);
styleField.addActionListener(this);
container.add(styleField);
buttonStart = new JButton("開始");
buttonStart.addActionListener(this);
container.add(buttonStart);
questionLabel = new JLabel("請回答下面的問題:");
container.add(questionLabel);
questionField = new JTextField(20);
questionField.setEditable(false);
container.add(questionField);
answerLabel = new JLabel("在此輸入您的答案:");
container.add(answerLabel);
answerField = new JTextField(20);
answerField.setText("");
answerField.addActionListener(this);
container.add(answerField);
outputArea = new JTextArea(20, 40);
outputArea.setEditable(false);
scroller = new JScrollPane(outputArea);
container.add(scroller);
buttonRetry = new JButton("再來一次");
buttonRetry.addActionListener(this);
container.add(buttonRetry);
JLabel TimeCount = new JLabel("計(jì)時(shí)器");
container.add(TimeCount);
timeField = new JTextField(20);
timeField.setEditable(false);
container.add(timeField);
}
public void actionPerformed(ActionEvent actionEvent)
{
if (actionEvent.getSource() == levelField)
{
level = Integer.parseInt(levelField.getText());
if (level 5 || level 1)
{
JOptionPane.showMessageDialog(null,
"您輸入了未定義的等級!\n請重新輸入!\n如果您堅(jiān)持系統(tǒng)將會(huì)使用默認(rèn)的等級!", "錯(cuò)誤信息!",
JOptionPane.INFORMATION_MESSAGE);
level = 1;
}
else
{
levelField.setEditable(false);
}
}
else if (actionEvent.getSource() == styleField)
{
style = Integer.parseInt(styleField.getText());
if (style 5 || style 1)
{
JOptionPane.showMessageDialog(null,
"您輸入了未定義的試題類型!\n請重新輸入!如果您堅(jiān)持系統(tǒng)將會(huì)使用默認(rèn)的等級!", "錯(cuò)誤信息!",
JOptionPane.INFORMATION_MESSAGE);
style = 1;
}
else
{
styleField.setEditable(false);
}
}
else if (actionEvent.getSource() == buttonStart)
{
int time=Integer.parseInt(JOptionPane.showInputDialog(null,"輸入定時(shí)時(shí)間!","60").trim());//獲得自定義時(shí)間
timeSub=new Exercise6_41TimeSub(time,timeField);//創(chuàng)建計(jì)時(shí)器
timeSub.start();//啟動(dòng)計(jì)時(shí)器
answerField.setEditable(true);
answerField.setText("");
totalCounter = 0;
rightCounter = 0;
{
createQuestion(style);
}
}
else if (actionEvent.getSource() == answerField)
{
answer = Integer.parseInt(answerField.getText());
judgeAnswer();
presentQuestionAnswered = true;
totalCounter++;
answerField.setText("");
if (totalCounter MAX)
{
createQuestion(style);
}
else
{
questionField.setText("你已經(jīng)回答完了規(guī)定數(shù)量的試題!");
answerField.setText("");
questionField.setEditable(false);
answerField.setEditable(false);
caculateResult();
}
}
else if (actionEvent.getSource() == buttonRetry)
{
level = 1;
style = 1;
levelField.setText("");
levelField.setEditable(true);
styleField.setText("");
styleField.setEditable(true);
totalCounter = 0;
rightCounter = 0;// /再次初始化
}
}
public void createQuestion(int style)
{
if (style == 1)
{
long multi = 0;
if (level 0 level = 5)
{
multi = (long) (Math.pow(10, level));
}
int number1 = (int) (Math.random() * multi);
int number2 = (int) (Math.random() * multi);
rightAnswer = number1 + number2;
questionField.setText(number1 + " + " + number2 + " = ? ");
outputArea.append("\nQuestion " + (int) (totalCounter + 1) + " :"
+ number1 + " + " + number2 + " = ? ");
}
else if (style == 2)
{
long multi = 0;
if (level 0 level = 5)
{
multi = (long) (Math.pow(10, level));
}
int number1 = (int) (Math.random() * multi);
int number2 = (int) (Math.random() * multi);
rightAnswer = number1 - number2;
questionField.setText(number1 + " - " + number2 + " = ? ");
outputArea.append("\nQuestion " + (int) (1 + totalCounter) + " :"
+ number1 + "- " + number2 + " = ? ");
}
else if (style == 3)
{
long multi = 0;
if (level 0 level = 5)
{
multi = (long) (Math.pow(10, level));
}
int number1 = (int) (Math.random() * multi);
int number2 = (int) (Math.random() * multi);
rightAnswer = number1 * number2;
questionField.setText(number1 + " X " + number2 + " = ? ");
outputArea.append("\nQuestion " + (int) (totalCounter + 1) + " :"
+ number1 + " X " + number2 + " = ? ");
}
else if (style == 4)
{
long multi = 0;
if (level 0 level 5)
{
multi = (long) (Math.pow(10, level));
}
int number1 = (int) (Math.random() * multi);
int number2 = (int) (Math.random() * multi);
rightAnswer = number1 % number2;
questionField.setText(number1 + " % " + number2 + " = ? ");
outputArea.append("\nQuestion " + (int) (1 + totalCounter) + " :"
+ number1 + " % " + number2 + " = ? ");
}
else if (style == 5)
{
int temp;
temp = (int) (Math.random() * 100) % 4 + 1;
createQuestion(temp);
}
presentQuestionAnswered = false;
}
public void judgeAnswer()
{
if (answer == rightAnswer)
{
rightCounter++;
outputArea.append(" 回答正確! ");
}
else if (answer != rightAnswer)
{
outputArea.append(" 回答錯(cuò)誤! ");
answerField.setText("");
answer = -1;
}
outputArea.append("還剩余 " + (MAX - totalCounter - 1) + " 個(gè)問題!\n");
}
public void caculateResult()
{
if (totalCounter == 0)
{
outputArea.append("\n您還未做任何題目!");
JOptionPane.showMessageDialog(null, "無法計(jì)算你的正確率!", "統(tǒng)計(jì)信息!",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
double result = rightCounter / totalCounter * 100;
outputArea.append("\n正確的概率為:" + result + "%\n");
JOptionPane.showMessageDialog(null, "正確的概率為:" + result + "%",
"統(tǒng)計(jì)信息!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
/**********************************************************************
計(jì)時(shí)器線程
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Exercise6_41TimeSub extends Thread
{
private int time;
private JTextField showTime;
public Exercise6_41TimeSub(int time,JTextField showTime)
{
this.time=time;
this.showTime=showTime;
}
public void run()
{
while(true)
{
time--;
showTime.setText("剩余時(shí)間:"+time);
if(time==0)
{
JOptionPane.showMessageDialog(null,"時(shí)間到,請你檢查你的答題情況!","時(shí)間到",JOptionPane.INFORMATION_MESSAGE);
}
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
哎這個(gè)太簡單了。。。
Timer t = new Timer();
int s = 5;
TimerTask tt = new TimerTask()
{
public void run()
{
if(s 0)
s--;
}
};
t.scheduleAtFixedRate(tt,0,1000);
應(yīng)該用線程里面的Timer來控制package com.sy.game.test;
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("到點(diǎn)啦!");
timer.cancel();
}
};
timer.schedule(tt, 3000);
}
}
整合的:
/*
* java倒計(jì)時(shí)器
* 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("開始倒計(jì)時(shí)");
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();
}
}
抱歉,之前沒看到第二個(gè)條件,重新寫了下。
在本機(jī)上可以正確運(yùn)行。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TimeThreadFrame extends JFrame{
// 定義組件
private JLabel lblTime;
private JTextField txtInput;
private JButton btnEnter;
// 記錄所要啟動(dòng)的程序
private Process runningProcess;
// 構(gòu)造方法
public TimeThreadFrame(){
// 設(shè)置窗體的相關(guān)屬性
super("TimerThread");
this.setSize(300,200);
this.setLayout(null);
this.setLocation(100,50);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 創(chuàng)建組件
this.lblTime = new JLabel("請輸入倒計(jì)時(shí)時(shí)間");
this.lblTime.setBounds(30,20,200,30);
this.txtInput = new JTextField();
this.txtInput.setBounds(30,70,100,30);
this.btnEnter = new JButton("確定");
this.btnEnter.setBounds(150,70,70,30);
this.runningProcess = null;
// 給JTextField注冊監(jiān)聽
this.txtInput.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke) {
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
txtInput_KeyTyped(ke);
}
});
// 給JButton注冊監(jiān)聽
this.btnEnter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnEnter_ActionPerformed(ae);
}
});
// 將各組件添加到窗體上
add(lblTime);
add(txtInput);
add(btnEnter);
// 顯示窗體
this.setVisible(true);
}
// 輸入時(shí)的事件處理,控制用戶只能輸入數(shù)字
public void txtInput_KeyTyped(KeyEvent ke){
if(ke.getKeyChar() '0' || ke.getKeyChar() '9'){
ke.setKeyChar('\0');
}
}
// 點(diǎn)擊按鈕時(shí)的事件處理,核心!
public void btnEnter_ActionPerformed(ActionEvent ae){
// 獲得用戶輸入的倒計(jì)時(shí)時(shí)間
String strTime = this.txtInput.getText();
if(strTime.equals("")){
// 檢測用戶是否輸入
this.lblTime.setText("您尚未輸入,請輸入!");
}
else{
Integer time = Integer.parseInt(strTime);
// 創(chuàng)建線程
TimeThread tt = new TimeThread(this.lblTime,time);
tt.start();
// 創(chuàng)建Timer
Timer timer = new Timer();
timer.schedule(new TimerTask(){
// 啟動(dòng)其他程序
public void run() {
try {
// 當(dāng)程序不存在時(shí),會(huì)進(jìn)行創(chuàng)建;存在時(shí)直接調(diào)用。
runningProcess = Runtime.getRuntime().exec("D:\\Program Files\\Tencent\\QQDoctor\\QQDoctor.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
}, time * 1000);
}
}
// 啟動(dòng)窗體
public static void main(String[] args){
TimeThreadFrame ttf = new TimeThreadFrame();
}
}
// 時(shí)間線程類
class TimeThread extends Thread{
private JLabel lblTime;
private int time;
// 構(gòu)造方法傳入,顯示事件的JLabel和倒計(jì)時(shí)的時(shí)間。
public TimeThread(JLabel lblTime, int time){
this.lblTime = lblTime;
this.time = time;
}
// run方法
public void run(){
while(time 0){
// 顯示所剩時(shí)間
this.lblTime.setText("所剩時(shí)間:" + time);
// 所剩時(shí)間減少
time--;
try {
this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
新聞標(biāo)題:java倒計(jì)時(shí)器代碼插件 java 倒計(jì)時(shí)器
文章起源:http://m.rwnh.cn/article22/hpjsjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、域名注冊、建站公司、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站營銷、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)