Automatic dialer does not stop when you click Stop Spam

I created an automatic typer because I always wanted to know how they work. The only problem is that when I click the "Stop" button, it does not stop and it freezes my system.

I tried to change the time interval, and it still does not stop when the stop button is pressed.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class AutoSpammer{

    private static int interval;
    private static Timer timerMain;
    private static JTextField txtSpam;
    private static JTextField txtInterval;
    private static JButton btnStart;
    private static JButton btnStop;

    public static void main(String[]args){
        ActionListener taskSpam = new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                sendkeys(txtSpam.getText());
              }
        };
        ActionListener taskStartTimer = new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                timerMain.setDelay(Integer.parseInt(txtInterval.getText()));
                timerMain.start();
                btnStart.setEnabled(false);
                btnStop.setEnabled(true);
                txtInterval.setEnabled(false);
            }
        };
        ActionListener taskStopTimer = new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                timerMain.stop();
                btnStart.setEnabled(true);
                btnStop.setEnabled(false);
                txtInterval.setEnabled(true);
            }
        };

        btnStart = new JButton("Start Spam");
        btnStop = new JButton("Stop Spam");
        JLabel lbl1 = new JLabel("Enter Text:");
        JLabel lbl2 = new JLabel("Interval:");
        timerMain = new Timer(1,taskSpam);
        txtSpam = new JTextField("Enter text:", 13);
        txtInterval = new JTextField("3000",3);

        btnStart.addActionListener(taskStartTimer);
        btnStop.addActionListener(taskStopTimer);
        btnStop.setEnabled(false);

        JPanel intervalpane = new JPanel();
        intervalpane.add(lbl2,BorderLayout.EAST);
        intervalpane.add(txtInterval,BorderLayout.WEST);

        JPanel bottompane = new JPanel();
        bottompane.add(btnStart,BorderLayout.EAST);
        bottompane.add(btnStop,BorderLayout.CENTER);
        bottompane.add(intervalpane,BorderLayout.WEST);

        JPanel toppane = new JPanel();
        toppane.add(lbl1,BorderLayout.EAST);
        toppane.add(txtSpam,BorderLayout.NORTH);

        JPanel pane = new JPanel(new BorderLayout());
        pane.add(toppane,BorderLayout.NORTH);
        pane.add(bottompane,BorderLayout.SOUTH);

        JFrame frame = new JFrame("Spammer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(pane);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
      }

    private static void sendkeys(String text) {
        try {
            Robot robot = new Robot();
            text = text.toUpperCase();
            for(int i = 0; i < text.length(); i++)
                robot.keyPress(text.charAt(i));
        robot.keyPress(KeyEvent.VK_ENTER);
        }catch(java.awt.AWTException exc) {
            System.out.println("error");
        }
    }
}
+4
source share
1 answer
Program

works great for me. Stop stops the robot. I added simple System.outwhat was clicked and it stops

+1
source

Source: https://habr.com/ru/post/1615597/


All Articles