Таймер в Swing - Java (240784)

Узнай цену своей работы

Формулировка задачи:

не могу разобраться, как поставить таймер что бы по прошествию времени указанном в jTextField1 нажималась кнопка

Решение задачи: «Таймер в Swing»

textual
Листинг программы
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.FocusEvent;
  6. import java.awt.event.FocusListener;
  7.  
  8. public class MainButton2 extends JFrame {
  9.     private JButton jButton;
  10.     private JButton jButton2;
  11.     private JTextField jTextField;
  12.     private JTextField jTextField2;
  13.     private Timer timer;
  14.     private int counter;
  15.     private int counterMax;
  16.     private final int MAX_TIME = 200;
  17.  
  18.     public MainButton2() throws HeadlessException {
  19.         timer = new Timer(20, new ActionListener() {
  20.             @Override
  21.             public void actionPerformed(ActionEvent e) {
  22.                 counter++;
  23.  
  24.                 jTextField.setText("count:" + counter);
  25.  
  26.                 if (counter >= counterMax) {
  27.                     counter = 0;
  28.                     timer.stop();
  29.                     jButton2.doClick();
  30.                 }
  31.  
  32.             }
  33.         });
  34.  
  35.  
  36.         jButton = new JButton("Start Timer");
  37.         jButton2 = new JButton("Press Me");
  38.  
  39.         jButton.addActionListener(new ActionListener() {
  40.             @Override
  41.             public void actionPerformed(ActionEvent e) {
  42.  
  43.                 timer.start();
  44.                 if (counterMax == 0) {
  45.                     counterMax = MAX_TIME;
  46.                 }
  47.                 jTextField2.setText("" + counterMax);
  48.             }
  49.         });
  50.  
  51.  
  52.         jButton2.addActionListener(new ActionListener() {
  53.             @Override
  54.             public void actionPerformed(ActionEvent e) {
  55.                 JOptionPane.showMessageDialog(getContentPane(), " Таймер сработал! ");
  56.             }
  57.         });
  58.         jTextField = new JTextField(10);
  59.         jTextField2 = new JTextField(10);
  60.  
  61.  
  62.         jTextField2.addFocusListener(new FocusListener() {
  63.             @Override
  64.             public void focusGained(FocusEvent e) {
  65.             }
  66.  
  67.             @Override
  68.             public void focusLost(FocusEvent e) {
  69.                 try {
  70.                     counterMax = Integer.parseInt(((JTextField) e.getSource()).getText());
  71.                 } catch (NumberFormatException ex) {
  72.                     JOptionPane.showMessageDialog(getContentPane(), " Wrong value, enter time! ");
  73.                 }
  74.  
  75.             }
  76.         });
  77.         jTextField2.addActionListener(new ActionListener() {
  78.             @Override
  79.             public void actionPerformed(ActionEvent e) {
  80.  
  81.                 try {
  82.                     counterMax = Integer.parseInt(((JTextField) e.getSource()).getText());
  83.                 } catch (NumberFormatException ex) {
  84.                     JOptionPane.showMessageDialog(getContentPane(), " Wrong value, enter time! ");
  85.                 }
  86.             }
  87.         });
  88.  
  89.         JPanel jp = new JPanel(new GridLayout(4, 4));
  90.         jp.add(new JLabel("Enter Time: "));
  91.         jp.add(jTextField2);
  92.         jp.add(new JLabel("Status:"));
  93.         jp.add(jTextField);
  94.         jp.add(jButton);
  95.         jp.add(jButton2);
  96.         add(jp, BorderLayout.NORTH);
  97.  
  98.  
  99.     }
  100.  
  101.  
  102.     public static void main(String[] args) {
  103.         SwingUtilities.invokeLater(new Runnable() {
  104.             @Override
  105.             public void run() {
  106.                 JFrame jFrame = new MainButton2();
  107.                 jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  108.                 jFrame.setSize(400, 200);
  109.                 jFrame.setLocationRelativeTo(null);
  110.                 jFrame.setVisible(true);
  111.  
  112.  
  113.             }
  114.         });
  115.     }
  116. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 4.1 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут