简体   繁体   English

循环未从actionPerformed中获取更新的布尔值

[英]Loop isn't getting updated boolean value from actionPerformed in java

I'm trying to create a simple chat program in java, using a frame. 我正在尝试使用框架在Java中创建一个简单的聊天程序。 The user can either host a server (by clicking the server button) or connect as a client (using the connect button). 用户可以托管服务器(通过单击服务器按钮),也可以作为客户端连接(使用连接按钮)。 the problem I'm having is the server button. 我遇到的问题是服务器按钮。 My goal is that when the Start server button is clicked, all of the other buttons and fields are disabled, and the startServer method should run. 我的目标是,当单击“启动服务器”按钮时,所有其他按钮和字段都被禁用,并且startServer方法应该运行。

The entire program is inside of a while (!kill) loop, and right now, it's just checking the isServer boolean. 整个程序处于while (!kill)循环中,而现在,它只是检查isServer布尔值。

    while (!kill)
    {
        if (isServer)
        {
            startServer();
        }
    }

isServer is set to true inside of actionPerformed when the startServerButton is pressed. 在按下startServerButton时,在actionPerformed中将isServer设置为true。

my problem is that startServer() is never run because the while (!kill) loop isn't getting the updated isServer value when startServerButton is clicked. 我的问题是startServer()永远不会运行,因为单击startServerButton时while(!kill)循环未获取更新的isServer值。

Here's my run method: 这是我的运行方法:

public void run()
{   
    conversationBox.appendText("Session Start.\n");
    inputBox.requestFocus();

    while (!kill)
    {
        if (isServer)
        {
            startServer();
        }
    }       

}

and here's my actionPerformed: 这是我执行的动作:

public void actionPerformed(ActionEvent e) throws NumberFormatException
{
    Object o = e.getSource();

    if (o == sendButton || o == inputBox)
    {
        if(inputBox.getText() != "")
        {
            clientSendMsg = inputBox.getText();
            inputBox.setText("");
        }
    }
    if (o == changeHost || o == hostField)
    {
        if (hostField.getText() != "" && hostField.getText() != host)
        {
            host = hostField.getText();
            conversationBox.appendText("Host changed to " + host + "\n");
        }
    }
    if (o == changePort || o == portField)
    {
        if (portField.getText() != "" && Integer.valueOf(portField.getText()) != port)
        {
            try
            {
                port = Integer.valueOf(portField.getText());
                conversationBox.appendText("Port changed to " + port + "\n");
            }
            catch(NumberFormatException up)
            {
                throw up; //blargh enter a real value
            }
        }
    }
    if (o == startServerButton)
    {
        isServer = true;
        startServerButton.enable(false);
        connectButton.enable(false);
        changeHost.enable(false);
        changePort.enable(false);
        sendButton.enable(false);
        hostField.enable(false);
        portField.enable(false);
        inputBox.enable(false);
    }
    inputBox.requestFocus();
}

Obviously, the program is nowhere near complete, but this is a pretty big hurdle to ignore, so I figured it would be best to get it fixed before chugging along. 显然,该程序还远未完成,但这是一个很大的障碍,可以忽略,因此我认为最好在进行之前对其进行修复。 Also, it should be noted that I have a new Thread(this).start(); 另外,应该注意,我有一个new Thread(this).start(); inside the Chat() object that creates the frame layout. 在创建框架布局的Chat()对象内部。 I'm not 100% sure how effective this is. 我不确定这有多有效。

In this line: if (isServer) 在这一行: if (isServer)

do you mean to say if (isServer == true) if when the button is clicked isServer is set to true? 您的意思是说if (isServer == true)如果单击按钮时isServer设置为true?

In my experience "if" statements should always have a condition, like if (variable1.equals(variable2)) or if (boolean == true) for example. 以我的经验, "if" statements应始终具有条件,例如if (variable1.equals(variable2))if (boolean == true)

Or the problem could be in your while (!kill) loop. 或问题可能出在您的while(!kill)循环中。 In that case just take out the if statement and see if the method runs then. 在这种情况下,只需取出if语句,然后查看该方法是否随后运行。

Volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory. Java中的Volatile关键字用作Java编译器和Thread的指示符, 它们不缓存此变量的值,而始终从主内存中读取它。 So if you want to share any variable in which read and write operation is atomic by implementation eg read and write in int or boolean variable you can declare them as volatile variable. 因此,如果您想共享实现中原子的读写操作所涉及的任何变量,例如int或boolean变量中的读写,则可以将它们声明为volatile变量。

Here is a link: 这里是一个链接:

Explanation about volatile 关于挥发物的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM