我希望延迟这个计划

时间:2017-11-26 12:11:09

标签: java delay

我使用'Thread''TimeUnit'但不知道如何在以下程序中使用。我希望当WIN + E执行然后在1或2秒的下一个语句运行一段延迟之后。因为,下一个语句是for循环所以它应该在2秒无限时间后运行(因为无限循环)。您只能看到ActionListener行。 包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);  
    }

}

1 个答案:

答案 0 :(得分:0)

我会选择这样的事情:

src_pts = np.float32([ kp1[m.queryIdx].pt for m in matches[:50] ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in matches[:50] ]).reshape(-1,1,2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
matchesMask = mask.ravel().tolist()
h,w = img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)

正如你所看到的,我已经添加了一个布尔值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); } } 来控制以前是否已经访问过该函数,所以,这样你就不能运行多个,最后我添加了一个notstarted来减轻Thread对调用它的线程可能产生的影响。

无论如何,应该有更好的方法来实现你想要的东西。