Java 我的JTextField有什么问题?(详情见正文)
编辑:基本上UI类将登录发送到客户端,客户端将其发送到服务器,服务器将@login发送回客户端,指示成功登录,客户端通过UI.display将@login发送到UI 抱歉语法/格式错误 以下是最相关的代码块,并提供以下解释:Java 我的JTextField有什么问题?(详情见正文),java,swing,jtextfield,Java,Swing,Jtextfield,编辑:基本上UI类将登录发送到客户端,客户端将其发送到服务器,服务器将@login发送回客户端,指示成功登录,客户端通过UI.display将@login发送到UI 抱歉语法/格式错误 以下是最相关的代码块,并提供以下解释: else if (e.getSource() == this.loginButton) { if (this.soundON == true) { this.clickSo
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
}
}
这只是本课程的一部分,其余部分如下:
我的问题是loginButton案例中的ActionPerformedActionE事件。单击按钮时,它始终首先进入此块一次:
else
{
JOptionPane.showMessageDialog(null, "Log-in Failed.", "alert", JOptionPane.ERROR_MESSAGE);
}
然后,如果我再次单击该按钮,它将工作并转到if this.loggedIn case块。执行此行时获得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.handleMessageFromClientObject msg,连接客户端,其中包含以下相关代码:
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语句在第一次按下按钮时失败,即使文本是在textfield中键入的,第二次也会通过???那么,是什么将this.loggedIn设置为true?在哪里调用displayString msg,将loggedIn设置为true?@@MadProgrammer:如果向下滚动第一段代码,我不能在这里包含它,因为字符限制@@MihaiC:在客户端类,它没有显示,但基本上它从服务器类接收消息并将其抛出到这个类:public void handleMessageFromServerObject msg{clientUI.displaymsg.toString;}基本上UI类将登录发送到客户端,客户端将其发送到服务器,服务器将@login发送回客户端,表示成功登录,客户端将@login通过UI.displayhello发送到UI?还有人在吗?