在 JTextArea 中获取位置并使用计时器
Getting a position in a JTextArea and using a timer
我正在做一个学校项目,希望你们能帮助我。
我有这个:
JTextArea processTxt = new JTextArea();
当我按下这个按钮时,
JButton sortBtn = new JButton("SORT!");
这将在 processTxt
上显示。
public void sort(String type, String order, int size) {
int tempInt, int1, int2;
processTxt.append("Pass 0:\n");
for (int i=0; i<size; i++) {
if (type.equals("int")) {
processTxt.append(intList.get(i)+" ");
}
}
processTxt.append("\n");
for (int i=0; i<size-1; i++) {
int x=0;
processTxt.append("Pass "+(i+1)+": \n");
while (x<size-1) {
int y = x + 1;
if (type.equals("int")) {
int1 = intList.get(x);
int2 = intList.get(y);
if (order.equals("asc")) {
if (int1 >= int2) {
tempInt = int1;
intList.set(x, int2);
intList.set(y, int1);
}
}
else if (order.equals("desc")) {
if (int1 <= int2) {
tempInt = int1;
intList.set(x, int2);
intList.set(y, int1);
}
}
}
for (int z=0; z<size; z++) {
if (type.equals("int")) {
processTxt.append(intList.get(z)+" ");
}
}
processTxt.append("\n");
x++;
}
}
processTxt.append("Sorted:\n");
for (int i=0; i<size; i++) {
if (type.equals("int")) {
processTxt.append(intList.get(i)+" ");
}
}
}
我在想:
- 如何获取
int1
在文本区域的位置?
- 有什么方法可以使用计时器来延迟文本区域的输出吗?我已经用了
thread.sleep()
但它不起作用。
1) how could i get int1's position on the textarea;
除了对文本进行物理搜索外,您还可以在插入之前保持对文本插入位置的引用。
如果要在当前光标位置插入文本,则可以使用 JTextArea
的插入符号位置,否则,您将需要从要更新的 Document
中确定索引点.
2) is there any way that i could use a timer to delay the output on the textarea? i have used thread.sleep() but it wont work.
是的,但是请理解,这变得越来越复杂。
您可以使用 Swing Timer
,它将定期触发回调,允许您更新排序状态并更新 UI。这很有用,因为它会在事件调度线程的上下文中触发回调,从而可以安全地从内部更新 UI。
然而,它确实增加了复杂性,因为 Timer
充当一种伪循环。
您可以改用 SwingWorker
。这将允许您获取当前代码并 运行 它在 doInBackground
方法中,该方法在事件调度线程之外执行,允许您使用 Thread.sleep
在您需要的地方注入延迟希望他们不影响 UI.
问题是,您需要使用 SwingWoker
s publish
/process
方法将更新推送到 UI,这会断开您创建的时间将模型更改为它出现在 UI 中的时间,这可能会在确定给定更新应该发生的位置时出现问题(在您的第一个问题的上下文中)。
看看:
了解更多详情
我正在做一个学校项目,希望你们能帮助我。 我有这个:
JTextArea processTxt = new JTextArea();
当我按下这个按钮时,
JButton sortBtn = new JButton("SORT!");
这将在 processTxt
上显示。
public void sort(String type, String order, int size) {
int tempInt, int1, int2;
processTxt.append("Pass 0:\n");
for (int i=0; i<size; i++) {
if (type.equals("int")) {
processTxt.append(intList.get(i)+" ");
}
}
processTxt.append("\n");
for (int i=0; i<size-1; i++) {
int x=0;
processTxt.append("Pass "+(i+1)+": \n");
while (x<size-1) {
int y = x + 1;
if (type.equals("int")) {
int1 = intList.get(x);
int2 = intList.get(y);
if (order.equals("asc")) {
if (int1 >= int2) {
tempInt = int1;
intList.set(x, int2);
intList.set(y, int1);
}
}
else if (order.equals("desc")) {
if (int1 <= int2) {
tempInt = int1;
intList.set(x, int2);
intList.set(y, int1);
}
}
}
for (int z=0; z<size; z++) {
if (type.equals("int")) {
processTxt.append(intList.get(z)+" ");
}
}
processTxt.append("\n");
x++;
}
}
processTxt.append("Sorted:\n");
for (int i=0; i<size; i++) {
if (type.equals("int")) {
processTxt.append(intList.get(i)+" ");
}
}
}
我在想:
- 如何获取
int1
在文本区域的位置? - 有什么方法可以使用计时器来延迟文本区域的输出吗?我已经用了
thread.sleep()
但它不起作用。
1) how could i get int1's position on the textarea;
除了对文本进行物理搜索外,您还可以在插入之前保持对文本插入位置的引用。
如果要在当前光标位置插入文本,则可以使用 JTextArea
的插入符号位置,否则,您将需要从要更新的 Document
中确定索引点.
2) is there any way that i could use a timer to delay the output on the textarea? i have used thread.sleep() but it wont work.
是的,但是请理解,这变得越来越复杂。
您可以使用 Swing Timer
,它将定期触发回调,允许您更新排序状态并更新 UI。这很有用,因为它会在事件调度线程的上下文中触发回调,从而可以安全地从内部更新 UI。
然而,它确实增加了复杂性,因为 Timer
充当一种伪循环。
您可以改用 SwingWorker
。这将允许您获取当前代码并 运行 它在 doInBackground
方法中,该方法在事件调度线程之外执行,允许您使用 Thread.sleep
在您需要的地方注入延迟希望他们不影响 UI.
问题是,您需要使用 SwingWoker
s publish
/process
方法将更新推送到 UI,这会断开您创建的时间将模型更改为它出现在 UI 中的时间,这可能会在确定给定更新应该发生的位置时出现问题(在您的第一个问题的上下文中)。
看看:
了解更多详情