jbutton动作,setIcon在动作结束时执行
我正在开发我的第一个 awt 游戏。我试图在单击按钮时(在某些条件下)将图标设置为按钮。这是代码的相关部分:
if (tileIsMemTile) {
System.out.println("!!! Right Tile !!!");
memTile.setBackground(Color.red);
numberOfMemTilesToGuess -= 1;
System.out.println("It rest " + numberOfMemTilesToGuess + " tiles to guess");
} else {
System.out.println("!!! Wrong Tile !!!");
Icon falseTileIcon = new ImageIcon(getClass().getResource("wrong.png"));
memTile.setIcon(falseTileIcon);
wrongGuessAction();
}
System.out.println(">>> Action processed >>>");
if (numberOfMemTilesToGuess == 0) {
System.out.println("\n END OF THE LEVEL");
System.out.println("Congratulations, you guessed all the tiles without error !! \n");
dispose();
//TODO !!!! SHOW INTERLEVEL INFORMATION !!!!
if (memGridDimX != maxDimX && memGridDimY != maxDimY) {
if (memGridDimX == memGridDimY)
new Memory(memGridDimX, memGridDimY + 1);
else
new Memory(memGridDimX + 1, memGridDimY);
} else
System.out.println("You have a really good memory my friend!");
}
}
private void wrongGuessAction() {
//TODO !!! FAILED IN LEVEL MESSAGE !!!
try { Thread.sleep(2000); } catch (Exception e1) {}
dispose();
if (memGridDimX == initialDimX && ( memGridDimY == initialDimY || memGridDimY == initialDimY + 1))
new Memory(initialDimX, initialDimY);
else
new Memory(memGridDimX - 1, memGridDimY - 1);
}
因此,在我设置 jbutton 的图标之后,我想在处理旧的 jframe 并启动新的 jframe (游戏中的下一个级别)之前看到它。但尽管尝试通过以下方式延迟执行 dispose() 函数:
try { Thread.sleep(2000); } catch (Exception e1) {}
以及任何其他延迟函数(例如比较 currenttimemillis()、to-t1),我什至尝试使用;
memTile.setBackground(Color.green);
int i = 0;
do {
System.out.println(i); i++;
} while (!memTile.getIcon().equals(Color.green));
实际上最后一个似乎正在工作,但 Eclipse 毫无例外地跳转到调试模式,即使我关闭框架,它也不会终止进程。
所以什么都行不通。在每种情况下,框架都会关闭,我看不到按钮上的图标。如果我评论处理并让它创建下一帧,则旧框架将保留,图标将加载到按钮上并正在创建新框架。我无法理解setIcon方法的执行原理。
提前致谢。
// memTile.setBackground(Color.green); // int i = 0; // 做 { //
// System.out.println(i);我++; // } while (!memTile.getIcon().equals(Color.green));
I'm developing my first awt game. I'm trying to set an icon to a button when it is clicked (under some conditions). here is the concerning part of the code:
if (tileIsMemTile) {
System.out.println("!!! Right Tile !!!");
memTile.setBackground(Color.red);
numberOfMemTilesToGuess -= 1;
System.out.println("It rest " + numberOfMemTilesToGuess + " tiles to guess");
} else {
System.out.println("!!! Wrong Tile !!!");
Icon falseTileIcon = new ImageIcon(getClass().getResource("wrong.png"));
memTile.setIcon(falseTileIcon);
wrongGuessAction();
}
System.out.println(">>> Action processed >>>");
if (numberOfMemTilesToGuess == 0) {
System.out.println("\n END OF THE LEVEL");
System.out.println("Congratulations, you guessed all the tiles without error !! \n");
dispose();
//TODO !!!! SHOW INTERLEVEL INFORMATION !!!!
if (memGridDimX != maxDimX && memGridDimY != maxDimY) {
if (memGridDimX == memGridDimY)
new Memory(memGridDimX, memGridDimY + 1);
else
new Memory(memGridDimX + 1, memGridDimY);
} else
System.out.println("You have a really good memory my friend!");
}
}
private void wrongGuessAction() {
//TODO !!! FAILED IN LEVEL MESSAGE !!!
try { Thread.sleep(2000); } catch (Exception e1) {}
dispose();
if (memGridDimX == initialDimX && ( memGridDimY == initialDimY || memGridDimY == initialDimY + 1))
new Memory(initialDimX, initialDimY);
else
new Memory(memGridDimX - 1, memGridDimY - 1);
}
So here after i set icon of the jbutton, i want to see it before disposing the old jframe and launching the new one (next level in the game). but despite of trying to delay of executing dispose() function by:
try { Thread.sleep(2000); } catch (Exception e1) {}
and with any other delaying functions (such as comparing currenttimemillis(), to-t1), i even tried to use;
memTile.setBackground(Color.green);
int i = 0;
do {
System.out.println(i); i++;
} while (!memTile.getIcon().equals(Color.green));
actually last one seems to be working but eclipse jumps to debug mode without any exception and it doesn't kill the process even i close the frame.
So nothing works. In each case the frame closes and i cannot see the icon on the button. if i comment dispose and leave it to create next frame, the old frame stays, icon loads on button and new frame is being created. I couldn't understand the principle of execution of setIcon method.
Thanks in advance.
// memTile.setBackground(Color.green);
// int i = 0;
// do {
//
// System.out.println(i); i++;
// } while (!memTile.getIcon().equals(Color.green));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ActionListener
在 AWT 事件调度线程上调用。同一线程还处理任何其他事件,例如绘画。因此,只要您的actionPerformed()
方法不返回,就不会绘制任何内容(或者 GUI 上的其他任何内容都会更改)。您应该在此线程之外执行长时间运行的操作(即启动一个新线程来执行此操作),然后对于 GUI 更改,使用
EventQueue.invokeLater
(或EventQueue.invokeAndWait
)回调代码>)。The
ActionListener
is invoked on the AWT event dispatch thread. The same thread also processes any other events like paints. Thus, as long as youractionPerformed()
method not returns, nothing is painted (or anything else on the GUI is changed).You should do longer-running actions outside of this thread (i.e. start a new Thread doing this), and then for GUI changes later call back with
EventQueue.invokeLater
(orEventQueue.invokeAndWait
).Paŭlo Ebermann 给出了正确的答案,但我只想补充一点,永远不要在事件调度线程上睡觉!这只会让事情变得更糟。 Swing 是一个功能强大的工具包,但恕我直言,它对不正确线程的容忍度(在 EDT 上睡眠、在 EDT 外更新 UI)是人们在使用 Swing 时遇到麻烦的第一个原因。如果在这种情况下它会因RuntimeException而失败,那么就会暴露出很多错误,从而更容易修复。
Paŭlo Ebermann gives the correct answer but I just wanna add that never sleep on the Event Dispatch Thread! That only makes things worse. Swing is powerful toolkit, but it's tolerance to incorrect threading (sleep on EDT, update UI outside EDT) is IMHO number one reason why people have trouble with Swing. If it instead would fail with RuntimeException under such circumstances a lot of bugs would be exposed and thus more easily fixed.
阅读 Swing 教程中有关并发的部分。
也许您的 Thread.sleep(...) 代码应该替换为 Swing Timer。 Swing 教程还有一个关于“如何使用 Swing 计时器”的部分。
Read the section from the Swing tutorial on Concurrency.
Maybe your Thread.sleep(...) code should be replaced with a Swing Timer. The Swing tutorial also has a section on "How to Use Swing Timers".