2、 考试时间的倒计时
a) 在界面显示2个小时的考试时间倒计时(使用线程)
b) 并可以实现倒计时的暂停和继续
package pojo;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class TestClock {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Clock();
}
}
class Clock extends JFrame implements ActionListener {
//private Clocker c = new Clocker();
private JLabel label = new JLabel();
private Button button1=new Button("继续");
private Button button2=new Button("停止");
private boolean isStart = false;
private static Date date = new Date(7200000*9+1000);
public Clock (){
super("电子时钟");
this.setLocation(300,300);
this.setSize(100,150);
this.setBackground(Color.black);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
label.setFont(new Font("Dialog", Font.BOLD, 30));
setLabel();
this.add(label,BorderLayout.NORTH);
Panel p1= new Panel();
p1.add(button1);
p1.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
this.add(p1,BorderLayout.SOUTH);
this.setVisible(true);
this.setResizable(false);
this.pack();
}
private Date subTime(){
long l = 1000;
this.date.setTime(date.getTime()-l);
return date;
}
public void setLabel(){
Date d = subTime();
String strTime = String.format("%tT",d);
label.setText(strTime);
}
public void actionPerformed(ActionEvent e) {
Clocker c = new Clocker();
String current = e.getActionCommand();
if (current == "继续") {
c.starts();
}
if (current == "停止") c.stops();
}
class Clocker extends Thread{
public void run(){
while(isStart) {
setLabel();
try {
Thread.sleep(1000);
} catch(Exception e) {
e.printStackTrace();
}
}
}
public void starts(){
isStart = true;
this.start();
}
public void stops (){
isStart = false;
}
}
}
2、 考试时间的倒计时
a) 在界面显示2个小时的考试时间倒计时(使用线程)
b) 并可以实现倒计时的暂停和继续
c) 时间结束后能够继续开始
package chn1;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
public class ExamCountDown extends JFrame implements Runnable{
private boolean flag = true;
private boolean flag1 = true;
private JPanel contentPane;
private Timer timer;
private JTextArea textArea;
private String string = "02:00:00";
private int []intArray;
private JButton button;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ExamCountDown frame = new ExamCountDown();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ExamCountDown() {
String []stringArray = string.split(":");
intArray = new int[3];
for(int i = 0;i < stringArray.length;i++){
intArray[i] = Integer.parseInt(stringArray[i]);
}
setTitle("\u8003\u8BD5\u65F6\u95F4\u5012\u8BA1\u65F6");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u5269\u4F59\u65F6\u95F4\uFF1A");
label.setFont(new Font("宋体", Font.BOLD, 27));
textArea = new JTextArea();
textArea.setText(string);
textArea.setFont(new Font("宋体", Font.BOLD, 27));
timer = new Timer();
button = new JButton("开始");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
timer = new Timer();
textArea.setText(string);
flag = true;
flag1 = true;
// TODO Auto-generated method stub
timer.schedule(new Task(),0, 1000);
button.setEnabled(false);
string = "02:00:00";
}
});
JButton button_1 = new JButton("暂停");
button_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
flag = false;
} catch (Exception e1) {
// TODO: handle exception
}
}});
JButton button_2 = new JButton("继续");
button_2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
flag = true;
}
});
GroupLayout groupLayout = new GroupLayout(contentPane);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button)
.addGap(47, 47, 47)
.addComponent(button_1)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(button_2)))
.addContainerGap(101, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(58, 58, 58)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(64, 64, 64)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(button_1)
.addComponent(button_2))
.addContainerGap(76, Short.MAX_VALUE))
);
contentPane.setLayout(groupLayout);
}
public JTextArea getTextArea() {
return textArea;
}
public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}
class Task extends TimerTask{
String string1 = string;
DecimalFormat decimalFormat = new DecimalFormat("00");
public void run() {
if(flag1 == false){
return;
}
// TODO Auto-generated method stub
if(flag == true){
intArray[2] -= 1;
if(intArray[2] < 0)
{
intArray[1] -= 1;
intArray[2] += 60;
if(intArray[1] < 0)
{
intArray[0] -= 1;
intArray[1] += 60;
}
}
if(intArray[0] <= 0 && intArray[1] <= 0
&& intArray[2] <= 0)
{
flag1 = false;
button.setEnabled(true);
timer.cancel();
string = new String(intArray[0] + ":" + intArray[1]
+ ":" + intArray[2]);
string = string.format("%02d:%02d:%02d", intArray[0],intArray[1],intArray[2]);
textArea.setText(string);
string = "02:00:00";
String []stringArray = string.split(":");
intArray = new int[3];
for(int i = 0;i < stringArray.length;i++){
intArray[i] = Integer.parseInt(stringArray[i]);
}
return;
}
string = string.format("%02d:%02d:%02d", intArray[0],intArray[1],intArray[2]);
textArea.setText(string);
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
代码太长, 直接hi我.. 或者追问留下你的联系方式,给你发..
这么难的题啊…
太大了,需要时间,