2015-05-14 36 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),它現在起作用了! –