相关疑难解决方法(0)

可以在main之外的类中使用进度条吗?

现在,我的主要是用10行调用一个gui.根据这些行中有多少行有文本,调用9个类中的1个(两行必须有文本).被调用的类执行计算,我希望将进度条绑定到.以下是其中一个被调用类的示例(每个类都相似,但足够不同以保证新类.)我认为问题违反了EDT规则,但我在其上看到的所有示例都涉及到论点.运行代码时会出现框架,但在完成所有计算之前,进度条不会更新.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class twoLoan extends JFrame {

    static JFrame progressFrame;
    static JProgressBar progressBar;
    static Container pane;
    double amountSaved = 0;
    int i = 0;

    public void runCalcs(Double MP, Double StepAmt,
        Double L1, Double L2, Double C1, Double C2,
        Double IM1, Double IM2, Double M1Start, Double M2Start) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }

        int iterations = (int) (MP - (M1Start * M2Start));

        //Create all components
        progressFrame = new JFrame("Calculation Progress");
        progressFrame.setSize(300, …
Run Code Online (Sandbox Code Playgroud)

java swing swingworker jprogressbar

31
推荐指数
1
解决办法
2万
查看次数

如果线程启动执行程序,则无法从Future <?>和SwingWorker获取ArrayIndexOutOfBoundsException

我通过使用Executor玩SwingWorker的多线程,我在那里错误地从Vector中识别出错误的元素,看起来像这个代码相当忽略了Vector中的元素不存在

我的问题 - >如何/有可能以某种方式捕获此异常

简单的输出

run:
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
BUILD SUCCESSFUL (total time: 11 seconds)
Run Code Online (Sandbox Code Playgroud)

通过取消注释

//changeTableValues1(); // un-comment for get ArrayIndexOutOfBoundsException
Run Code Online (Sandbox Code Playgroud)

一切都正确,我得到ArrayIndexOutOfBoundsException并输出

run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: …
Run Code Online (Sandbox Code Playgroud)

java swing exception-handling vector swingworker

20
推荐指数
2
解决办法
8356
查看次数

如何取消SwingWorker的执行?

目前我有两个SwingWorker线程在后台工作.如果发生异常,该方法将停止工作,但该线程仍然运行.

如何停止执行并杀死doInBackground()发生异常的线程?

this.cancel(true)不要破坏/关闭线程.我怎样才能做到这一点?

@Override
protected Boolean doInBackground() throws Exception {
        try {
            while (true) {
                //some code here                   
                return true;
            }
        } catch (Exception e) {       
            this.cancel(true); //<-- this not cancel the thread               
            return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在Netbeans的调试中看到了这些线程.

'AWT-EventQueue-0' em execução
'AWT-Windows' em execução
'SwingWorker-pool-1-thread-1' em execução
'SwingWorker-pool-1-thread-2' em execução

//*em execução = in execution
Run Code Online (Sandbox Code Playgroud)

java swing multithreading swingworker cancellation

6
推荐指数
2
解决办法
2万
查看次数