按下JButton时返回的Make方法 [英] Make method that returns when JButton is pressed

查看:68
本文介绍了按下JButton时返回的Make方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个仅在按下JButton时返回的方法.我有一个自定义的JButton

I need to make a method that returns only when a JButton is pressed. I have a custom JButton class

public class MyButton extends JButton {


   public void waitForPress() {
       //returns only when user presses this button
   }

}

,我想实现waitForPress.基本上,该方法仅应在用户用鼠标按下按钮时返回.我已经为JTextField实现了类似的行为(仅在用户按下Space时返回):

and I want to implement waitForPress. Basically, the method should only return when the user presses the button with their mouse. I have achieved similar behavior for JTextField (to return only when user presses Space):

public void waitForTriggerKey() {
        final CountDownLatch latch = new CountDownLatch(1);
            KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
                public boolean dispatchKeyEvent(KeyEvent e) {
                    if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_SPACE) {
                        System.out.println("presed!");
                        latch.countDown();
                    }
                    return false;
                }
            };
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
            try {
                //current thread waits here until countDown() is called (see a few lines above)
                latch.await();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }  
            KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);

    }

但是我想对JButton做同样的事情.

but I would like to do the same thing with JButton.

提前: 请,如果您想发表评论说这不是一个好主意,请您只需等待JButton事件在然后执行一些操作,请意识到我已经知道这一点,并且有充分的理由去做我在这里要问的事情.请尝试仅提供我所要求的帮助.谢谢!

In advance: Please, if you wish to comment saying that this is not a good idea and that one should simply wait for actionPerformed event on a JButton and then do some action, please realize I already know that and have a good reason for doing what I'm asking here. Please try to only help with what I've asked. Thanks!!

预先: 请注意,实施actionPerformed也不能直接解决问题.因为即使不按下按钮,代码也会继续执行.我需要程序停止,并且仅在按下按钮后才返回.如果我要使用actionPerformed,这是一个糟糕的解决方案:

In advance: Please, also realize that implementing actionPerformed also will not directly solve the problem. Because the code will progress even without the button being pressed. I need the program to stop, and only return when the button has been pressed. Here is a terrible solution if I were to use actionPerformed:

public class MyButton extends JButton implements ActionPerformed {
   private boolean keepGoing = true;

   public MyButton(String s) {
       super(s);
       addActionListener(this);
   }

   public void waitForPress() {
       while(keepGoing);
       return;
   }

   public void actionPerformed(ActionEvent e) {
       keepGoing = false;
   }

}

推荐答案

当您要求使用互斥量的实现时,会是这样. 我正在使用ActionListener,但是没有忙碌的等待时间.如果那不是您想要的,您至少会明白Burkhard的意思;)

As you asked for an implementation with a mutex, here's what it would be like. I'm using an ActionListener though, but there's no busy wait in it. If that isn't what you desire, you atleast saw what Burkhard meant ;)

public class MyButton extends JButton implements ActionListener
{
    private Semaphore sem = new Semaphore(1);

    public MyButton(String text) throws InterruptedException
    {
        super(text);
        addActionListener(this);
        sem.acquire();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        sem.release();
    }

    public void waitForPress() throws InterruptedException {
        sem.acquire();
        //do your stuff
        sem.acquire();
        //or just
        //waitForPress()
            //if you dont want it to end.
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame();
        MyButton m = new MyButton("test");
        frame.add(m);
        frame.pack();
        frame.setVisible(true);
        m.waitForPress();
        //another time, if you only want it to block twice
        m.waitForPress();
    }
}

但是我不认为这是一种干净的方法,但是它不会像while(isStatementTrue)实现那样消耗CPU时间.
这里重要的是:您正在使用m.waitForPress()阻塞主线程,但是在撰写本文时,您很有经验并且知道如何处理.

But I don't think this is a clean approach, but it doesn't consume CPU-time like a while(isStatementTrue)-implementation.
An important thing here is: you're blocking the main thread with m.waitForPress() but as you wrote you're quite experienced and you know how to handle that.

这篇关于按下JButton时返回的Make方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