0

JButton我需要创建一个仅在按下a 时才返回的方法。我有一个自定义JButton课程

public class MyButton extends JButton {


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

}

我想实施waitForPress. 基本上,该方法应该只在用户用鼠标按下按钮时返回。我已经实现了类似的行为JTextField(仅在用户按下时返回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.

提前:请,如果您想评论说这不是一个好主意,并且应该简单地等待actionPerformed事件发生JButton然后采取一些行动,请意识到我已经知道并且有充分的理由去做我的事情'我在这里问。请尽量只帮助我提出的问题。谢谢!!

提前:请也意识到执行 actionPerformed 也不会直接解决问题。因为即使没有按下按钮,代码也会继续进行。我需要程序停止,并且只有在按下按钮时才返回。如果我要使用 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;
   }

}
4

2 回答 2

3

对于它的价值,这是你可以做到的wait()notify()但我觉得这里有一个更深层次的问题。我不认为这是一个令人满意的解决方案:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestBlockingButton {

    boolean clicked = false;
    private Object toNotify;

    private void initUI() {
        JFrame frame = new JFrame(TestBlockingButton.class.getSimpleName());
        JButton button = new JButton("Click me");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                clicked = true;
                if (toNotify != null) {
                    synchronized (TestBlockingButton.this) {
                        toNotify.notify();
                    }
                }
            }
        });
        frame.add(button);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void waitForProcess() {
        toNotify = this;
        while (!clicked) {
            synchronized (this) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        System.out.println("continuing work");
    }

    public static void main(String[] args) {
        final TestBlockingButton test = new TestBlockingButton();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                test.initUI();
            }
        });
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("I was doing something and now I will wait for button click");
                test.waitForProcess();
                System.out.println("User has now cliked the button and I can continue my work");
            }
        });

    }
}
于 2013-01-06T22:54:31.870 回答
2

当您要求使用互斥锁实现时,这就是它的样子。我正在使用一个ActionListener,但它没有忙碌的等待。如果那不是您想要的,那么您至少看到了 Burkhard 的意思;)

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()但是正如您所写的那样,您很有经验并且知道如何处理它。

于 2013-01-06T22:35:01.130 回答