There is a string in JFrame , the value of which needs to be constantly updated when a button is pressed, but this does not work.

How to implement it?

Button :

  class Button extends JFrame() { JButton button; JTextArea text; Listining l = new Listining(); public Button(String s) { super(s); //Вызываем исходник int a; setLayout(new FlowLayout()); button = new JButton("Та самая кнопка"); text = new JTextArea(a); add(button); add(text); button.addActionListener(l); } } 

ActionListener :

 public class Listining implements ActionListener { public void actionPerformed(ActionEvent e) { try { if (e.getSource() == button) { a++; } } catch(Exception ex) { JOptionPane.showMessageDialog(null, "Error, sorry."); } } } 
  • Added by ActionListener - Dmig12

1 answer 1

 try { if (e.getSource() == button) { a++; text.setText(a); } } catch(Exception ex) { JOptionPane.showMessageDialog(null, "Error, sorry."); } 
  • Your answer does not work - Dmig12
  • Your question was - as when clicking on the button to update the value in the string. The code I gave makes the following look at whether the button was pressed or not. If that button is pressed, then increment the counter a, and then write the resulting value in the text field. What is not working? - Andrew Bystrov
  • ** I deeply apologize, made a mistake before testing, thanks. - Dmig12