[英]What is the correct way to implement this do while?
I start my GUI like this, which seems correct. 我这样启动我的GUI,这似乎是正确的。
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame gui = new JFrame();
gui.setExtendedState(JFrame.MAXIMIZED_BOTH);
gui.setVisible(true);
}
});
At a certain point in the application, the playTurn() method gets fired. 在应用程序中的某个点,将触发playTurn()方法。 The for loops all turns in the list.
for循环会在列表中轮流显示。
for (String turn : controller.getTurns()) {
playTurn(turn);
}
I now load the correct panel with my CardLayout which worked fine. 现在,我用CardLayout加载了正确的面板,效果很好。 Then I had to write the playTurn() method.
然后,我不得不编写playTurn()方法。 So playTurn() gets called.
因此playTurn()被调用。 It should do certain things according to some variables.
它应该根据一些变量来做某些事情。 But it should not return until some buttons are disabled.
但是在禁用某些按钮之前,它不应该返回。 This is what I can't achieve, the program just stops working.
这是我无法实现的,程序只是停止工作。 I can guess it's in the direction of threads etc.. but can't seem to figure it out.
我可以猜测它是朝线程等方向发展的。但是似乎无法弄清楚。 Thanks in advance.
提前致谢。
public void playTurn(String turn) {
if (controller.givePlayers().contains(turn)) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
while (!turnFinished) {
if (!button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled()) {
turnFinished = true;
}
}
}
});
} else {
deletePlayer(turn);
}
}
Sorry for bad formatting. 抱歉,格式化错误。 Couldn't find where.
找不到位置。
EDIT: The GUI stops being responsive. 编辑:GUI停止响应。 Can't close program either.
也无法关闭程序。 I tried using a SwingWorker for the while which does not block the GUI but still playTurn() returns.
我尝试使用SwingWorker一段时间,它不会阻塞GUI,但playTurn()返回。
I have even tried creating a new thread where I call the method. 我什至尝试创建一个新线程来调用该方法。 The doesn't get blocked anymore but the method still returns.
不再被阻止,但方法仍然返回。
Thread one = new Thread() {
public void run() {
playTurn(turn);
}
};
FIXED: Placing the runnable up higher in the stack; 修正:将可运行物品放到更高的位置;
Your playTurn
method runs the code on the EDT , cause of this line javax.swing.SwingUtilities.invokeLater(new Runnable() {
, which makes your application GUI unresponsive as GUI-changing code must generally be run on the EDT
. Since your buttons won't change from your GUI, once the loop starts, it might just loop forever. 您的
playTurn
方法在EDT上运行代码,此行的原因是javax.swing.SwingUtilities.invokeLater(new Runnable() {
,这会使您的应用程序GUI无响应,因为更改GUI的代码通常必须在EDT
上运行。不会从您的GUI更改,一旦循环开始,它可能永远循环下去。
By running the code in another Thread
, you won't freeze your GUI. 通过在另一个
Thread
运行代码,您将不会冻结GUI。 I'm guessing, since you don't provide much informations on the rest of your code, that you might have to change the way you handle things once your loop is done. 我猜,由于您没有在其余代码中提供太多信息,因此一旦循环完成,您可能必须更改处理事情的方式。
Edit from comments : Since you don't want playTurn
to return, don't use a thread within it and make sure playTurn
is not running on the EDT
. 从注释中进行编辑:由于您不希望
playTurn
返回,因此请勿在其中使用线程,并确保playTurn
不在EDT
上运行。 Your playTurn
method will return after creating and making a new Thread
run the code. 创建并让新
Thread
运行代码后,您的playTurn
方法将返回。
You might want to try dong it like this : 您可能想要尝试像这样:
Runnable code = new Runnable() {
@Override
public void run() {
for (String turn : controller.getTurns()) {
playTurn(turn);
}
}
};
if (!SwingUtilities.isEventDispatchThread()) {
code.run();
} else {
new Thread(code).start();
}
To make sure you don't run the code on the EDT
. 为了确保您不在
EDT
上运行代码。 That way, playTurn
doesn't return until the loop condition is met, the GUI
stays responsive. 这样,直到满足循环条件时,
playTurn
才返回, GUI
保持响应。
public void playTurn(String turn) {
if (controller.givePlayers().contains(turn)) {
while (!turnFinished) {
if (!button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled()) {
turnFinished = true;
}
}
} else {
deletePlayer(turn);
}
}
Doing this might have you change a few things more. 这样做可能会让您做出更多更改。
The idea is to make the call to a new Thread
where you don't want it/need it to wait for the code being run in a new Thread
to end to continue. 这个想法是在不希望/不需要它的地方调用新
Thread
,以等待在新Thread
运行的代码结束以继续。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.