2015-05-14 67 views
0

我正在为聊天编写GUI,并且我遇到了问题,我似乎找不到解决方案。动作监听器不会将设置变量更改为其他值

当按钮发送点击可变OKpressed应改为true和功能getUserInput应该承认它改变,但它不.. 它像个还在说假的.. 我试着打印出的发送工作,这样的问题仅仅是functiong getUserInput不承认变量改变

任何帮助是appreciated..Here是我不能将所有其他类,所以你可以启动它的代码 ,但一切工作除了问题如上所述

public class Chat extends Process { 

public static class myFrame extends JFrame{ 

/** Creates a new instance of myFrame */ 
private JTextArea ChatBox=new JTextArea(10,45); 
private JScrollPane myChatHistory=new JScrollPane(ChatBox,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
private JTextArea UserText = new JTextArea(5,40); 
private JScrollPane myUserHistory=new JScrollPane(UserText,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
private JButton Send = new JButton("Send"); 
private JTextField User=new JTextField(20); 
private String ServerName; 
private String UserName; 
boolean OKPressed = false; 
String poruka; 
public myFrame() { 
    setResizable(false); 
    setTitle("Client"); 
    setSize(560,400); 
    Container cp=getContentPane(); 
    cp.setLayout(new FlowLayout()); 
    cp.add(new JLabel("Chat History")); 
    cp.add(myChatHistory); 
    cp.add(new JLabel("Chat Box : ")); 
    cp.add(myUserHistory); 
    cp.add(Send); 
    cp.add(User); 

    Send.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      poruka=(String)UserText.getText(); 
      OKPressed = true; 

     } 
    }); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true); 


    } 
} 

static myFrame t=new myFrame(); 

public Chat(Linker initComm) { 
    super(initComm); 
} 
public synchronized void handleMsg(Msg m, int src, String tag){ 
    if (tag.equals("chat")) { 
     System.out.println("Message from " + src +":"); 
     System.out.println(m.getMessage()); 
     t.ChatBox.append(src + ":" + m.getMessage() + "\n"); 

    } 
} 
public String getUserInput() throws Exception { 
    while (t.OKPressed == false){} 
    String chatMsg=t.poruka; 
    return chatMsg; 
} 
public IntLinkedList getDest(BufferedReader din) throws Exception { 
    System.out.println("Type in destination pids with -1 at end:"); 
    System.out.println("Only one pid for synch order:"); 
    IntLinkedList destIds = new IntLinkedList(); //dest for msg 
    StringTokenizer st = new StringTokenizer(din.readLine()); 

// StringTokenizer st = new StringTokenizer(t.poruka); 
    while (st.hasMoreTokens()) { 
     int pid = Integer.parseInt(st.nextToken()); 
     if (pid == -1) break; 
     else destIds.add(pid); 
    } 
    return destIds; 
} 
public static void main(String[] args) throws Exception { 

    String baseName = "Chat"; 
    int myId = Integer.parseInt(args[0]); 
    int numProc = Integer.parseInt(args[1]); 
    Linker comm = null; 
     comm = new CausalLinker(baseName, myId, numProc); 

    Chat c = new Chat(comm); 
    for (int i = 0; i < numProc; i++) 
     if (i != myId) (new ListenerThread(i, c)).start(); 
    BufferedReader din = new BufferedReader(
    new InputStreamReader(System.in)); 
    while (true){ 
     System.out.println(c.getUserInput()); 
     String chatMsg = c.getUserInput(); 
     if (chatMsg.equals("quit")) break; 
     t.ChatBox.append(myId + ": " + chatMsg +"\n"); 
     IntLinkedList destIds = c.getDest(din); 

      comm.multicast(destIds, "chat", chatMsg); 
    } 
} 
} 

回答

0

正如你写,我不能运行你的代码,因此它是一种猜测,但我认为这个问题是空的无限循环:

while (t.OKPressed == false){} 

,如果你添加任何东西里面,甚至:

while(t.OKPressed == false){ 
    System.out.println(); 
} 

它应该工作。它与例如这里更好地描述的问题相关联:Threads: Busy Waiting - Empty While-Loop,并且在后者中重复它。

+0

谢谢!我添加了Thread.sleep(500),它现在起作用了! –