the timer every 100 milliseconds must finish writing the top five, but it does not

package com.javacodegeeks.example; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; 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); } } javax.swing.Timer timer = new javax.swing.Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { textArea1.setText(textArea1.getText() + " 5 "); } }); 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); } } 
  • one
    your program is fundamentally wrong and with a bunch of errors - the context panel is not created, but simply declared, the button is not placed in it, the time, the more jtextarea. It feels like you just threw all the vegetables into a plate and asked how to make a salad. - Denis
  • @ Kuzmich and where are the panel and text variables initialized? - Peter Slusar
  • but how to do it? I did the form for a lesson from the Internet - kuzmich
  • What is the purpose of your lesson? A timer that will add to the textArea every 0.1 seconds. by "5" at the end? - Denis
  • Yes, this is the purpose of the lesson - kuzmich

1 answer 1

Then carefully look at your lesson again. Here you can see good examples of how to make applications using Swing . And here is a simple example using a timer , which adds the number "5" to the end of the JTextArea each time:

 package test; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test { public static void main(String[] args) { JFrame frame = new JFrame("Calculator"); // создаём фрейм нашей программы JPanel mainPane = new JPanel(); // создаём панель, на которой будет лежать текстовое поле JTextArea textarea = new JTextArea(); mainPane.add(textarea); // добавляем на панель ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textarea.append("5"); // ставим задачу - добавлять пятерку в textarea } }; Timer timer = new Timer( 100, listener ); // ставим задачу на таймер на каждые 100 милисекунд timer.start(); // запускаем таймер frame.setContentPane(mainPane); // кидаем панель в JFrame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } } 

Conclusion:

enter image description here