当前位置:  技术问答>java相关

大侠们,帮个忙!我编个计时器出现问题!

    来源: 互联网  发布时间:2015-08-22

    本文导语:  共有3个button,其中时钟的按钮,按下时,线程ClockThread,他没有重复的刷新label1。为什么呢? package homework1; import javax.swing.*; import java.awt.*; import javax.swing.border.*; import java.awt.event.*; import java.util.*; import java.lang.Th...

共有3个button,其中时钟的按钮,按下时,线程ClockThread,他没有重复的刷新label1。为什么呢?
package homework1;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Thread;
/**
 * 

Title: 


 * 

Description: 


 * 

Copyright: Copyright (c) 2002


 * 

Company: 


 * @author unascribed
 * @version 1.0
 */

public class Clock {
    private static JPanel jPanel1 = new JPanel();
    private JLabel jLabel1 = new JLabel();
    private JButton btn_Start = new JButton();
    private JButton btn_Clean = new JButton();
    private JButton btn_Clock = new JButton();
    private BorderLayout borderLayout1 = new BorderLayout();
    private TitledBorder titledBorder1;

    public Clock() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    private void jbInit() throws Exception {
        titledBorder1 = new TitledBorder("");
        jLabel1.setBackground(Color.white);
        jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel1.setForeground(Color.red);
        jLabel1.setBorder(titledBorder1);
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("jLabel1");
        btn_Start.setBackground(Color.orange);
        btn_Start.setFont(new java.awt.Font("Dialog", 0, 14));
        btn_Start.setForeground(Color.red);
        btn_Start.setBorder(titledBorder1);
        btn_Start.setToolTipText("");
        btn_Start.setMnemonic('S');
        btn_Start.setText("开始(S)");
        btn_Start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn_Start_actionPerformed(e);
            }
        });
        btn_Clean.setBackground(Color.orange);
        btn_Clean.setFont(new java.awt.Font("Dialog", 0, 14));
        btn_Clean.setForeground(Color.red);
        btn_Clean.setBorder(titledBorder1);
        btn_Clean.setMnemonic('C');
        btn_Clean.setText(" 清零(C)");
        btn_Clean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn_Clean_actionPerformed(e);
            }
        });
        btn_Clock.setBackground(Color.orange);
        btn_Clock.setFont(new java.awt.Font("Dialog", 0, 14));
        btn_Clock.setForeground(Color.magenta);
        btn_Clock.setBorder(titledBorder1);
        btn_Clock.setMnemonic('O');
        btn_Clock.setText(" 时钟(O)");
        btn_Clock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn_Clock_actionPerformed(e);
            }
        });
        jPanel1.setLayout(borderLayout1);
        borderLayout1.setHgap(2);
        borderLayout1.setVgap(2);
        jPanel1.setEnabled(true);
        jPanel1.add(jLabel1, BorderLayout.NORTH);
        jPanel1.add(btn_Start, BorderLayout.WEST);
        jPanel1.add(btn_Clean, BorderLayout.CENTER);
        jPanel1.add(btn_Clock, BorderLayout.EAST);
    }
/*    public void start(){
        Thread clockThread;
        if(clockThread==null){
            clockThread=new java.lang.Thread(this,"Clock");
            clockThread.start();
        }
    }*/
    public static void main(String args[]){
        JFrame frame=new JFrame("nihao");
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        new Clock();
        frame.getContentPane().add(jPanel1,BorderLayout.CENTER );
        frame.pack();
        frame.setVisible(true);
    }

    void btn_Start_actionPerformed(ActionEvent e) {
        jLabel1.setText("nihao");
    }

    void btn_Clean_actionPerformed(ActionEvent e) {
        jLabel1.setText("00:00:00");
    }

    void btn_Clock_actionPerformed(ActionEvent e) {
        ClockThread nowClock=new ClockThread();
        nowClock.run(jLabel1);
        nowClock.start();
/*        jLabel1.setText(nowClock.repaint());
        Date now=new Date();
        String s;
        s=now.getHours()+":"+now.getMinutes() +":"+now.getSeconds();
        jLabel1.setText(s);*/
    }
}

