我创建了一个小的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();
}
}
答案 0 :(得分:2)
我不是专家,但我改变了一些你的代码。首先,我在调用execute()
之前为worker创建了一个窗口,我将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();
}
}
有关详情,请查看SwingWorker
的{{3}}。