动作侦听器不会将set变量更改为其他值

时间:2015-05-14 13:26:47

标签: java user-interface jbutton distributed-computing

我正在为聊天编写GUI,但我遇到问题似乎无法找到解决方案。

当单击按钮发送时,变量OKpressed应该更改为true,并且在函数getUserInput中它应该识别它已更改但它没有... 它的表现仍然是假的.. 我尝试在发送中打印出来,所以问题只是函数getUserInput没有将变量识别为已更改

任何帮助都表示赞赏。这是代码 我无法附加所有其他类,所以你可以启动它,但除了上面提到的问题外,一切都正常工作

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);
    }
}
}

1 个答案:

答案 0 :(得分:0)

正如你所写,我无法运行你的代码,所以它有点猜测,但我认为问题是在空的无限循环中:

var newItems = _.map(items, function(item, key){
  item.name = key;
  return item;
});
console.log(newItems);

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

 while (t.OKPressed == false){}

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