import java.util.*;
import javax.swing.*;
public class ClockThread extends Thread {
    private Thread clockTh=null;
    public void start(){
        if(clockTh==null){
            clockTh=new Thread(this,"ClockThread");
            clockTh.start();
        }
    }
    public void run(JLabel L){
        Thread myThread=Thread.currentThread();
        start();
        System.out.println("currentThread = "+myThread);
        System.out.println("this = "+this);
        System.out.println("clockTh = "+clockTh);
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e){}
        L.setText("Zhangping");
    /*        try{
                Thread.sleep(1000);
            }catch(InterruptedException e){}
            L.setText("23:59:59");*/

        while(clockTh==myThread){
            L.setText(repaint());
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){}
            if(clockTh.getThreadGroup()!=myThread.getThreadGroup())
                System.out.println("Flase");
            else
                System.out.println("True");
            }
        }
    public String repaint(){
        String s;
        Date now=new Date();
        s=now.getHours() +":"+now.getMinutes()+":"+now.getSeconds();
        return s;
    }
}

|
也许不太规范,但你所要的功能是实现了:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.*;
import java.util.*;
import javax.swing.*;
import java.lang.Thread;
/**
 * 

Title: 


 * 

Description: 


 * 

Copyright: Copyright (c) 2002


 * 

Company: 


 * @author unascribed
 * @version 1.0
 */

public class Clock {

    ClockThread nowClock=new ClockThread();

    private static JPanel jPanel1 = new JPanel();
    private JLabel jLabel1 = new JLabel();
    private JButton btn_Start = new JButton();
    private JButton btn_Clean = new JButton();
    private JButton btn_Clock = new JButton();
    private BorderLayout borderLayout1 = new BorderLayout();
    private TitledBorder titledBorder1;

    public Clock() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    private void jbInit() throws Exception {
        titledBorder1 = new TitledBorder("");
        jLabel1.setBackground(Color.white);
        jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel1.setForeground(Color.red);
        jLabel1.setBorder(titledBorder1);
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("jLabel1");
        btn_Start.setBackground(Color.orange);
        btn_Start.setFont(new java.awt.Font("Dialog", 0, 14));
        btn_Start.setForeground(Color.red);
        btn_Start.setBorder(titledBorder1);
        btn_Start.setToolTipText("");
        btn_Start.setMnemonic('S');
        btn_Start.setText("开始(S)");
        btn_Start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn_Start_actionPerformed(e);
            }
        });
        btn_Clean.setBackground(Color.orange);
        btn_Clean.setFont(new java.awt.Font("Dialog", 0, 14));
        btn_Clean.setForeground(Color.red);
        btn_Clean.setBorder(titledBorder1);
        btn_Clean.setMnemonic('C');
        btn_Clean.setText(" 清零(C)");
        btn_Clean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn_Clean_actionPerformed(e);
            }
        });
        btn_Clock.setBackground(Color.orange);
        btn_Clock.setFont(new java.awt.Font("Dialog", 0, 14));
        btn_Clock.setForeground(Color.magenta);
        btn_Clock.setBorder(titledBorder1);
        btn_Clock.setMnemonic('O');
        btn_Clock.setText(" 时钟(O)");
        btn_Clock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn_Clock_actionPerformed(e);
            }
        });
        jPanel1.setLayout(borderLayout1);
        borderLayout1.setHgap(2);
        borderLayout1.setVgap(2);
        jPanel1.setEnabled(true);
        jPanel1.add(jLabel1, BorderLayout.NORTH);
        jPanel1.add(btn_Start, BorderLayout.WEST);
        jPanel1.add(btn_Clean, BorderLayout.CENTER);
        jPanel1.add(btn_Clock, BorderLayout.EAST);

//        ClockThread nowClock=new ClockThread();
    }
/*    public void start(){
        Thread clockThread;
        if(clockThread==null){
            clockThread=new java.lang.Thread(this,"Clock");
            clockThread.start();
        }
    }*/
    public static void main(String args[]){
        JFrame frame=new JFrame("nihao");
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        new Clock();
        frame.getContentPane().add(jPanel1,BorderLayout.CENTER );
        frame.pack();
        frame.setVisible(true);
    }

    void btn_Start_actionPerformed(ActionEvent e) {
        jLabel1.setText("nihao");
        try{
        nowClock.suspend();
        }
        catch(Exception e1) {
          e1.printStackTrace();
        }
    }

    void btn_Clean_actionPerformed(ActionEvent e) {
        jLabel1.setText("00:00:00");
        try{
        nowClock.suspend();
        }
        catch(Exception e1) {
          e1.printStackTrace();
        }
    }

