Using scanner class with a GUI

前端 未结 2 390
说谎
说谎 2021-01-27 20:31

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?

相关标签:
2条回答
  • 2021-01-27 21:15

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

    0 讨论(0)
  • 2021-01-27 21:22

    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

    0 讨论(0)
提交回复
热议问题