gpt4 book ai didi

java - 如何在按下按钮时更新我的​​ TextArea?

转载 作者:行者123 更新时间:2023-12-02 05:01:35 24 4
gpt4 key购买 nike

这是我的简单聊天 GUI 的代码,我希望用户可以单击“发送”按钮,对称为 MessageArea 的 TextArea 进行更改。

在 ButtonPress 事件中,如果按“发送”按钮,它会更改 MessageArea,更改 MessageArea 下面面板的颜色,重新验证并重新绘制。

单击“发送”后,面板会更改颜色,但 TextArea 不会更改文本。我做错了什么,如何解决这个问题?

public class BuildWindow extends JFrame implements WindowListener, MouseListener, KeyListener {

private TextArea MessageArea = null; //the chat window?
private TextField SendArea = null; // where you type in your message
private String Username = null; //username
private Button Send, Clear;
private final static String newline = "\n";
private Panel p = null;

BuildWindow(String s) {
super(s);

this.addWindowListener(this);
this.setSize(800,600);
this.setResizable(true);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MessageArea = new TextArea("little test");
MessageArea.setEditable(false);
this.add(MessageArea, "Center");
MessageArea.setFont(new Font("Arial", Font.PLAIN, 16));

p = new Panel();
p.setLayout(new FlowLayout());

SendArea = new TextField(30);
SendArea.addKeyListener(this);
SendArea.setFont(new Font("Arial", Font.PLAIN, 16));

p.add(SendArea);
p.setBackground(new Color(221,221,221));

Send = new Button("Send");
Send.addMouseListener(this);
p.add(Send);

Clear = new Button("Clear");
Clear.addMouseListener(this);
p.add(Clear);

this.add(p, "South");
this.setVisible(true);
SendArea.requestFocus();
}

public void mouseClicked(MouseEvent arg0) {
if (arg0.getSource() == Send) {
MessageArea = new TextArea("TEST222"); //what i want to see change but dont
p.setBackground(new Color(011,011,011));// what i do see change

revalidate();
repaint();
}
if (arg0.getSource() == Clear) {
//add to list
}
}

最佳答案

用途:

MessageArea.setText("TEST222");

相反。你之前的线路,

MessageArea = new TextArea("TEST222");

创建一个全新的TextArea。由于它没有添加到容器中,因此它是不可见的。 (旧的仍然可见,因为你没有做任何使它不可见的事情)

关于java - 如何在按下按钮时更新我的​​ TextArea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28228912/

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