本文介绍了JTextArea setText不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JTextArea中设置文本时遇到问题,我尝试了setText(我希望这样做)并追加.我不知道问题出在哪里,我有客户端服务器应用程序.我想把服务器发送的消息放在JTextField中,但是我无法在这里输入我的代码:

I have a problem with setting text in JTextArea, I tried setText(which I'd prefer) and append also. I do not know where the problem is, I got client-server app. I want to put message that server sends in JTextField but I am unable to here is my code:

正确接收消息的客户端代码:

client side code which is reciving the message properly:

try
    {


        Socket socket = new Socket("localhost", PORT);
        BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
        DataOutputStream output = new DataOutputStream(socket.getOutputStream());
        BufferedReader serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        output.writeBytes(outputString + "\n");
        inputString = serverInput.readLine(); // private String inputString
        mymodel.setTextArea(inputString); // this is not working
        System.out.println(inputString); // this is working
        socket.close();

    }
    catch...

setTextArea方法:

setTextArea method:

public void setTextArea(String string)
{
    MyPanel mypanel = new MyPanel();
    mypanel.textArea.setText(string); // debugger shows that the string contains message from server
}

由于setter方法不起作用,因此我已将textarea设置为公共区域,实际上,这一方法也不起作用.我不知道问题出在哪里,调试器也没有帮助我.

I have set textarea to public since setter method weren't working, actually this one isn't working also. I do not know where the problem is, and debugger isn't helping me either.

寻找答案

JTextTable代码:

JTextTable code:

textArea = new JTextArea(1, 30);
textArea.setEditable(false);
panel.add(textArea, c);

推荐答案

尝试通过getter获取访问权限.像

Try to get access through getter.Like

public JTextArea getTextArea()
{
     return jTextAreaField;
}

然后

getTextArea().append("ur text");

这篇关于JTextArea setText不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:12