这是我的代码。如果用户按下按钮,我想返回true,否则返回false。我的问题是,当按下按钮时,它仍然等待time.sleep完成,然后返回。我该如何解决?
public boolean popConfirmation(String action) {
delete = false;
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton clickmeButton = new JButton("Click Me");
buttonPanel.add(clickmeButton);
frame.add(buttonPanel,BorderLayout.SOUTH);
frame.setSize(popUpWidth, popUpHeight);
frame.setLocation(popUpX, popUpY);
frame.setVisible(true);
frame.setAlwaysOnTop(ALWAYS_ON_TOP);
clickmeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
delete = true;
frame.setVisible(false);
frame.dispose();
}
});
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
logger.error("Error", e);
}
frame.setVisible(false);
frame.dispose();
return delete;
}
答案 0 :(得分:-1)
尝试将Thread.interrupt()放置在actionPerformed主体中。
答案 1 :(得分:-1)
我会尝试创建全局变量
私有线程线程;
然后使用thread.sleep()和thread.interupt();