- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:请注意,这个问题与我的其他问题相关:SwingWorker - Passing parameters and returning an ArrayList<Object>
在深入研究 SwingWorker 后,为了尝试解决这个问题,我想出了这个 SSCCE 代码。
注释结束
编辑结束
给出以下 SSCCE 代码:
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 java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class MainFrame extends JFrame {
private JLabel countLabel1 = new JLabel("0");
private JLabel statusLabel1 = new JLabel("Task not completed.");
private JLabel statusLabel2 = new JLabel("Task not completed.");
private JLabel statusLabel3 = 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(statusLabel1, gc);
gc.gridx = 0;
gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
add(statusLabel2, gc);
gc.gridx = 0;
gc.gridy = 3;
gc.weightx = 1;
gc.weighty = 1;
add(statusLabel3, gc);
gc.gridx = 0;
gc.gridy = 4;
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);
}
public static void main(String[] args) {
// Notice that it kicks it off on the event-dispatching thread, not the main thread.
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame mf = new MainFrame("Yo");
}
});
}
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(final ArrayList<Train> trainList) {
// Add here a SwingWorker to create a new thread
// and execute the following code and update later on
// a progress bar
SwingWorker<ArrayList, Integer> worker = new SwingWorker<ArrayList, Integer>() {
@Override
protected ArrayList doInBackground() throws Exception {
for(int i = 0; i<trainList.size(); i++)
{
trainList.get(i).setName("No name");
publish(i);
}
return trainList;
}
@Override
protected void process(List<Integer> chunks) {
int mostRecentValue = chunks.get(chunks.size() - 1);
countLabel1.setText(Integer.toString(mostRecentValue));
}
@Override
protected void done() {
ArrayList<Train> status;
try {
// Retrieve the return value of doInBackground.
status = get();
statusLabel1.setText("Completed Train 1 with name: " + status.get(0).getName());
statusLabel2.setText("\nCompleted Train 2 with name: " + status.get(1).getName());
statusLabel3.setText("\nCompleted Train 3 with name: " + status.get(2).getName());
} catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
worker.execute();
return trainList;
// for (int i = 0; i < trainList.size(); i++) {
// trainList.get(i).setName("No name");
// }
// return trainList;
}
}
}
为什么System.Out.Println()
打印:
Train id: 1 Train Name: John
Train id: 2 Train Name: Mark
Train id: 3 Train Name: Lewis
当用户界面打印:No Name
?就我个人而言,我希望在两者上都打印No Name
。
问题 1:为什么输出不同?
问题2:如何让它在两个地方(Sysout 和 GUI)都输出 No Name
?
_________________________ 编辑 _______________________________
问题3:为什么我第二次按下开始按钮时,两者都会打印No Name
?
最佳答案
doInBackground()
毫不奇怪地在后台运行。也就是说,它是在另一个线程中执行的。因此,您不能依赖该代码的执行顺序以及您的打印代码。因此trainsList
的内容可以在打印之前、之后或期间修改。如果您确实需要立即获得结果,则需要添加一些同步(通常不需要。这就是在另一个线程中运行任务以避免等待任务完成的全部意义所在)。
最有可能的正确解决方案是根本没有打印代码,并让 SwingWorker
按照预期的方式工作。结果可以在 done()
中以线程安全的方式获得(在 process()
中可以在子任务准备就绪后立即获得您需要的任何内容)。
在第二次运行中,您会得到“无名称”,因为您从未将名称重置为其他任何名称,因此它们是在上一次运行中设置的名称。
关于java - SwingWorker - 相同的变量在 sysout 和 GUI 上显示不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19548570/
在我的代码中想要输出控制台消息/并且/将其记录下来的地方,我当前正在调用 System.out.println/and/和 log.info。 这当然是不可取的,我想解决它。我一直在查看 slf4j/
在 Eclipse 中练习 Java 数组时,我遇到了数组的这种奇怪行为。 public class base3 { public static void main(String[] args
执行此操作: public class Test { public static void main(String[] args) { String s=null;
这个问题在这里已经有了答案: Loop doesn't see value changed by other thread without a print statement (1 个回答) 关闭
我注意到一个奇怪的问题。我得到了这个异常(exception): org.hibernate.LazyInitializationException: failed to lazily initial
我一直在使用 eclipse 学习 java,但是当我输入 sysout 时,它通常显示 println,但现在它显示了 private void sysout() 什么时候显示 System.out
我检查了我的 Eclipse 中的首选项设置,它全部设置为默认值并启用了 sysout 选项,但是当我在 Eclipse 中键入 sysout 时,它不会自动进入 System.out.println
在我的应用程序中,我使用浮点值来保存客户评分。并且根据要求,分数可以只有5位小数点前和3位小数点后。 例如:99999.999 但是,我不理解使用 sysout 打印浮点值时的奇怪行为。 更多细节请引
我在查找 BB 应用程序中的新错误时遇到了困难。我想了解 RIM 提供的控制台日志输出。 背景是:我无法让它在模拟器上中断,因此我在 Torch 设备上运行 JDE-4.5 编译的应用程序。通过 Ec
我是 Java 新手。在新的 Lubuntu(12.10 x64,包含所有更新)下,我从 Lubuntu 软件中心下载 Eclipse (3.8)。我的问题是 System.out.print() 仅
所以,我在文件“Functions.java”中有一个方法,如下所示。 public static void updateDataToExcel (String ExcelFileName) thro
我想重定向/隐藏由以下代码生成的系统输出: Tools tool = new ToolsImpl(); HashCode hash = tool.computeHashCodes(dir); 困难的部
我们有一个在 tomcat 上运行的 java web 应用程序,在该应用程序中使用了很多 System.out.println 语句。会不会影响应用性能 注意:tomcat 作为服务运行,所有 sy
这是cobol代码 *----------------------- IDENTIFICATION DIVISION. *---------------------
在编写JCL时,我们给出SYSOUT和SYSPRINT DD。哪种类型的输出会发送到 SYSOUT,还有什么类型的输出会发送到 SYSPRINT? 最佳答案 SYSOUT 始终被分配,并获取系统级进程
这是cobol代码 *----------------------- IDENTIFICATION DIVISION. *---------------------
有几个来自 DFSORT 的消息,它被具有多个排序操作的 COBOL 程序内部使用。我想删除那些 DFSORT 消息并仅保留来自 COBOL 程序的消息。 最佳答案 您有三个选择。 使用 OUTDD(
在哈希集中,我可以打印使用 obj 名称的 sysout 添加的对象,也可以通过迭代和返回元素来打印。有什么不同?两者都给出存储在哈希集中的对象。 下面是代码: public static void
我一直在尝试找出如何放置 toString() 方法以使系统 println 看起来有意义,但我不知道我应该将此方法放在哪里才能使代码看起来正确。所以我添加所有构造函数 + getters 和 set
这个问题在这里已经有了答案: Loop doesn't see value changed by other thread without a print statement (1 个回答) 关闭
我是一名优秀的程序员,十分优秀!