gpt4 book ai didi

java - 手动按下 JButton 会产生与 doClick() 不同的结果?

转载 作者:行者123 更新时间:2023-11-29 07:49:41 25 4
gpt4 key购买 nike

当我的界面上的 JButton 按下按钮触发此事件监听器时,我遇到了问题:

if (event.getSource() == goButton)
{
MainLauncher.doProgram(assetsField.getText(), FileFinder.completePath(String.format("%s\\%s", modDirectoryPath, modList.getSelectedValue())), databaseField.getText());
}

运行以下代码:

public static void doProgram(final String baseGamePath, final String modPath, final String databasePath)
{
new Thread()
{
public void run()
{
System.out.println("Running: " + modPath + "\n");

reader = new MetaDataReader(databasePath);
reader.formConnection();

long start = System.currentTimeMillis();
long temp;

File cache = new File("RegisterCache.SC");
if (!cache.exists())
{
temp = System.currentTimeMillis();

start += System.currentTimeMillis() - temp;
System.out.println("Calculating number of files");
FileFinder baseGame = new FileFinder();
baseGame.calculateNumFiles(baseGamePath);
System.out.println(String.format("Loading %s base game files", baseGame.getNumFiles()));
gui.doProgressBar();
baseGame.findFiles(baseGamePath, true);
gui.killProgressBar();
System.out.println();

//load the base game assets, we want to move this to metadata later
System.out.println("Checking for base game reference errors");
new ReferenceDetector(Table.getRegister()).findReferences(true);
Table.serializeTable(cache); //write the database to cache, we can use it next time
}
else
{
System.out.println("Loading cache");
Table.unserializeTable(cache);
}

gui.doCommands(); //calls reset()!!! be careful with the temporary excludes!

System.out.println("Eliminating base game requires errors");
RequiresWhere.executeRequires(true);

temp = System.currentTimeMillis();

start += System.currentTimeMillis() - temp;
System.out.println("Calculating number of files");
FileFinder mod = new FileFinder();
mod.calculateNumFiles(modPath);
System.out.println(String.format("Loading %s mod files", mod.getNumFiles()));
gui.doProgressBar();
mod.findFiles(modPath, false);
gui.killProgressBar();

System.out.println();
System.out.println("Finding reference errors");
new ReferenceDetector(Table.getRegister()).findReferences(false);

System.out.println("Finding requires errors");
RequiresWhere.executeRequires(false);

System.out.println("Checks complete");
reader.killConnection();

System.out.println(String.format("Execution Time: %dms", System.currentTimeMillis() - start));
Table.reset();
}
}.run();
}

奇怪的是:

测试时,我没有将按钮添加到界面,所以在 main() 上,我调用了 goButton.doClick();,它触发了 doProgram(),并为我提供了我预期的结果是这样的:

一些东西被打印出来了(打印到从System.out.println()接收的界面上的一个JTextArea),一个进度条在上面弹出控制台,并在大约 10 秒内从 0% 变为 100%,消失,打印更多内容等等。

然而,当我将按钮添加到界面时,我点击了它,行为却完全不同:

什么都不打印。 progress Bar出现了,在0%停留了10秒,然后消失,几秒后doProgram执行完毕,运行时应该打印的信息突然一下子全部打印出来。

谁能告诉我是什么导致了这种奇怪的行为以及如何解决它?

我会发布一个 SSCCE,但是对于这个程序来说有点困难,因为有太多的任务和子流程。请注意,我使用的进度条在 GUI 线程上运行,主要代码在它自己的线程中完成(如您所见)

编辑:我在调用 doProgram 的监听器中添加了 setEnabled(false); 和 true,当以编程方式按下按钮时,按钮变灰并且按我的预期重新启用,但是如果我用手单击按钮它不会禁用,我可以再次按下它

最佳答案

代码看起来有点困惑。

但是,首先:您正在调用您在那里创建的新线程的 run() 方法。这将导致 run() 方法中的代码由调用 doProgram 的线程执行,而由您创建的新线程执行在那里创造。为了在这个新线程中真正执行代码,您必须调用 start() 而不是 run()

这可能解释了您描述的大部分差异。但是,请记住,对 Swing 组件的修改始终必须在事件调度线程上完成。因此,无论您在做什么,例如使用 gui.doCommands(),请确保您没有违反此单线程规则

关于java - 手动按下 JButton 会产生与 doClick() 不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22263074/

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