【问题标题】:JButton not invoking actionPerformed on any subsequent click in same GUI instanceJButton 不会在同一 GUI 实例中的任何后续单击时调用 actionPerformed
【发布时间】:2017-02-13 14:22:27
【问题描述】:

我有一个 JButton,它不允许我在同一个 Swing GUI 实例中的第一次点击之后对它执行相同的操作。

    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);
    }
}

【问题讨论】:

    标签: java swing


    【解决方案1】:

    乍一看,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 检查,它们在控制台中都显示得很好,但不要执行脚本
    • @lxuboiro 5 参数用于指定 5 个线程。在这种情况下,1 也可以。如果您在更大的 gui 中有一个共享的执行器服务,那么拥有多个后台线程会很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2012-09-13
    • 2012-07-17
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    相关资源
    最近更新 更多