Swing/Java: How to use the getText and setText string properly

后端 未结 4 1564
青春惊慌失措
青春惊慌失措 2021-01-05 16:28

I\'m trying to make input nameField appear in a Label called label1 after a Button called button1 is clicked

相关标签:
4条回答
  • 2021-01-05 16:59

    the getText method returns a String, while the setText receives a String, so you can write it like label1.setText(nameField.getText()); in your listener.

    0 讨论(0)
  • 2021-01-05 17:01

    You are setting the label text before the button is clicked to "txt". Instead when the button is clicked call setText() on the label and pass it the text from the text field.

    Example:

    label1.setText(nameField.getText()); 
    
    0 讨论(0)
  • 2021-01-05 17:06

    Setup a DocumentListener on nameField. When nameField is updated, update your label.

    http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/JTextField.html

    0 讨论(0)
  • 2021-01-05 17:09

    in your action performed method, call:

    label1.setText(nameField.getText());
    

    This way, when the button is clicked, label will be updated to the nameField text.

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