在 JApplet 中等待来自另一个类的用户输入
我有两节课。一个类在 JApplet
中创建 GUI,另一个类从小程序获取用户输入并用它进行计算。
applet 类创建 GUI 并通知计算类开始。然后计算类调用小程序类中的方法来请求用户输入。然后,此方法侦听来自 JTextField
的操作以返回计算类。
但问题是,当您运行该程序时,它实际上并没有等待用户在文本字段中输入内容。结果,计算类收到空输入。
我该如何解决这个问题?
我尝试在方法中使用 JOptionPane.showInputDialog
而不是 ActionListener
,这有效。但我不想用这个;主要是因为它就像一个弹出窗口,我希望用户直接在小程序中输入数据。
I have two classes. One creates the GUI in JApplet
, and the other class takes user input from the applet and does calculations with it.
The applet class creates GUI and tells the calculation class to start. Then the calculation class calls on a method from the applet class to ask for user input. This method then listens for action from a JTextField
to return to the calculation class.
But the problem is that when you run the program, it doesn't actually wait for the user to enter something in the text field. As a result the calculation class receives a null input.
How can I fix this?
I've tried using a JOptionPane.showInputDialog
instead of an ActionListener
in the method, and this works. But I don't want to use this; mainly since it's like a pop-up and I want the user to input data directly in the applet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
ActionListener
添加到小程序中的JTextField
。当事件被触发时(通常是当用户按“Enter”键时),使用文本字段中的String
调用计算类。或者换一种说法。作为事件源的类(小程序)应该调用计算类的方法。不应该是相反的情况。
Add an
ActionListener
to theJTextField
in the applet. When an event is fired (typically when the user presses 'Enter'), call the calculation class using theString
in the text field.Or to put that a different way. The class that is the source of the event (the applet) should invoke the method of the calculation class. It should not be the other way around.