java - 使用方法将文本附加到 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) {}
然而,我不想对框架的所有组件都这样做。
解决方案
推荐阅读
- ignite - Apache 点燃为什么 odbc/jdbc 驱动被初始化
- swift - 将字符串转换为字典?
- python - Python:如何要求字符串作为输入而不是特定值
- python - 调用方法上的 Python 错误处理
- node.js - PassportJS 和使用 postgres 创建用户
- json - 使用 EVReflection 的 JSON 解析非常慢,具体取决于 iOS 设备
- mysql - 为什么睡眠功能在算术运算中执行?
- amazon-web-services - IAM 政策允许 SMS 发布但不拒绝所有 SNS
- python-3.x - 尽管使用不同的 random_state 值,为什么 stratifiedkfold 会生成相同的拆分?
- android - 以编程方式下载和启动应用程序时解析错误