我的JTextField出了什么问题? (文字详情)

时间:2014-11-27 10:21:20

标签: java swing jtextfield

编辑:基本上,UI类将#login发送到客户端,客户端将其发送到服务器,服务器发回" @ login"到客户端表示登录成功,客户端发送" @ login"通过UI.display()

连接UI

抱歉语法/格式错误。

这是最相关的代码块,附带说明:

    else if (e.getSource() == this.loginButton)
    {               
        if (this.soundON == true)
        {
            this.clickSound.play();
        }   

        this.client.handleMessageFromClientUI("#login "+this.loginField.getText());
        this.client.handleMessageFromClientUI("#balance");
        this.client.setLoginID(this.loginField.getText());                      

        if (this.loggedIn)
        {
            this.loginButton.setEnabled(true);

            this.note.setText("Account Screen");
            this.status.setText("Logged in to: "+this.client.getLoginID());             

            this.mainDisplay.removeAll();           
            this.controlBar.removeAll();

            this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
            this.mainDisplay.add(this.title);                               
            this.mainDisplay.add(Box.createRigidArea(new Dimension(0,50)));
            this.mainDisplay.add(Box.createRigidArea(new Dimension(100,0)));
            this.mainDisplay.add(this.balanceLabel);                                                          
            this.mainDisplay.add(Box.createRigidArea(new Dimension(0,110)));

            this.controlBar.add(debitButton);
            this.controlBar.add(creditButton);
            this.controlBar.add(quitButton);

            this.repaint();                 
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Log-in Failed.", "alert", JOptionPane.ERROR_MESSAGE);
        }
    }



public void display(String msg)
{       
    if (msg.startsWith("@LOGIN"))
    {
        this.loggedIn = true;
    }
    else if (msg.startsWith("@BALANCE"))
    {
        this.balanceLabel.setText("Balance: $"+msg.substring(43));
        this.repaint();
    }
    else if (msg.startsWith("@ALERT"))
    {
        JOptionPane.showMessageDialog(null, msg.substring(7), "Server Message", JOptionPane.PLAIN_MESSAGE);
    }
    else
    {
        //JOptionPane.showMessageDialog(null, msg, "Server Message", JOptionPane.INFORMATION_MESSAGE); //For debugging
    }
}

这只是课程的一部分,其余部分:http://pastebin.com/3f99eFWk

我的问题在于loginButton案例中的actionPerformed(ActionEvent e)。一旦最初单击按钮,它总是会进入此块:

        else
        {
            JOptionPane.showMessageDialog(null, "Log-in Failed.", "alert", JOptionPane.ERROR_MESSAGE);
        }

然后,如果我再次单击该按钮,它将工作并转到if(this.loggedIn)案例块。执行此行时获得loggedIn = true:

this.client.handleMessageFromClientUI("#login "+this.loginField.getText());

反过来调用:

public void handleMessageFromClientUI(String message)
{
else
  { 
      try
      {
          sendToServer(message);
      }
      catch(IOException e)
      {
          clientUI.display("Could not send message to server.");
          //quit();
      }
  }
}

其中调用server.handleMessageFromClient(Object msg,Connection client),其中包含以下相关代码:

                try
                {
                    client.sendToClient("@LOGIN Logging in to account: "+tempStr);                          
                }
                catch (IOException e)
                { 
                    System.out.println("Failed to send message to client");
                }
                client.setInfo("loginID", tempStr);

我知道它没有发送,我已经测试过了。问题是为什么即使文本在文本字段中输入,第一次按下按钮时actionPerformed中的if语句也会失败,然后传递第二次?

0 个答案:

没有答案