如何使GUI等待用户输入?

编程入门 行业动态 更新时间:2024-10-25 15:22:41
本文介绍了如何使GUI等待用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是一个相当基础的程序员,他被分配来制作GUI程序,而没有任何创建GUI的经验.使用NetBeans,我设法设计了GUI的外观,以及按下某些按钮时应该执行的操作,但是主程序在继续操作之前不会等待用户的输入.我的问题是,如何使该程序等待输入?

I'm a rather basic programmer who has been assigned to make a GUI program without any prior experience with creating a GUI. Using NetBeans, I managed to design what I feel the GUI should look like, and what some of the buttons should do when pressed, but the main program doesn't wait for the user's input before continuing. My question is, how do I make this program wait for input?

public class UnoMain { public static void main(String args[]) { UnoGUI form = new UnoGUI(); // GUI class instance // NetBeans allowed me to design some dialog boxes alongside the main JFrame, so form.gameSetupDialog.setVisible(true); // This is how I'm trying to use a dialog box /* Right around here is the first part of the problem. * I don't know how to make the program wait for the dialog to complete. * It should wait for a submission by a button named playerCountButton. * After the dialog is complete it's supposed to hide too but it doesn't do that either. */ Uno Game = new Uno(form.Players); // Game instance is started form.setVisible(true); // Main GUI made visible boolean beingPlayed = true; // Variable dictating if player still wishes to play. form.playerCountLabel.setText("Players: " + Game.Players.size()); // A GUI label reflects the number of players input by the user in the dialog box. while (beingPlayed) { if (!Game.getCompleted()) // While the game runs, two general functions are repeatedly called. { Player activePlayer = Game.Players.get(Game.getWhoseTurn()); // There are CPU players, which do their thing automatically... Game.Turn(activePlayer); // And human players which require input before continuing. /* Second part of the problem: * if activePlayer's strategy == manual/human * wait for GUI input from either a button named * playButton or a button named passButton */ Game.advanceTurn(); // GUI updating code // } } } }

我花了大约三天的时间来弄清楚如何集成我的代码和GUI,因此,如果有人可以向我展示如何使这一件事起作用,我将不胜感激.如果您需要其他任何信息来帮助我,请询问.

I've spent about three days trying to figure out how to integrate my code and GUI, so I would be grateful if someone could show me how to make this one thing work. If you need any other information to help me, please ask.

基本上,教授指派我们用GUI制作Uno游戏.可能有计算机和人类玩家,其数量由用户在游戏开始时确定.首先,我基于控制台对整个事物进行了编码,以使游戏的核心发挥作用,此后便尝试设计GUI.当前,该GUI仅在运行时显示有关游戏的信息,但是我不确定如何在不预先加载程序的情况下允许代码等待和接收来自GUI的输入.我已经调查了其他StackOverflow问题,例如此,此,此或此,但我无法理解如何将答案应用于自己的代码.如果可能,我希望获得与链接中的答案相似的答案(我可以检查和/或使用的具有代码的答案).如果我听起来要求苛刻或未受教育且令人困惑,我深表歉意.我已经在这个项目上进行了几周的努力,现在已经定于明天,而且我一直在强调,因为直到我弄清楚这一点,我才能继续前进.

Basically, the professor assigned us to make a game of Uno with a GUI. There can be computer and human players, the numbers of which are determined by the user at the beginning of the game. I coded the entire thing console-based at first to get the core of the game to work, and have since tried to design a GUI; currently this GUI only displays information about the game while it's running, but I'm not sure how to allow the code to wait for and receive input from the GUI without the program charging on ahead. I've investigated other StackOverflow questions like this, this, this, or this, but I cannot comprehend how to apply the answers to my own code. If possible, I'd like an answer similar to the answers in the links (an answer with code I can examine and/or use). I apologize if I sound demanding or uneducated and confusing; I've been working diligently on this project for a couple weeks and it's now due tomorrow, and I've been stressing because I can't advance until I figure this out.

TL; DR -如何让我的主程序等待并监听按钮单击事件?我应该使用模式对话框,还是有其他方法可以使用?无论哪种情况,都需要更改哪些代码才能实现?

TL;DR - How do I get my main program to wait and listen for a button click event? Should I use modal dialog boxes, or is there some other way to do it? In either case, what code needs to be changed to do it?

推荐答案

与基于控制台的编程(通常具有定义明确的执行路径)不同,GUI应用程序在事件驱动的环境中运行.事件是从外部传入的,您对此有反应.可能会发生多种类型的事件,但通常,我们对用户通过单击鼠标和键盘输入生成的事件感兴趣.

Unlike console based programming, that typically has a well defined execution path, GUI apps operate within a event driven environment. Events come in from the outside and you react to them. There are many types of events that might occur, but typically, we're interested in those generate by the user, via mouse clicks and keyboard input.

这改变了GUI应用程序的工作方式.

This changes the way an GUI application works.

例如,您将需要摆脱while循环,因为这在GUI环境中是非常危险的事情,因为它通常会冻结"应用程序,使其看起来像您的应用程序已挂起(本质上它具有).

For example, you will need to get rid of your while loop, as this is a very dangerous thing to do in a GUI environment, as it will typically "freeze" the application, making it look like your application has hung (in essence it has).

相反,您将在UI控件上提供大量的侦听器,以响应用户输入并更新某种模型,这可能会影响UI上的其他控件.

Instead, you would provide a serious of listeners on your UI controls that respond to user input and update some kind of model, that may effect other controls on your UI.

因此,要尝试回答您的问题,您可以不做(等待用户输入),该应用程序已经存在,但是您可以通过侦听器捕获该输入并根据需要对其进行操作.

So, to try and answer your question, you kind of don't (wait for user input), the application already is, but you capture that input via listeners and act upon them as required.

更多推荐

如何使GUI等待用户输入?

本文发布于:2023-08-01 18:42:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270728.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!