Make JButton按下时返回的方法

时间:2013-01-06 21:58:19

标签: java swing jbutton jtextfield

我需要创建一个仅在按下JButton时返回的方法。我有一个自定义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;
   }

}

2 个答案:

答案 0 :(得分: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");
            }
        });

    }
}

答案 1 :(得分: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();
    }
}

但我不认为这是一种干净的方法,但它不会消耗CPU时间(isStatementTrue) - 实现。
这里一个重要的事情是:你用m.waitForPress()来阻止主线程,但正如你所写的那样,你很有经验,而且你知道如何处理它。