请帮忙修改代码!(有关多线程)
大家好,请高手帮忙修改下代码,我的原意是设计一个闹钟程序,输入时间,在时间到后,时钟的前景和背景颜色会交替变色。但是我用了两个方法都没法做到,程序也未报错,请高手帮忙修改下!谢谢!
第一种方法代码:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class testcl6 implements ActionListener,Runnable
{ Frame f,f1;
Label b,b1,b2;
Panel p;
TextField t1,t2;
Button b3;
GregorianCalendar gc;
String y,w,m;
String t3,t4;
Thread t,nn;
public static void main(String[] args)
{
new testcl6();
}
public testcl6()
{ f=new Frame("test clock");
f.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);
}
}
);
b1=new Label("xiaoshi");
b2=new Label("fenzhong");
t1=new TextField();
t2=new TextField();
b3=new Button("shuru");
t1.addActionListener(this);
t2.addActionListener(this);
b3.addActionListener(this);
p=new Panel(new GridLayout(1,5));
p.add(b1);
p.add(t1);
p.add(b2);
p.add(t2);
p.add(b3);
b=new Label(gettime(),Label.CENTER);
b.setFont(new Font("Arial",Font.PLAIN,36));
f.add(p,BorderLayout.NORTH);
f.add(b,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
t=new Thread(this);
t.start();
nn=new Thread(new no());
}
public void actionPerformed(ActionEvent e)
{ t3=t1.getText();
t4=t2.getText();
System.out.println(t3+t4);
t1.setText("");
t2.setText("");
}
public void run()
{
if ((Integer.parseInt(t3)==gc.get(GregorianCalendar.HOUR_OF_DAY))&&(Integer.parseInt(t4)==gc.get(GregorianCalendar.MINUTE)))
{
try
{nn.join(60000);}
catch (InterruptedException e)
{ System.err.println(e);
}
}
else { while (true)
try {
Thread.sleep(1000);
b.setText(gettime());
}
catch (InterruptedException e)
{ System.err.println(e);
}
}
}
public String gettime()
{ GregorianCalendar gc=new GregorianCalendar();
return
gc.get(GregorianCalendar.HOUR_OF_DAY)+":"+
gc.get(GregorianCalendar.MINUTE)+":"+
gc.get(GregorianCalendar.SECOND);
}
public class no implements Runnable
{
public void run()
{ while (true)
{ try
{ Thread.sleep(1000);
b.setForeground(Color.green);
b.setBackground(Color.white);
b.setText(gettime());
Thread.sleep(1000);
b.setText(gettime());
b.setForeground(Color.red);
b.setBackground(Color.yellow);
}
catch (InterruptedException e)
{ System.err.println(e);
}
}
}
}
}
第二种方法代码:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class testcl7 implements ActionListener,Runnable
{ Frame f,f1;
Label b,b1,b2;
Panel p;
TextField t1,t2;
Button b3;
GregorianCalendar gc;
String y,w,m;
String t3,t4;
Thread t,nn;
public static void main(String[] args)
{
new testcl7();
}
public testcl7()
{ f=new Frame("test clock");
f.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);
}
}
);
b1=new Label("xiaoshi");
b2=new Label("fenzhong");
t1=new TextField();
t2=new TextField();
b3=new Button("shuru");
t1.addActionListener(this);
t2.addActionListener(this);
b3.addActionListener(this);
p=new Panel(new GridLayout(1,5));
p.add(b1);
p.add(t1);
p.add(b2);
p.add(t2);
p.add(b3);
b=new Label(gettime(),Label.CENTER);
b.setFont(new Font("Arial",Font.PLAIN,36));
f.add(p,BorderLayout.NORTH);
f.add(b,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
t=new Thread(this);
nn=new Thread(new no());
t.start();
nn.start();
nao();
}
public void actionPerformed(ActionEvent e)
{ t3=t1.getText();
t4=t2.getText();
System.out.println(t3+t4);
t1.setText("");
t2.setText("");
}
public void run()
{
while (true)
synchronized(this)
{
try {
Thread.sleep(1000);
b.setText(gettime());
}
catch (InterruptedException e)
{ System.err.println(e);
}
}
}
public void nao()
{if ((Integer.parseInt(t3)==gc.get(GregorianCalendar.HOUR_OF_DAY))&&(Integer.parseInt(t4)==gc.get(GregorianCalendar.MINUTE)))
{ try{
this.wait();}
catch (InterruptedException e)
{ System.err.println(e);
}
this.notify();
}
else { try
{
nn.wait();
}
catch (InterruptedException e)
{ System.err.println(e);
}
nn.notify();
}
}
public String gettime()
{ GregorianCalendar gc=new GregorianCalendar();
return
gc.get(GregorianCalendar.HOUR_OF_DAY)+":"+
gc.get(GregorianCalendar.MINUTE)+":"+
gc.get(GregorianCalendar.SECOND);
}
public class no implements Runnable
{
public void run()
{ while (true)
{
synchronized(this){try
{ Thread.sleep(1000);
b.setForeground(Color.green);
b.setBackground(Color.white);
b.setText(gettime());
Thread.sleep(1000);
b.setText(gettime());
b.setForeground(Color.red);
b.setBackground(Color.yellow);
}
catch (InterruptedException e)
{ System.err.println(e);
}
}
}
}
}
}