gpt4 book ai didi

Java添加类型编写器效果,不会停止整个程序

转载 作者:行者123 更新时间:2023-12-02 11:19:34 26 4
gpt4 key购买 nike

我已经考虑了一段时间来添加一个仅延迟 GUI 文本区域的系统,但没有成功,到目前为止我有这个:

    public void msg2(String msg) {
for (int i = 0; i < msg.length(); i++) {
mainTextController.append(Character.toString(msg.charAt(i)));
try {
Thread.sleep(45);
} catch (Exception ex) {
ex.printStackTrace();
}
}

mainTextController.append("\n");
}

这里的问题是,每当我运行这个方法时,它都会起作用,并且打字机效果也存在,但是,整个程序都会停止,直到 sleep 方法结束。

我尝试过 BlockingQueue 但显示了相同的结果。

我不想被灌输,只是想了解如何克服这个问题。

这是整个类(class):

package gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class LabyrinthGUI {

protected Shell shell;
private Text mainTextController;

/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
LabyrinthGUI window = new LabyrinthGUI();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window.
*/
Display display;
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
*/
Button btnGoNorth;

protected void createContents() {
shell = new Shell();
shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
shell.setSize(600, 700);
shell.setText("Murat's Labyrinth - v0.1");

mainTextController = new Text(shell, SWT.READ_ONLY | SWT.WRAP);
mainTextController.setDragDetect(false);
mainTextController.setDoubleClickEnabled(false);
mainTextController.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_ARROW));
mainTextController.setEditable(false);
mainTextController.setFont(SWTResourceManager.getFont("System", 9, SWT.NORMAL));
mainTextController.setForeground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
mainTextController.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
mainTextController.setBounds(10, 10, 564, 535);

btnGoNorth = new Button(shell, SWT.BORDER);
btnGoNorth.setSelection(true);


btnGoNorth.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
msg2("You clicked here");
}
});

btnGoNorth.setGrayed(true);
btnGoNorth.setTouchEnabled(true);
btnGoNorth.setFont(SWTResourceManager.getFont("System", 9, SWT.NORMAL));
btnGoNorth.setBounds(227, 551, 75, 25);
btnGoNorth.setText("Go North");

Button btnGoSouth = new Button(shell, SWT.BORDER);
btnGoSouth.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
}
});
btnGoSouth.setText("Go South");
btnGoSouth.setFont(SWTResourceManager.getFont("System", 9, SWT.NORMAL));
btnGoSouth.setBounds(227, 626, 75, 25);

Button btnGoWest = new Button(shell, SWT.BORDER);
btnGoWest.setText("Go West");
btnGoWest.setFont(SWTResourceManager.getFont("System", 9, SWT.NORMAL));
btnGoWest.setBounds(134, 587, 75, 25);

Button btnGoEast = new Button(shell, SWT.BORDER);
btnGoEast.setText("Go East");
//btnGoEast.setCursor(new Cursor());
btnGoEast.setFont(SWTResourceManager.getFont("System", 9, SWT.NORMAL));
btnGoEast.setBounds(328, 587, 75, 25);

}


public void lock() {
btnGoNorth.setEnabled(false);
}

public void unlock() {
btnGoNorth.setEnabled(true);
}

public void msg2(String msg) {
for (int i = 0; i < msg.length(); i++) {
mainTextController.append(Character.toString(msg.charAt(i)));
try {
Thread.sleep(45);
} catch (Exception ex) {
ex.printStackTrace();
}
}

mainTextController.append("\n");
}
}

最佳答案

the entire program halts until the sleep method is over.

不要使用Thread.sleep()。

相反,您应该使用 Swing Timer 来制作动画。

阅读 Swing 教程中关于 How to Use Swing Timers 的部分了解更多信息和工作示例。

关于Java添加类型编写器效果,不会停止整个程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50029235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com