繁体   English   中英

如何在JScrollPane中更新JTextArea的文本?

[英]How do I update the text of a JTextArea inside of a JScrollPane?

正如问题所指出的,我在更新JScrollPane内部的JTextArea的文本时遇到了麻烦。

我能够从JScrollPane的类型转换getView()中获取文本。 但是,我尝试了以下更新JTextArea的方法。

((JTextArea)(chatWindow.getViewport().getView())).setText("Hello!");

其中chatWindow是JScrollPane。

我已经尝试过了

chatWindowInsert.setText(processMessage()); 

其中chatWindowInsert是JScrollPane中的JTextArea

不幸的是,这两个工作都没有。

我没有任何例外或悬空。

协助将不胜感激!

这是我完整的代码。 如果我违反了一百万种编程实践,请原谅我。

public class ChatterBotClient extends JFrame{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private static ChatterBotFactory chatterBotFactory;
private static ChatterBotSession chatterBotSession;
private static ChatterBot chatterBot;
public static JScrollPane chatWindow;
public static JTextField userInput;
public static JTextArea chatWindowInsert;
public ChatterBotClient() 
{
    try{
    chatterBotFactory = new ChatterBotFactory();
    chatterBot = chatterBotFactory.create(ChatterBotType.CLEVERBOT);
    chatterBotSession = chatterBot.createSession();
    }catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("Error :O");
        JOptionPane.showMessageDialog(null,
                "There has been a problem initializing the Bot. Please restart.");
    }

    initUI();
}

public void initUI()
{
    //Define the mainPanel that everything goes into in the JFrame
    JPanel mainPanel = new JPanel(new BorderLayout());
    //Define the child panels of "mainPanel"
    JPanel infoPanel = new JPanel(new FlowLayout());
    JPanel chatHistoryPanel = new JPanel(new FlowLayout());
    JPanel userInputAndButtonPanel = new JPanel(new FlowLayout());
    setTitle("ChatterBot Chat Client");
    setSize(600,300);
    setResizable(false);

    //Define each component
    //Set properties of each component
    JLabel infoLabel = new JLabel("Welcome to my Cleverbot Client! Please enjoy! :D");
        infoLabel.setPreferredSize(new Dimension(550, 20));
        infoLabel.setHorizontalTextPosition(SwingConstants.CENTER);

    chatWindowInsert = new JTextArea();
        chatWindowInsert.setWrapStyleWord(true);

    chatWindow = new JScrollPane(chatWindowInsert);
        chatWindow.setPreferredSize(new Dimension(500,225));
        chatWindow.setPreferredSize(chatWindow.getPreferredSize());


    userInput = new JTextField();
        userInput.setPreferredSize(new Dimension(250, 25));

    JButton enterBtn = new JButton("Send");
        enterBtn.setPreferredSize(new Dimension(75, 25));

    //Add each component to the required panels
    infoPanel.add(infoLabel);

    chatHistoryPanel.add(chatWindow);

    userInputAndButtonPanel.add(userInput, BorderLayout.CENTER);
    userInputAndButtonPanel.add(enterBtn, BorderLayout.EAST);

    //Add the child panels to the mainPanel
    mainPanel.add(infoPanel, BorderLayout.NORTH);
    mainPanel.add(chatHistoryPanel, BorderLayout.CENTER);
    mainPanel.add(userInputAndButtonPanel, BorderLayout.SOUTH);

    //Now, add the appropriate listeners to your components
    enterBtn.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("You clicked me! :D");
            try {
                processMessage();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Something went wrong when you pressed the button! :O");
            }
        }
    });

    userInput.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0)
        {
            chatWindow = new JScrollPane(new JTextArea(processMessage()));

        }
    });

    //Tell the JFrame to display what we've made!
    add(mainPanel);     

}

public static String processMessage()
{
    try {
        String completeMessage = chatWindowInsert.getText();
        completeMessage.concat("You: " + userInput.getText() + "\n");
        String response = chatterBotSession.think(userInput.getText());
        completeMessage.concat("Bot: " + response + "\n");
        userInput.setText("");
        return completeMessage;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "ERROR";
    }
}

public static void main(String[] args)
{

    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            ChatterBotClient cli = new ChatterBotClient();
            cli.setVisible(true);
        }
    });
}

}

您声明:

正如问题所指出的,我在更新JScrollPane内部的JTextArea的文本时遇到了麻烦。

我能够从JScrollPane的类型转换getView()中获取文本。 但是,我尝试了以下更新JTextArea的方法。

((JTextArea)(chatWindow.getViewport().getView())).setText("Hello!");


为什么要经历所有这些易碎的代码体操? 创建一个类级别的JTextArea实例字段会更简单,更安全,更轻松,然后在JScrollPane中的GUI中显示它,然后简单地在该实例上获取文本或设置文本。 没有混乱,没有大惊小怪,没有错误。

如果您当前的程序无法做到这一点,那么您对我们的建议还不够多,否则您将需要告诉我们更多信息。


编辑
回复您的评论:

具有同时引用JTextArea和JTextField的变量(只是为了弄清楚这一点),但是即使我使用上述适当的方法之一更改了任何一个变量,JTextArea内部的内容仍然不会改变。 除非我不明白你在说什么?

这表明,尽管您可能正在使用正确的变量,但您可能正在使用错误的引用。 也许您正在使用的变量没有保存在当前显示的GUI中。

但这只是SWAG工作(愚蠢的野驴猜测工作)而已。 请不要强迫我们猜测-编辑您的原始文章并向我们展示您的代码(最好是sscce) ,向我们展示如何获取这些变量的句柄以及如何知道它们属于所显示的 gui。


编辑2
关于您的最新代码。 让我们看一下这一行:

public static JTextArea chatWindowInsert;
  1. 该字段不应为静态字段,对此我100%可以肯定。
  2. 您在哪里将此字段最终引用的任何对象添加到GUI? 在您发布的代码中,我找不到任何地方。

编辑3

我现在看到,您将完全不同的JTextField放入了JScrollPane中,再也没有将其放入GUI了,并且所有这些操作都可以在所有位置进行!

userInput.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        chatWindow = new JScrollPane(new JTextArea(processMessage()));

    }
});

建议:

  1. 不要这样
  2. 每次激活此操作侦听器时,请勿创建新的JTextArea和新的JScrollPane。
  3. 不要创建组件,而只是让它们挂在外面腐烂,不要在GUI中放置关键组件。
  4. 而是在构造GUI时将JTextArea插入GUI。 只有一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM