Таймер не отсылает значение в поле - Java
Формулировка задачи:
каждые 5 секунд должно выполняться textArea1.setText(" 55 " ); и System.out.println("Running....");
выполняется только System.out.println("Running...."); а в textArea1 ничего нету
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;
public class Calculator {
private JTextField resultsTxt;
private JButton SetBtn;
private JPanel calculatorView;
private JTextArea textArea1;
public Calculator() {
SetBtn.addActionListener(new SetBtnClicked());
}
private class SetBtnClicked implements ActionListener {
public void actionPerformed(ActionEvent e) {
String a, s;
a = resultsTxt.getText();
s = textArea1.getText();
textArea1.setText(s + " " + a);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setContentPane(new Calculator().calculatorView);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
Calculator sc = new Calculator();
sc.textArea1.setText(" 55 " );
System.out.println("Running....");
}
}, 0, 5000);
}
}Решение задачи: «Таймер не отсылает значение в поле»
textual
Листинг программы
package com.javacodegeeks.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;
public class Calculator{
private JTextField resultsTxt = new JTextField();
private JButton setBtn = new JButton("Set");
private JPanel calculatorView = new JPanel(new GridLayout(3,0));
private JTextArea textArea1 = new JTextArea();