问题
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