gpt4 book ai didi

java - 以正确的方式实现 Event-Dispatch-Thread

转载 作者:行者123 更新时间:2023-11-30 08:27:35 24 4
gpt4 key购买 nike

我的程序中需要一个 Event-Dispatch-Thread。

我想从这里开始:

public Controller {
this.login= new Login(this);
}

为此:

public Controller {
Runnable guiCreator = new Runnable() {
public void run() {
this.login= new Login(this);
}
};
SwingUtilities.invokeLater(guiCreator);
}

我知道第二个版本哪里错了。但是 Login(Controller controller) 构造函数需要 Controller 引用...我该怎么做?

最佳答案

尝试使用 Controller.this 来引用类的外部/顶级实例

public Controller() {
Runnable guiCreator = new Runnable() {
public void run() {
login= new Login(Controller.this);
}
};
SwingUtilities.invokeLater(guiCreator);
}

关于java - 以正确的方式实现 Event-Dispatch-Thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20783892/

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