[英]How to stop program until a button is pressed in JAVA?
How do I make it so that the program doesn't keep reading in code until the button is clicked? 我该如何做才能使程序在单击按钮之前不继续读取代码?
Why?: I have a 10x10 grid with buttons in each part and then code running depending on what is clicked. 为什么?:我有一个10x10的网格,每个部分都有按钮,然后根据单击的内容运行代码。 However, my program keeps reading in code so there is never a choice being made and it gives me error. 但是,我的程序一直在阅读代码,因此永远不会做出选择,这会给我带来错误。 I tried giving it a infinite loop until a button is pressed, but that doesn't work out so well 我尝试给它一个无限循环,直到按下按钮为止,但是效果并不理想
-edit I'm a complete beginner with Java. -edit我是Java的完整入门者。
This is a picture of the GUI 这是GUI的图片
http://imageshack.us/content_round.php?page=done&l=img843/5351/sascp.png http://imageshack.us/content_round.php?page=done&l=img843/5351/sascp.png
What I want is for the code to not keep running step by step until I click a button. 我要的是使代码在单击按钮之前不逐步运行。
EG: 例如:
if(buttonClicked[i][k] == something){
System.out.println("lool");
}
But what's happening in my code is that it creates the gameGUI
and then because the user isn't fast enough to click it just skips over the if statement or gets a run-time error because nothing was pressed. 但是在我的代码中发生的事情是它创建了gameGUI
,然后由于用户不够快而无法单击它而只是跳过了if语句或由于未按任何内容而导致运行时错误。
In both Android & Swing (& I'd expect J2ME), buttons fire events when told to do so (by activating them). 在Android和Swing中(并且我希望J2ME),按钮会在被告知执行事件时触发事件(通过激活它们)。 You would generally just wait for that to happen before doing anything, and not bother with what the rest of the GUI is doing (or not doing) at the time. 通常,您在执行任何操作之前通常只是等待该操作发生,而不必理会当时GUI的其余部分正在执行(或未执行)的操作。
Or in other words: 换句话说:
ActionListener
to the buttons. 将ActionListener
添加到按钮。 actionPerformed()
method, insert the code that you have above. 在actionPerformed()
方法中,插入上面的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.