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
}
}
}
小兄弟、你的程序也不算大程序、但是樱渣要做,至少也得3个小时、脊羡悄都在上班、哪有那么多时间给你做啊。知识是学着自己用的、你要是把这个项目做出来了、你就会进步、会明白很多、其实java真的不难派悔,何不自己试试呢?一步一步的做,加油,别低估了JAVA。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)