[英]ActionListener invoke while loop method
我添加了一個ActionListener,它創建了一個新的聊天JFrame,可以從服務器發送和接收消息。 這是ActionPerformed()方法中的代碼
BananaChat chat = new BananaChat(name, password, IP, port, status);
try {
chat.chatting();
} catch (Exception e) {
showInfo("fail");
}
因此它創建了一個新的聊天框架,如果我不調用chat.chatting()方法,則可以正常地將消息發送到Server,但是無法從服務器接收消息。 因此,我必須調用此方法,因為如果它確實發送了消息,則需要繼續監聽來自服務器的消息。
這是chatting()的代碼
String line = null;
try {
while ((line = in.readLine()) != null) {
if (line.equals("your user name is already registered") || line.equals("user name doesn't exist") || line.equals("wrong password")) {
showMessage(line);
break;
}
showMessage(line);
}
} catch (IOException e) {
showMessage("can not receive the message");
}
這是while循環。 如果創建我的聊天框架並在main方法中調用此方法,則它可以工作,但是如果我在ActionListener中創建該聊天框架,則它會卡住。 看來ActionListener不能有一個while循環,它根本不會結束。
我不知道如何解決,是否有更好的方法通過登錄界面創建新的聊天界面?
正如kiheru所建議的那樣 ,在后台線程中運行您的聊天代碼可能是一個好主意-例如,使用SwingWorker
(專門用於Swing GUI)。 在對BananaChat
類進行一些猜測BananaChat
,一個非常簡單的(單向)聊天程序可能如下所示:
主類:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Chat {
private int nextPort = 80;
public static void main(final String[] args) {
new Chat().launchGui();
}
private void launchGui() {
final JFrame frame = new JFrame("Stack Overflow: chat with multiple servers");
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
final JButton startChatButton = new JButton("Start a new chat");
startChatButton.addActionListener(actionEvent -> {
final BananaChat bananaChat = new BananaChat("user name", "password",
"192.0.0.1", String.valueOf(nextPort), "???");
nextPort++;
bananaChat.startChatting();
});
panel.add(startChatButton);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
BananaChat
類:
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class BananaChat extends JFrame {
private final String userName;
private final String password;
private final String IP;
private final String port;
private final String status;
private final Random dummyServerMessages;
private final JTextArea messagesTextArea;
public BananaChat(final String userName, final String password, final String IP,
final String port, final String status) {
super("Chat with " + IP + ":" + port);
this.userName = userName;
this.password = password;
this.IP = IP;
this.port = port;
this.status = status;
dummyServerMessages = new Random(123456);
messagesTextArea = new JTextArea();
add(new JScrollPane(messagesTextArea));
setBounds(1000, 100, 400, 200);
setVisible(true);
}
public void startChatting() {
final SwingWorker chatWorker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() {
while (!isCancelled()) {
final String message = getMessageFromServer();
publish(message + "\n");
pause(1000);
}
return null;
}
@Override
protected void process(final List<String> messages) {
messages.forEach(messagesTextArea::append);
}
};
chatWorker.execute();
}
// Generate silly random messages. Can be replaced by a call to in.readLine().
private String getMessageFromServer() {
final String message;
if (dummyServerMessages.nextInt(6) < 2)
message = "Hello";
else
message = "Silence";
return message + " from " + userName + "@" + IP + ":" + port;
}
private void pause(final int milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
public String getPassword() {
return password;
}
public String getStatus() {
return status;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.