gpt4 book ai didi

Java - invokeLater 将不会运行

转载 作者:行者123 更新时间:2023-11-30 03:49:53 25 4
gpt4 key购买 nike

我按照教程创建了一个信使。我正确地输入了代码,并且我对所有内容的作用有相当基本的了解。

尽管如此,我最终并没有得到相同的结果。代码如下:

<小时/>
public class Server extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;

//constructor
public Server(){
super("Coffee Messenger");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.SOUTH);
chatWindow = new JTextArea();
add(new JScrollPane());
setSize(300,150);
setVisible(true);
}
//set up and run the server
public void startRunning(){
try{
server = new ServerSocket(6789, 100);
while(true){
try{
//connect and have conversation
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}finally{
closeCrap();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException{
showMessage("Waiting for someone to connect...\n");
connection = server.accept();
showMessage("Now connected to " + connection.getInetAddress().getHostName());
}
//get stream to send and receive data
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup! \n");
}
//during the chat conversation
private void whileChatting() throws IOException{
String message = "You are now connected!";
sendMessage(message);
ableToType(true);
do{
//have conversation
try{
message = (String) input.readObject();
showMessage("\n " + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n idk wtf that user sent");
}
}while(!message.equals("CLIENT - END"));
}
//close streams and sockets (application)
private void closeCrap(){
showMessage("\n Closing connections...\n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send message to client
private void sendMessage(String message){
try{
output.writeObject("SERVER - " + message);
output.flush();
showMessage("\nSERVER - " + message);

}catch(IOException ioException){
chatWindow.append("\n ERROR: Cannot send message.");
}
}
//updates chatWindow
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(text);
}
}
);
}
//sets the ability to edit the textfield
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
}
__
<小时/>

当我从主方法启动应用程序时,字符串“等待某人连接”(来自 waitForConnection 方法)不会显示。我相信问题出在 showMessage 方法中。难道是我用错了?如果我用简单的 system.out.println(); 替换 invokeLater 方法,项目将完全按计划运行。

抱歉,我有点缺乏经验,所以这可能非常简单。预先非常感谢您。

(感谢 thenewboston 制作这些教程)

最佳答案

这里:

chatWindow = new JTextArea();
add(new JScrollPane());

您的 GUI 正在创建一个 JTextArea、chatWindow,但将其添加到没有显示它的内容中,而您的 GUI 显示了一个空的 JScrollPane。如果有就更好了

chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));

这样发送到 JTextArea 的文本就有机会被显示。

关于Java - invokeLater 将不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24717853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com