等待一个JFrame,然后从中检索信息 [英] Waiting for a JFrame and then retrieving information from it
问题描述
首先,我是Java的新手,您可能会猜到.预先感谢.
First of all I'm new to Java and you'll probably be able to guess. Thanks in advance.
我目前正在使用Java编写棋盘游戏进行作业.有人类和计算机玩家类,而GUI仅在人类玩家类中.在人类班级中,我使用名为"UI"的类扩展JFrame,用户在其中选择一块.
I'm currently writing a board game using Java for an assignment. There are human and computer player classes, and the GUI only in the human player class. In the human class I use a class called 'UI' extending JFrame where the user selects a piece.
此UI类等待输入按钮按下,然后将'done'变量设置为true.在人类玩家类中,我创建了这个UI,然后在while循环中不断检查此布尔值,然后获取移动的X/Y位置.
At the moment this UI class waits for the enter button to be pressed then sets a 'done' variable to true. In the human player class I create this UI then wait in a while loop continuously checking this boolean before getting the X/Y position of the move.
while (!input.moveEntered());
move.setPosition(input.getX(), input.getY());
这仅在while循环不为空时才起作用.如果我在其中添加打印语句,则效果很好. 似乎有更好的方法可以解决此问题.我已经查看了对话框,但每次输入动作时都不会关闭窗口.
This only seems to work if the while loop is not empty. If I add a print statement in there it works fine. It seems like there is a much better way to go about this. I've looked into dialogs but I don't close the window every time a move is entered.
感谢阅读.
推荐答案
您永远不应该等待.在您的情况下,我将显示一个模式JDialog.当用户输入了输入内容后,setVisible(true)方法将结束,而无需等待.
You should never be busy-waiting; in your case, I would show a modal JDialog. When the user has entered their input, the setVisible(true) method will end without busy-waiting.
或者也许我误解了您的问题,您只需要在主JFrame中定义一个事件侦听器即可处理用户输入.看一下 http://www.tutorialspoint.com/swing/swing_event_listeners.htm
Or maybe I misunderstood your problem and you only need to define an event listener in your main JFrame in order to handle user input. Have a look at http://www.tutorialspoint.com/swing/swing_event_listeners.htm
这篇关于等待一个JFrame,然后从中检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!