Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton不调用在同一GUI实例中的任何后续单击上执行的操作_Java_Swing - Fatal编程技术网

Java JButton不调用在同一GUI实例中的任何后续单击上执行的操作

Java JButton不调用在同一GUI实例中的任何后续单击上执行的操作,java,swing,Java,Swing,我有一个JButton,它不允许我在同一个swinggui实例中的第一次单击之后对其执行相同的操作 JButton Run = new JButton("Run"); Run.setLocation(290, 70); Run.setSize(120, 30); buttonPanel.add(Run); Run.addActionListener(new ActionListener() { @Override publi

我有一个JButton,它不允许我在同一个swinggui实例中的第一次单击之后对其执行相同的操作

    JButton Run = new JButton("Run");
    Run.setLocation(290, 70);
    Run.setSize(120, 30);
    buttonPanel.add(Run);
    Run.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (Run.isEnabled()) {
                errorLabel.setText("");
                    Result result = JUnitCore.runClasses(Run.class);
                        errorMessageDisplay(result);

            }
        }
    });

totalGUI.setOpaque(true);
    return totalGUI;
}

到目前为止,我考虑并尝试移除JPanel,重新绘制所有按钮,禁用/重新绘制按钮

errorMessageDisplay方法如下所示:

public void errorMessageDisplay(Result resultPass) {
    if (resultPass.getFailureCount() > 0) {
        errorLabel.setForeground(Color.red);
        errorLabel.setVisible(true);
        errorLabel.setText(" Failed");
    }

    else {
        errorLabel.setForeground(Color.green);
        errorLabel.setText(" Passed");
        errorLabel.setVisible(true);
    }
}

乍一看,
JUnitCore.runClasses(Run.class)呼叫是可疑的。另外,最好知道
errorMessageDisplay()
的作用。我认为,问题在于这些方法之一

您可以使用以下实验代码来验证这一点。只是小心不要把它推到生产中

JButton run = new JButton("Run");
run.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (Run.isEnabled()) {
                errorLabel.setText("");
                System.out.println("Run action peformed.");

            }
        }
更新由于
errorMessageDisplay()
看起来没有问题,这可能是
JUniCore
的线程问题。因此,我将尝试以下代码:

final ExecutorService executor = Executors.newFixedThreadPool(5); // this runs stuff in background
JButton run = new JButton("Run");
// ..
run.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (Run.isEnabled()) {
                executor.execute(new Runnable() { // This is how we run stuff in background. You can use lambdas instead of Runnables.
                    public void run() {
                        final Result result = JUnitCore.runClasses(Run.class); // Run.class is different from the current JButton run.
                        SwingUtilities.invokeLater(new Runnable() { // Now we go back to the GUI thread
                            public void run() {
                                errorMessageDisplay(result);
                            }
                        });
                    }
                });
        }
    });

好的,谢谢,我现在就开始实施。我可以问一下“5”的论点是什么吗?不幸的是,这似乎没有起作用。我添加了该块中最后一段代码的另一个示例。也许这与此有关?我还尝试了第一条关于“运行已执行操作”消息的建议,这条建议很有效,所以我猜这与您建议的线程有关?奇怪的是,我在您提供的代码示例中加入了一些sysout签入,它们在控制台中都显示得非常好,但不要执行script@lxuboiro
5
参数用于指定5个线程。在这种情况下,
1
也可以。如果您在一个更大的gui中有一个共享执行器服务,那么拥有多个后台线程可能会很有用。