在actionPerformed完成java之前单击禁用按钮

在actionPerformed完成java之前单击禁用按钮

本文介绍了在actionPerformed完成java之前单击禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮执行很长的功能,我想在用户点击它之后禁用此按钮,以防止他再次点击它

I have a button doing a long function, I want to disable this button after the user once click on it to in order to prevent him from clicking it again many times

按钮被禁用,但问题是在功能完成后按钮再次启用

i尝试放置 button.setEnabled(false); 在一个新的线程但它不起作用

The button gets disabled but the problem is after the function finished the button gets enabled again
i tried to put button.setEnabled(false); in a new thread but it didn't work either

用于测试此代码示例

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        button.setEnabled(false);
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            for (int j = 0; j < Integer.MAX_VALUE; j++) {
                for (int ii = 0; ii < Integer.MAX_VALUE; ii++) {
                }
            }
        }
    }
});


推荐答案

使用用于长时间运行的后台任务。在此中, startButton 操作会执行 setEnabled(false),而worker的 done()实现 setEnabled(true)

Use SwingWorker for long running background tasks. In this example, the startButton action does setEnabled(false), and the worker's done() implementation does setEnabled(true).

这篇关于在actionPerformed完成java之前单击禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:19