gpt4 book ai didi

java - doInBackground 不起作用

转载 作者:行者123 更新时间:2023-11-30 03:39:55 27 4
gpt4 key购买 nike

我创建了一个小型 BackGroundWorker 来测试 pdf 文件。一切正常,直到代码捕获 pdf 文件的路径。我真的不知道为什么它不起作用 -.- 我也没有在 Eclipse 的控制台中收到任何错误消息。你知道我的问题到底是什么吗?

public class SwingWorkerDemo {
static JButton pressMe;
static JPanel buttonPanel;
static String text;
protected static JTextArea textArea = new JTextArea(25, 45);

public SwingWorkerDemo() {
JFrame frame = new JFrame();
frame.setVisible(true);

buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
pressMe= new JButton("Drück mich");
buttonPanel.add(pressMe);
frame.add(buttonPanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pressMe.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
runWorker();
} catch (PDFDocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}

static class MyTask extends SwingWorker<Void, Void> {

public MyTask() throws PDFDocumentException {
JFrame frame = new JFrame();
frame.setVisible(true);

frame.add(textArea);
frame.pack();
}

@SuppressWarnings("resource")
@Override
protected Void doInBackground() throws Exception {
File inputFiles = new File("C:\\Users\\Tommy\\Desktop\\pdf\\Name_der_testenden.pdf");
PDFDocument pdf = new PDFDocument(inputFiles);

textArea.setText("content errors:" + "\n");
for (int pageNo = 1; pageNo < pdf.getPages(); pageNo++) {
try {
PCProperty content = pdf.getContent(pageNo, ContentCollationOptions.NONE);

if (content == null) {
textArea.setText("no content on page " + pageNo + "\n");
}

} catch (PDFDocumentException exception) {
textArea.setText("failed to read content on page " + pageNo + "\n");
}
}
return null;
}
}

public static void runWorker() throws PDFDocumentException {

MyTask task = new MyTask();
task.execute();
}

public static void main(String[] args) throws PDFDocumentException {
new SwingWorkerDemo();
}
}

最佳答案

我不是专家,但我稍微改变了你的代码。首先,我在调用 execute() 之前为工作人员创建了窗口,我通过构造函数将 textArea 传递给任务,并在那里使用 process( ) 方法来更新它。

这是代码,注意:我只是使用了一些带有 Thread.sleep() 的字符串来模拟进度。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

public class SwingWorkerDemo
{
static JButton pressMe;
static JPanel buttonPanel;
static String text;
protected static JTextArea textArea = new JTextArea(25, 45);

public SwingWorkerDemo() {
JFrame frame = new JFrame("Main Frame");
frame.setVisible(true);

buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
pressMe= new JButton("Drück mich");
buttonPanel.add(pressMe);
frame.add(buttonPanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pressMe.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
JFrame frame = new JFrame("Worker Frame");
frame.setVisible(true);

frame.add(textArea);
frame.pack();

MyTask task = new MyTask(textArea);
task.execute();
}
});
}

static class MyTask extends SwingWorker<String, String>
{
private JTextArea innerTextArea;

public MyTask(JTextArea innerTextArea)
{
this.innerTextArea = innerTextArea;
}

@Override
protected String doInBackground() throws Exception
{
publish("First message:" + "\n");
Thread.sleep(2000);
publish(" no content on page 2:" + "\n");
Thread.sleep(2000);
publish(" no content on page 4:" + "\n");
Thread.sleep(2000);
publish(" no content on page 5" + "\n");
Thread.sleep(2000);

return "reading done";
}

@Override
protected void process(List<String> chunks)
{
innerTextArea.append(chunks.get(chunks.size() - 1));
}

@Override
public void done()
{
try
{
innerTextArea.append(get());
}
catch (Exception e)
{
e.printStackTrace(System.err);
System.out.println("error");
}
}

}



public static void main(String[] args)
{
new SwingWorkerDemo();
}
}

有关更多信息,请查看 documentation SwingWorker 的。

关于java - doInBackground 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27012674/

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