gpt4 book ai didi

java - 使用 Swing Worker 在 Swing GUI 中运行 JUnit 测试

转载 作者:行者123 更新时间:2023-11-30 08:45:42 25 4
gpt4 key购买 nike

我正在编写一个能够执行一些 JUnit 测试的 GUI,为了处理这个问题,我使用了 SwingWorker。

当我启动程序时,会出现 GUI,我单击一些选择,然后 SwingWorker 启动并完成它的一部分,最后输出控制台输出或文件输出。然后我会再次点击 GUI 并开始另一个测试。此时当程序完成时,它会生成两次最终输出,例如控制台输出后将直接跟一个相同的控制台输出。

我假设这是由于 SwingWorker 没有终止和“死亡”。

此外,当我单击 GUI 中的“开始”按钮时,我正在创建 SwingWorker。这是个坏主意吗?正确的做法是什么?

编辑 添加了代码示例

public class TestMainFrame {

private static JFrame frame;
private static JTextArea textArea;

public TestMainFrame(){
createAndShowGUI();
}

private void createAndShowGUI() {
frame = new JFrame("Test");
frame.setLayout(new BorderLayout());

JButton btn = new JButton("Test Me");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(btn.getText().equals("Test Me")){
testMe();
}
}
});

textArea = new JTextArea("This is a test pane! \n");
textArea.setEditable(false);
JScrollPane scroller = new JScrollPane(textArea);

frame.add(scroller, BorderLayout.CENTER);
frame.add(btn, BorderLayout.PAGE_END);

frame.setSize(new Dimension(300, 400));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private static void testMe(){
writeToTextArea("Button Pressed");
writeToTextArea("Starting tests");

SwingWorker<Result, Void> worker = new SwingWorker<Result, Void>() {
@Override
public Result doInBackground() {
writeToTextArea("Inside the doInBackground method of SwingWorker");

return null;
}
@Override
public void done() {
writeToTextArea("The SwingWorker has finished");

}
};

worker.execute();
}

private static void writeToTextArea(String text){
textArea.append(text + "\n");
}

最佳答案

(不是答案)
这就是我运行您的代码的方式:

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

public class TestMainFrame {

private static JFrame frame;
private static JTextArea textArea;

public TestMainFrame() {
createAndShowGUI();
}

private void createAndShowGUI() {
frame = new JFrame("Test");
frame.setLayout(new BorderLayout());

final JButton btn = new JButton("Test Me");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (btn.getText().equals("Test Me")) {
testMe();
}
}
});

textArea = new JTextArea("This is a test pane! \n");
textArea.setEditable(false);
JScrollPane scroller = new JScrollPane(textArea);

frame.add(scroller, BorderLayout.CENTER);
frame.add(btn, BorderLayout.PAGE_END);

frame.setSize(new Dimension(300, 400));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private static void testMe() {
writeToTextArea("Button Pressed");
writeToTextArea("Starting tests");

SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
public Void doInBackground() {
writeToTextArea("Inside the doInBackground method of SwingWorker");

return null;
}

@Override
public void done() {
writeToTextArea("The SwingWorker has finished");
}
};

worker.execute();
}

// *** note change ***
private static void writeToTextArea(final String text) {
if (SwingUtilities.isEventDispatchThread()) {
textArea.append(text + "\n");
} else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text + "\n");
}
});
}

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestMainFrame testMainFrame = new TestMainFrame();
testMainFrame.createAndShowGUI();
}
});
}
}

关于java - 使用 Swing Worker 在 Swing GUI 中运行 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33169510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com