void btn_Clock_actionPerformed(ActionEvent e) {
//        ClockThread nowClock=new ClockThread();
        nowClock.setLabel(jLabel1);
        if(nowClock.isAlive()) nowClock.resume();
        else  nowClock.start();
/*        jLabel1.setText(nowClock.repaint());
        Date now=new Date();
        String s;
        s=now.getHours()+":"+now.getMinutes() +":"+now.getSeconds();
        jLabel1.setText(s);*/
    }
}

class ClockThread extends Thread {
    private Thread clockTh=null;
    JLabel L;
    public  void  setLabel(JLabel L)
    {
      this.L = L;
    }
    /*
    public void start(){

        if(clockTh==null){
            //clockTh=new Thread(this,"ClockThread");
            clockTh.start();
        }

    }
    */
    public void run(){
        //Thread myThread=Thread.currentThread();
        //start();
        //System.out.println("currentThread = "+myThread);
        //System.out.println("this = "+this);
        //System.out.println("clockTh = "+clockTh);
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e){}
        L.setText("Zhangping");
    /*        try{
                Thread.sleep(1000);
            }catch(InterruptedException e){}
            L.setText("23:59:59");*/

        //while(clockTh==myThread){
          while  (true)
          {
            L.setText(repaint());
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){}
          }
           // //i//f(clockTh.getThreadGroup()!=myThread.getThreadGroup())
              //  System.out.println("Flase");
            //e//lse
               // System.out.println("True");
            //}
        }
    public String repaint(){
        String s;
        Date now=new Date();
        s=now.getHours() +":"+now.getMinutes()+":"+now.getSeconds();
        return s;
    }
}



    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 大侠,求助!!LINUX下TCP套接字编程中客户端出现段错误
  • adsv1.2 for arm 新建工程出现问题?望大侠帮帮忙
  • 求助:ubuntu9.10下安装ns2.30出现问题,那位大侠帮帮忙!
  • 各位大侠帮帮忙。请帮我看看这是怎么回事。为什么一执行就出现错误?到底错在那里?
  • 求助:驱动编译出现的问题!小弟折腾好几天了,始终不得其解,各位大侠指点一下 谢谢
  • ★★拜托哪位大侠救命:JSP调试出现以下错误,没办法解决了阿!!(只要有启发的回复就有分,决不食言)★★
  • 修改了linux内核的tcp.c tcpsendmsg()函数 出现奔溃 求大侠帮助!!满屏幕的跑00000 元芳你怎么看????
  • 各位大侠,本人是新手上路,经理马上让做palm,我可是从0学起!有没有那位大侠给我介绍一点经验和好的技术论坛,谢了!
  • hotenM(CMP)大侠等EJB大侠进来!小问题请教!
  • 请教大侠,怎么选开发学习板?
  • 大侠请援手:有关动态连接库。
  • 写过BBS的大侠看过来!
  • 请各位大侠推荐两本Solaris的经典书籍?
  • 各位大侠,求助Linux下关机
  • 大侠们,我是新手,请教怎么判断串口是否有连接
  • 想各位大侠求教了,为什么我的jdbc驱动不支持absolute这个方法呢?
  • 数据恢复, 求大侠帮助, 急等!!!!!!!
  • 各位大侠,给点意见
  • JSP+数据库文本存取中的换行问题,请教大侠
  • 各位大侠:lock 命令怎么用?
  • 关于session方面的问题!哪位大侠帮帮忙!?
  • 各位大侠,那有JBuilder书down? 那有PDF格式文件的阅读器?
  • 各位大侠,还有一问题请教,如下:
  • 请“路人甲”大侠进来来看看。。。多谢了。。
  • 请各位大侠介绍几本 UNIX 的书籍
  • solaris,请大侠帮忙!
  • 哪位大侠能提供一些Motif的电子书?


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3