我无法使用 setEnabled(false) 禁用,按钮在正确的阶段未被禁用

I can't disable with setEnabled(false), the button is not disabled in correct phase

我有 Selenium WebDriver callSe.test(); + JFrame。 这是框架的构造函数:

public AutoFrame() {
    textFieldVersion.setColumns(10);
    textFieldUrl.setColumns(10);
    textPaneIsBuildCorrect.setBackground(UIManager.getColor("menu"));
    btnRun.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            btnRun.setEnabled(false);
            getEnteredVer();
            CheckBuildVersion callSe = new CheckBuildVersion();
            callSe.test();
            textPaneIsBuildCorrect.setText(callSe.getIsBuildCorrect());
            if (textPaneIsBuildCorrect.getText().contains("The Build is correct!")) {
                textPaneIsBuildCorrect.setForeground(Color.blue);
            }
            else {
                textPaneIsBuildCorrect.setForeground(Color.red);
            }
            textPaneCurrentBuild.setText(callSe.getBuild());
        }
    });
    initGUI();
}

所以我希望在 btnRun.setEnabled(false); 之后按钮被禁用,但事实并非如此。它只是被标记,而框架只是一种冻结。 仅当整个构造函数完成时,按钮才变为不可点击(false,禁用)。 为什么会这样?我想,当我按下要禁用的按钮时,我将启用它。也许我必须使用带有 PleaseWait 的模态对话框?

运行 单独线程中的 Selenium 任务。

   Thread thread = new Thread() {
        public void run() {
            //your selenium actions
        }
    };
    thread.start();

针对您的情况

 btnRun.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        btnRun.setEnabled(false);
        getEnteredVer();
        Thread thread = new Thread() {
            public void run() {
                  CheckBuildVersion callSe = new CheckBuildVersion();
            callSe.test();
            textPaneIsBuildCorrect.setText(callSe.getIsBuildCorrect());
            if (textPaneIsBuildCorrect.getText().contains("The Build is correct!")) {
                textPaneIsBuildCorrect.setForeground(Color.blue);
            }
            else {
                textPaneIsBuildCorrect.setForeground(Color.red);
            }
            textPaneCurrentBuild.setText(callSe.getBuild());
            }
        };
    thread.start();

    }
});