我想知道为什么我在这个gui群聊的发送按钮没有打印我想要打算文本区域的消息,一切都编译而且没有错误所以我有点不知所措,任何帮助,将不胜感激。这是我在这里发表的第一篇文章,请原谅任何格式错误。我在下面附上了我的代码,感谢所有的帮助,谢谢!
package ChatGUI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class preDisplay extends Thread{
String username;
public void run() {
JFrame preFrame = new JFrame();
JButton enterChat = new JButton("Enter Chat");
JTextField chooseUserName = new JTextField("Enter desired username");
preFrame.add(chooseUserName, BorderLayout.CENTER);
chooseUserName.setBounds(15,20,250,50);
enterChat.setBounds(80,80,120,40);
preFrame.add(enterChat);
preFrame.setSize(300,200);
preFrame.setLayout(null);
preFrame.setVisible(true);
enterChat.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
username = chooseUserName.getText();
preFrame.setVisible(false);
}
});
}
}
package ChatGUI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SendMessageUI extends Thread{
public void run() {
JFrame frame = new JFrame();//create new frame
JButton sendButton = new JButton("send");
JTextField textField = new JTextField();
frame.add(textField, BorderLayout.CENTER);
textField.setBounds(15,20,250,50);
DisplayMessages display = new DisplayMessages();
preDisplay pre_disp = new preDisplay();
sendButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
display.textArea.append(pre_disp.username + ": " + " " + textField.getText() + '\n');
}
});
sendButton.setBounds(100,80,80,40);//x axis, y axis, width, height
frame.add(sendButton);//adding button in JFrame
frame.setSize(300,200);//300 width and 200 height
frame.setLayout(null);//using no layout managers
frame.setVisible(true);//show frame
}
}
package ChatGUI;
public class DisplayMessages extends Thread{
JTextArea textArea = new JTextArea();
public void run(){
JFrame frame2 = new JFrame();//create new frame
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set behavior when closed
frame2.setSize(400,300);//400 width and 300 height
frame2.setLayout(null);//using no layout managers
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);//show frame
textArea.setBounds(15,20,350,220);
textArea.setEditable(false);
frame2.add(textArea);
}
}
package ChatGUI;
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new preDisplay());
Thread t2 = new Thread(new SendMessageUI());
Thread t3 = new Thread(new DisplayMessages());
t1.start();
t2.start();
t3.start();
}
}
答案 0 :(得分:2)
您正在设置错误的DisplayMessages对象的文本。您正在创建一个新的未显示的文本并将文本发送到该文本,同时显示的文本不会发送到该文本。整个问题是理解Java对象/引用是什么以及设置正确引用的状态,而不是一些碰巧与感兴趣的类型相同的随机引用。
下面:
public class SendMessageUI extends Thread{
public void run() {
JFrame frame = new JFrame();//create new frame
JButton sendButton = new JButton("send");
JTextField textField = new JTextField();
frame.add(textField, BorderLayout.CENTER);
textField.setBounds(15,20,250,50);
DisplayMessages display = new DisplayMessages();
此DisplayMessages对象不是向用户显示的对象。
取而代之的是:
public static void main(String[] args) {
Thread t1 = new Thread(new preDisplay());
Thread t2 = new Thread(new SendMessageUI());
Thread t3 = new Thread(new DisplayMessages()); // ******
是实际显示的那个。
解决方法是研究Java对象的真正含义,对象的引用意味着什么,然后将感兴趣的真实对象传递到需要的位置,以便在需要时更改其状态。
存在许多不相关的问题,包括: