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

Action Listener doesn't change set variable to other value

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

单击“发送”按钮时,变量 OKpressed 应更改为 true,并且在函数 getUserInput 中它应识别出它已更改,但它没有.. 它的表现就像它仍然说假.. 我尝试在 Send 中打印出有效,所以问题只是 functiong 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);
    }
}
}

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

 while (t.OKPressed == false){}

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

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

应该可以。它与这里描述的更好的问题有关:Threads: Busy Waiting - Empty While-Loop,在 post 中是重复的。