btnNewButton = new JButton("ok"); btnNewButton.addActionListener(new ActionListener() { private String i; private String a; public void actionPerformed(ActionEvent arg0) { i = textField.getText(); a = "Abandon"; if(i == a){ lblNewLabel.setText("Abandon"); } } });
1 answer
Changing the text of the label must be performed in the swing event stream. Try this:
public void actionPerformed(ActionEvent arg0) { SwingUtilities.invokeLater(() -> { if ("Abadon".equals(textField.getText())) lblNewLabel.setText("Abadon"); else myLabel.setText("my text"); }); }
In general, you have an error in the code; strings should be compared using the equals
method.
- syntax error + I am a beginner and I don’t know many methods, but the fact that I was taught very much does not include - Ilya Kaminsky
- Syntax error on token (s), misplaced construct (s) - Ilya Kaminsky
- I corrected the code, the error should not appear - Artem Konovalov
- it is btnNewButton = new JButton ("ok"); private String i; private String a; public actionPerformed (ActionEvent arg0) {SwingUtilities. ;}); } - Ilya Kaminsky
- error in arg0 invalid modifers - Ilya Kaminsky
|