Using scanner class with a GUI

南笙酒味 提交于 2021-02-05 09:28:18

问题


I'm using java swing to create my GUI and using the scanner class to get the information inputted from the JTextFields across to the server. is this possible and if so how?


回答1:


No. There is no console, so don't use Scanner. Instead get text when you need it by using TextField's getText() method.




回答2:


That isn't how Swing works. Scanner is only for command-line input. If you have a JTextField, just call the .getText() method on it.

JTextField myField = new JTextField();
...
String currentText = myField.getText();

Swing is event-based. You probably want to have a JButton and have that JButton cause the text to be submitted to the server when it is clicked. For that, you'll need an ActionListener. See the tutorial below for more information: http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html



来源:https://stackoverflow.com/questions/22671885/using-scanner-class-with-a-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!