请帮忙修改代码!(有关多线程)

forjavacrazy 2009-07-15 06:18:03
大家好,请高手帮忙修改下代码,我的原意是设计一个闹钟程序,输入时间,在时间到后,时钟的前景和背景颜色会交替变色。但是我用了两个方法都没法做到,程序也未报错,请高手帮忙修改下!谢谢!
第一种方法代码:
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);
}
}
}
}

}


}








...全文
38 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
forjavacrazy 2009-07-16
  • 打赏
  • 举报
回复
3楼的哥们,谢谢你的意见。但是有关您的第二,三点不妨看下我第二种方法,虽然有关数据同步的错误更多,但是我却也做到了时间更新。有关你的第一点,我也试了,没用!不妨帮忙把代码改下,更能明白你的思路。谢谢!
jasonchain 2009-07-15
  • 打赏
  • 举报
回复
错误挺多的:
1) run方法里只把循环放到else里面了,应该把循环放在最外面。

2) 你的gc都没有初始化

3) 判断时间是否相等的时候,你的gc只是初始化的时候的时间,没有变化。

heartlesstoanyone 2009-07-15
  • 打赏
  • 举报
回复
把你的更新界面显示的代码放在SwingInvokeLater中运行,在其中调用组件的repait方法?
forjavacrazy 2009-07-15
  • 打赏
  • 举报
回复
没人回答吗?

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