gpt4 book ai didi

java - 创建按下 JButton 时返回的方法

转载 作者:行者123 更新时间:2023-12-01 17:26:52 25 4
gpt4 key购买 nike

我需要创建一个仅在按下 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;
}

}

最佳答案

就其值(value)而言,这里是如何使用 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");
}
});

}
}

关于java - 创建按下 JButton 时返回的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14187235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com