实现秒表的功能的方法
怎么实现秒表的功能??有个按钮,一按开始计时,再按停止。
public class StopWatch extends javax.swing.JFrame {
private TimerThread timer = null;
/** Creates new form StopWatch */
public StopWatch() {
initComponents();
timer = new TimerThread();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
txcounter = new javax.swing.JTextField();
btstart = new javax.swing.JButton();
btreset = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
btmark = new javax.swing.JToggleButton();
jScrollPane1 = new javax.swing.JScrollPane();
tarea = new javax.swing.JTextArea();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().add(txcounter, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 40, 120, 30));
btstart.setText("/u542f/u52a8");
btstart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btstartActionPerformed(evt);
}
});
getContentPane().add(btstart, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 40, -1, -1));
btreset.setText("/u91cd/u7f6e");
btreset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btresetActionPerformed(evt);
}
});
getContentPane().add(btreset, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 40, 60, -1));
jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("/u8ba1/u65f6/u5668");
jLabel1.setToolTipText("null");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 50, 30));
btmark.setText("/u8ba1/u65f6");
btmark.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btmarkActionPerformed(evt);
}
});
getContentPane().add(btmark, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 40, 60, -1));
jScrollPane1.setViewportView(tarea);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, 360, 150));
pack();
}
private void btresetActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
tarea.setText("");
timer.reset();
}
private void btmarkActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
timer.mark = true;
}
private void btstartActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
if(btstart.getLabel().equalsIgnoreCase("启动"))
{
btstart.setLabel("停止");
if(timer.stop)
timer.stop = false;
else
timer.start();
}
else
{
btstart.setLabel("启动");
timer.stopCounter();
}
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
StopWatch watch = new StopWatch();
watch.setSize(400,300);
watch.show();
}
class TimerThread extends Thread
{
boolean active = false;
boolean stop = false;
boolean mark = false;
private int counter = 0;
private long startTime = 0;
TimerThread()
{
init();
}
public void stopCounter()
{
stop = true;
}
public void reset()
{
init();
startTime = System.currentTimeMillis();
}
public void init()
{
counter = 0;
active = true;
stop = false;
}
public void destory()
{
active = false;
stop = true;
}
private String getTime()
{
long tmp = System.currentTimeMillis()-startTime;
int d = (int)(tmp/(1000*60*60))%24;//分
String temp = (d<10 ? "0"+d : d+"")+":";
d = (int)(tmp/(1000*60))%60;//分
temp += (d<10 ? "0"+d : d+"")+":";
d = (int)(tmp/1000)%60;//秒
temp += (d<10 ? "0"+d : d+"")+".";
temp += (tmp%1000)/100;
return temp;
}
private void pr(String message)
{
System.out.println(message);
}
public void run()
{
startTime = System.currentTimeMillis();
while(active)
{
while(!stop)
{
if(mark)
{
tarea.append(getTime() + " 第"+ counter + "次计数 /n");
counter++;
mark = false;
}
txcounter.setText(getTime());
try{Thread.sleep(100);}catch(Exception e){}
}
try{Thread.sleep(100);}catch(Exception e){}
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton btreset;
private javax.swing.JTextField txcounter;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JToggleButton btmark;
private javax.swing.JButton btstart;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextArea tarea;
// End of variables declaration
}
Top
3 楼geddy()回复于 2004-11-16 20:59:29 得分 0
org.netbeans.lib.awtextra.AbsoluteLayout()
org.netbeans.lib.awtextra.AbsoluteConstraints()
未定义啊,能不能用标准的库实现!Top
本文地址:http://www.45fan.com/a/question/71725.html