现在在做一个类似QQ的程序,用UDP通信,在写界面的时候遇到了个问题:主类设置了一个isLog(bloolean)变量,(通过线程ListenThread(内部类)来监听服务器发来的消息,如果登陆则设置isLog = ture).按钮jbOnline根据isLog的值显示在线情况,现在的情况是无论isLog是否为false,jbOnline总是显示为不在线.
    恳求高手帮忙.public class MainFrame extends JFrame implements ActionListener{ BorderLayout borderLayout1;
BorderLayout borderLayout2; 
JLabel jLabel1;
JPanel jPanel1; 
JPanel jPanel2; 
JButton jbSerarch;                    //菜单按钮
JButton jbOnline;                     //标志在线的按钮
JButton[] JBFriends = null;           //好友按钮
JScrollPane jscrollPane;

private InetAddress add = null;
private User[] friends = null;        //好友数组
private boolean isLog;                //在线标志
private String name = null;           //好友昵称
private int userNo;                   //用户编号
private String pw;                    //用户密码
private DatagramSocket socket = null; //客户端接受数据端口
private byte[] buf = new byte[1000];
private DatagramPacket dp = new DatagramPacket(buf, buf.length); //接收的数据报
private Hashtable htonlineFriends = new Hashtable();             //关键字为No,Object为IP
private Vector[] vmessages = null;                               //存储每个好友发送过来的消息
private boolean isReceived[] = null;                             //标志各好友信息发送情况的标志
private ThreadGroup tg = new ThreadGroup("Client");
private Thread listen_thread;
public MainFrame(String userNo, String pw) {
this.userNo = Integer.parseInt(userNo);
this.pw = pw;
friends = CommonFunction.readFile(MacroDefine.FRIENDS_FILE_NAME);
try {
jbInit();                                        //绘制界面
socket = new DatagramSocket(MacroDefine.CPORT);  //打开端口
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
borderLayout1 = new BorderLayout();
jLabel1 = new JLabel();
jPanel1 = new JPanel();
jPanel1.setBorder(BorderFactory.createEtchedBorder()); jbSerarch = new JButton();
borderLayout2 = new BorderLayout();
jbOnline = new JButton();

int friendscount = 0;       //好友个数

this.setSize(new Dimension(60, 300));
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().setBackground(Color.lightGray);

jLabel1.setSize(50,10);
jLabel1.setFont(new Font("宋体",Font.PLAIN,13));
jLabel1.setOpaque(true);
// jLabel1.setBackground(Color.white);
jLabel1.setText("    我的好友");
jbOnline.setText("掉线");
        jbOnline.setPreferredSize(new Dimension(52,25));
        jbOnline.setFont(new Font("宋体",Font.PLAIN,9));
jbSerarch.setText("菜单");
jbSerarch.setPreferredSize(new Dimension(52,25));
jbSerarch.setFont(new Font("宋体",Font.PLAIN,9));
jbSerarch.addActionListener(this);
jPanel1.setLayout(borderLayout2); this.getContentPane().add(jLabel1, BorderLayout.NORTH);
this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(jbSerarch, BorderLayout.WEST);
jPanel1.add(jbOnline, BorderLayout.EAST);

jPanel2 = new JPanel();
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBackground(Color.lightGray);
jPanel2.setLayout(new GridLayout(50, 1));
this.getContentPane().add(jPanel2, BorderLayout.CENTER);

paintFriends(); //添加好友按钮


if (friends != null) {
friendscount = friends.length;
}
isReceived = new boolean[friendscount];
vmessages = new Vector[friendscount];
for (int j = 0; j < friendscount; j++) {
vmessages[j] = new Vector();
}
         if (isLog) {
jbOnline.setForeground(Color.pink);
jbOnline.setText("在线");
}

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}    /**
     * 添加好友按钮
     *
     */
private void paintFriends() {
int count = 0;           //好友个数
if (friends != null) {
count = friends.length;
}
JBFriends = new JButton[count];
for (int s = 0; s < count; s++) {
name = friends[s].getID();
if (name != null) {
JBFriends[s] = new JButton(name);
JBFriends[s].addActionListener(this);
jPanel2.add(JBFriends[s]);
}
this.getContentPane().add(jPanel2, BorderLayout.CENTER);
jscrollPane =
new JScrollPane(
jPanel2,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
this.getContentPane().add(jscrollPane, BorderLayout.CENTER);
}
jPanel2.validate();
//jPanel2.repaint();
}
    
//监控上线请求响应的线程
class ListeningOnLine_thread extends Thread {
public ListeningOnLine_thread(ThreadGroup tg, String name) {
super(tg, name);
}
public synchronized void run() {
for (int s = 0; s < 5 && isLog == false; s++) {
Object data = new Object();             //接收的数据
try {
System.out.println("客户端:监听客户端端口:" + MacroDefine.CPORT);
socket.receive(dp);                 //监听服务器端口,看是否有数据接收到
byte[] buf = dp.getData();
data = CommonFunction.getObject(buf);
System.out.println("客户端:接收一个数据报"+data.getClass().toString());
} catch (IOException e) {
return;
}
//if(data==null) continue;
if (data instanceof OnLineResponse) {
System.out.println("客户端:接收一个上线响应数据包");
OnLineResponse olp = (OnLineResponse) data;
Vector fs = olp.friends;              //好友
User[] self = new User[]{olp.self};                 //个人信息
User[] friends = new User[fs.size()];
Vector vfriends = new Vector();
Vector vself = new Vector();
//System.out.println("好友 个数 :"+fs.size());
for (int i = 0; i < fs.size(); i++) {
//System.out.println(((User)fs.elementAt(i)).getNo());
friends[i] = (User) fs.elementAt(i);
}
vfriends.add(friends);
vself.add(self);
CommonFunction.writeFile(vfriends,MacroDefine.FRIENDS_FILE_NAME);          //更新本机好友信息
CommonFunction.writeFile(vself,MacroDefine.SELF_FILE_NAME);                //更新本机个人信息
isLog = true;                         //设置其上线
notifyAll();
CommonFunction.readFile(MacroDefine.FRIENDS_FILE_NAME);
try {
jbInit();
//paintFriends();
setOnlineFriends();
System.out.println("客户端:isLog=" + isLog);
//*************************下面两行总是没用,SOS
jbOnline.setText("在线");
jbOnline.setForeground(Color.pink);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
}}