-1

I used 'Thread' and 'TimeUnit' but not know how to use in the following program. I want when WIN+E execute then after some delay of 1 or 2 second next statement run. As, next statement is in for loop so it should run after 2 seconds infinite time (because of infinite for loop). You can see ActionListener line only. package v;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class V extends JPanel{
    private JButton V;
    public V() throws AWTException{
        Robot r = new Robot();
        setBackground(Color.yellow);
        setPreferredSize(new Dimension(800,500));
        V = new JButton("PUSH");
        add(V);
        V.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
                for (int i=0; i>0; i++) {r.keyPress(KeyEvent.VK_WINDOWS); r.keyPress(KeyEvent.VK_E); r.keyRelease(KeyEvent.VK_WINDOWS); r.keyRelease(KeyEvent.VK_E);}
        }
        });
    }
    public static void main(String[] args) throws AWTException {
        V panel = new V();
        JFrame frame = new JFrame ("V");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);  
    }

}

2 Answers2

0

i would go for something like this:

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class V extends JPanel{
    private JButton V;
    private boolean notstarted=true;
    public V() throws AWTException{
        Robot r = new Robot();
        setBackground(Color.yellow);
        setPreferredSize(new Dimension(800,500));
        V = new JButton("PUSH");
        add(V);
        V.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
                if(notstarted){
                notstarted=false;
                new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {r.keyPress(KeyEvent.VK_WINDOWS); 
                        r.keyPress(KeyEvent.VK_E);
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(StreamServer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    r.keyRelease(KeyEvent.VK_WINDOWS); 
                    r.keyRelease(KeyEvent.VK_E);}
                }
            }).start();
            }
        }
        });
    }
    public static void main(String[] args) throws AWTException {
        V panel = new V();
        JFrame frame = new JFrame ("V");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);  
    }

}

As you can see i have added a boolean notstarted to control if the function have been accessed previously, so, this way you cant run that more than one and finally i have added a Thread to mitigate the impact the ActionListener could have on the thread that call it.

Anyway there should be beeter ways to achieve what you are looking for.

0

You can use javafx.animation.PauseTransition

PauseTransition happen = new PauseTransition(Duration.seconds(2));
    happen.setOnFinished(e -> {
        System.out.println("hello");
        happen.playFromStart();
    });
    happen.play();

Like that. It will print hello every 2 seconds. That however requires you to extend javafx.application.Application and I see that you don't use javafx in your program.

You can use Thread.sleep as the other answer suggests - to sleep each 2000 miliseconds (in your loop).

There is also java.util.Timer and java.util.TimerTask. It allows you to do code every milisecond given in time. There is an excellent video of that here https://www.youtube.com/watch?v=36jbBSQd3eU

Lealo
  • 331
  • 1
  • 3
  • 11