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
Java SwingWorker-传递参数并返回ArrayList<;对象>;_Java_Swing_Swingworker - Fatal编程技术网

Java SwingWorker-传递参数并返回ArrayList<;对象>;

Java SwingWorker-传递参数并返回ArrayList<;对象>;,java,swing,swingworker,Java,Swing,Swingworker,注意:SSCCE在这个问题的末尾 我是新来的SwingWorker,所以请容忍我 我使用的是GUI,当用户按下按钮时,它会执行我在另一个类中使用的一些方法。这个类里面只有方法,我需要这些方法在它们自己的线程上运行。问题是,这些方法需要被传递参数,它们还将向其他方法返回所需的内容 大概是这样的: public class MyClass { public ArrayList<Train> proccessTrain(int endTime, int startTime, Ar

注意:SSCCE在这个问题的末尾

我是新来的
SwingWorker
,所以请容忍我

我使用的是GUI,当用户按下按钮时,它会执行我在另一个类中使用的一些方法。这个类里面只有方法,我需要这些方法在它们自己的线程上运行。问题是,这些方法需要被传递参数,它们还将向其他方法返回所需的内容

大概是这样的:

public class MyClass {

   public  ArrayList<Train> proccessTrain(int endTime, int startTime, ArrayList<Train> trainList)
   {
       (... I start processing data here... with sets and gets to the stationDataList array...)

       return trainList;
   }

   public int getNumberOfCrashes(ArrayList<Train> trainList)
   {
       (... I run over the array... do some comparasions... and do some sets on the Train object in the array...)

       return numOfCrashes;
   }

}
如果有遗漏和/或不清楚的地方,请让我知道,我会更新问题

///////////strong>

根据要求,这里有一个简短的可执行代码:

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame {

private JLabel countLabel1 = new JLabel("0");
private JLabel statusLabel = new JLabel("Task not completed.");
private JButton startButton = new JButton("Start");
private ArrayList<Train> trainList = new ArrayList<>();
private ArrayList<Train> processedTrains = new ArrayList<>();

public MainFrame(String title) {
    super(title);

    setLayout(new GridBagLayout());

    countLabel1.setFont(new Font("serif", Font.BOLD, 28));

    GridBagConstraints gc = new GridBagConstraints();

    gc.fill = GridBagConstraints.NONE;

    gc.gridx = 0;
    gc.gridy = 0;
    gc.weightx = 1;
    gc.weighty = 1;
    add(countLabel1, gc);

    gc.gridx = 0;
    gc.gridy = 1;
    gc.weightx = 1;
    gc.weighty = 1;
    add(statusLabel, gc);

    gc.gridx = 0;
    gc.gridy = 2;
    gc.weightx = 1;
    gc.weighty = 1;
    add(startButton, gc);

    Train t1 = new Train();
    t1.setId(1);
    t1.setName("John");

    Train t2 = new Train();
    t2.setId(2);
    t2.setName("Mark");

    Train t3 = new Train();
    t3.setId(3);
    t3.setName("Lewis");


    trainList.add(t1);
    trainList.add(t2);
    trainList.add(t3);


    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            TrainMethods tm = new TrainMethods();

            // How can the swing worker return the array from the
            // processTrains method to this processedTrains array
            processedTrains = tm.processTrains(trainList);

            for(int i = 0; i<processedTrains.size(); i++)
            {
                System.out.println("Train id: " + processedTrains.get(i).getId() 
                        + " Train Name: " + processedTrains.get(i).getName());
            }
        }
    });

    setSize(200, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

private class Train
{
    String name;
    int id;

    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

}

private class TrainMethods
{
    public ArrayList processTrains(ArrayList<Train> trainList)
    {
        // Add here a SwingWorker to create a new thread
        // and execute the following code and update later on
        // a progress bar
        for(int i = 0; i<trainList.size(); i++)
        {
            trainList.get(i).setName("No name");
        }
        return trainList;
    }
}
}
导入java.awt.Font;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
公共类大型机扩展JFrame{
私有JLabel countLabel1=新JLabel(“0”);
私有JLabel statusLabel=新JLabel(“任务未完成”);
私有JButton startButton=新JButton(“开始”);
private ArrayList trainList=new ArrayList();
private ArrayList processedTrains=新建ArrayList();
公共主机(字符串标题){
超级(标题);
setLayout(新的GridBagLayout());
countLabel1.setFont(新字体(“衬线”,Font.BOLD,28));
GridBagConstraints gc=新的GridBagConstraints();
gc.fill=GridBagConstraints.NONE;
gc.gridx=0;
gc.gridy=0;
gc.weightx=1;
gc.weighty=1;
添加(countLabel1,gc);
gc.gridx=0;
gc.gridy=1;
gc.weightx=1;
gc.weighty=1;
添加(状态标签,gc);
gc.gridx=0;
gc.gridy=2;
gc.weightx=1;
gc.weighty=1;
添加(启动按钮,gc);
t1列=新列车();
t1.setId(1);
t1.设定名称(“约翰”);
t2列=新列车();
t2.setId(2);
t2.设定名称(“标记”);
t3列车=新列车();
t3.setId(3);
t3.设定名称(“刘易斯”);
列车列表。添加(t1);
列车列表。添加(t2);
列车列表。添加(t3);
addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件arg0){
TrainMethods tm=新的TrainMethods();
//swing worker如何从服务器返回数组
//将processTrains方法添加到此processedTrains数组
processedTrains=tm.processTrains(列车列表);

对于(int i=0;i通常我不明白该代码如何帮助我,它没有显示如何按照我在问题中指定的方式传递和返回参数。或者我没有完全理解该答案?!请帮助您发布一个,然后问题可以对所有读者都清楚,从
SwingWorker worker=new SwingWorker()开始{
我不太明白这个问题:如果我的类应该由swingWorker控制,请将对实例的引用传递给worker?@mKorbel我刚刚根据要求添加了SSCCE。希望我现在说清楚,否则请让我知道,我会再次解释。
public  ArrayList<Train> proccessTrain(int endTime, int startTime, ArrayList<Train> trainList)
{
    SwingWorker<String, Integer> worker = new SwingWorker<String, String>() {
        @Override
        protected String doInBackground() throws Exception {

            return 
        }

        protected void done() {

        }

        @Override
        protected void process(List<Integer> chunks) {

        }
    };

    worker.execute();
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame {

private JLabel countLabel1 = new JLabel("0");
private JLabel statusLabel = new JLabel("Task not completed.");
private JButton startButton = new JButton("Start");
private ArrayList<Train> trainList = new ArrayList<>();
private ArrayList<Train> processedTrains = new ArrayList<>();

public MainFrame(String title) {
    super(title);

    setLayout(new GridBagLayout());

    countLabel1.setFont(new Font("serif", Font.BOLD, 28));

    GridBagConstraints gc = new GridBagConstraints();

    gc.fill = GridBagConstraints.NONE;

    gc.gridx = 0;
    gc.gridy = 0;
    gc.weightx = 1;
    gc.weighty = 1;
    add(countLabel1, gc);

    gc.gridx = 0;
    gc.gridy = 1;
    gc.weightx = 1;
    gc.weighty = 1;
    add(statusLabel, gc);

    gc.gridx = 0;
    gc.gridy = 2;
    gc.weightx = 1;
    gc.weighty = 1;
    add(startButton, gc);

    Train t1 = new Train();
    t1.setId(1);
    t1.setName("John");

    Train t2 = new Train();
    t2.setId(2);
    t2.setName("Mark");

    Train t3 = new Train();
    t3.setId(3);
    t3.setName("Lewis");


    trainList.add(t1);
    trainList.add(t2);
    trainList.add(t3);


    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            TrainMethods tm = new TrainMethods();

            // How can the swing worker return the array from the
            // processTrains method to this processedTrains array
            processedTrains = tm.processTrains(trainList);

            for(int i = 0; i<processedTrains.size(); i++)
            {
                System.out.println("Train id: " + processedTrains.get(i).getId() 
                        + " Train Name: " + processedTrains.get(i).getName());
            }
        }
    });

    setSize(200, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

private class Train
{
    String name;
    int id;

    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

}

private class TrainMethods
{
    public ArrayList processTrains(ArrayList<Train> trainList)
    {
        // Add here a SwingWorker to create a new thread
        // and execute the following code and update later on
        // a progress bar
        for(int i = 0; i<trainList.size(); i++)
        {
            trainList.get(i).setName("No name");
        }
        return trainList;
    }
}
}