当前位置: 技术问答>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;
/**
*
*
*
*
* @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;
}
}
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;
/**
*
*
*
*
* @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;
}
}
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.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。