首页 > 解决方案 > 使用方法将文本附加到 JFrame 组件

问题描述

我正在创建一个程序,该程序通过JTextArea使用函数中的文本。我们称之为parse()方法。强制需要一个参数是要解析的文本,然后是另一个参数是JFrame组件。

parse()函数遍历每个字符并使用 if 语句确定操作过程。我还有一个数组,可以存储来自代码其他部分的整数。在其中一个 if 语句中,我需要从数组的值中附加 ASCII 字符。

这是我用于该方法的一些代码:

//Let's imagine the array has this values 
int arr[] = {0,72,101,108,108,111,32,119,111,114,108,100,0}; 

//This variable is the index of the array.
int index = 5;

public void parse(String str, Component comp) {
    for (int i = 0; i < str.length; i++) {
        if (str.charAt(i) == 'j') {
            index++;
        } else if (str.charAt(i) == 'l') {
            index--;
        } else if (str.charAt(i) == 'z') {
            arr[index]++;
        } else if (str.charAt(i) == 'x') {
            arr[index]--;
        } else if (str.charAt(i) == 'p') {
            //Here I need to append the ASCII char from the value of the array 
            //where the index variable is at. What I did is this:
            comp.[IDK WHAT SHOULD BE HERE OR IF IT USES PARENTHESIS OR !]String.valueOf((char)arr[index]);
        }
   }
}

在前面的代码中,应该附加到组件的 char 将是一个 'o'。我该怎么做?

我发现它可能的唯一方法是指定像

public void parse(String str, JTextArea ta) {}

然而,我不想对框架的所有组件都这样做。

标签: javaswingcastingappendjtextarea

解决方案


推荐阅读