2

我在使用 JprogressBar 'pb1' 时遇到问题 - 它不会更新。我尝试了不同的方法来使其工作,但似乎我找不到我哪里出错了。propertyChangeListener 可能存在问题,我不完全确定。

   package one;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.*;

public class ProgressBar extends JFrame implements ActionListener, PropertyChangeListener {

    private JButton searchBtn;
    private JButton displayBtn;
    private JButton reverseBtn;
    private JButton ZIPbtn;
    private JLabel lblPath;
    private JLabel lblTxt1;
    private JLabel lblTxt3;
    private JLabel lblTxt2;
    private JTextArea txt1;
    private JScrollPane txtP1;
    private JTextArea txt2;
    private JScrollPane txtP2;
    private JTextArea txt3;
    private JScrollPane txtP3;
    private JProgressBar pb1;
    private JProgressBar pb2;
    private JProgressBar pb3;

    private Task task;
    static File fileArray;


    class Task extends SwingWorker<Void, Void> {

        BufferedWriter bwr1 = null;
        BufferedReader br1w = null;

        @Override
        protected Void doInBackground()
                throws Exception {

            txt1.setText(null);
            bwr1 = new BufferedWriter(
                    new FileWriter(
                            new File(
                                    "C:\\Users\\Saint\\Documents\\file 1 - reversed.txt")));

            BufferedReader br1 = new BufferedReader(
                    new FileReader(fileArray));
            String line;
            while ((line = br1.readLine()) != null) {
                StringBuffer sb1 = new StringBuffer(br1
                        .readLine());
                sb1.reverse();

                bwr1.write(sb1.toString());
            }

            bwr1.flush();

            bwr1.close();

            br1.close();

            System.out
                    .println("Content of StringBuffer written to File 1.");

            br1w = new BufferedReader(
                    new FileReader(
                            "C:\\Users\\Saint\\Documents\\file 1 - reversed.txt"));

            txt1.read(br1w,
                    "C:\\Users\\Saint\\Documents\\file 1 - reversed.txt");

            return null;
        }

        protected void done() {

            Toolkit.getDefaultToolkit().beep();
            reverseBtn.setEnabled(true);
            setCursor(null); //turn off the wait cursor
            pb1.setValue(100);
            pb1.setVisible(false);
        }

    };

    public void actionPerformed(ActionEvent evt) {
        pb1.setStringPainted(true);
        pb1.setVisible(true);

        reverseBtn.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
            int progress = task.getProgress();
            pb1.setValue(progress);
            System.out.println(progress);


    }

public ProgressBar(){

        // construct components
                searchBtn = new JButton("Search");
                reverseBtn = new JButton("Reverse Text");
                reverseBtn.setActionCommand("start");
                reverseBtn.addActionListener(this);
                lblTxt1 = new JLabel("File 1");

                txt1 = new JTextArea(5, 5);
                txtP1 = new JScrollPane(txt1);
                txt1.setLineWrap(true);

                pb1 = new JProgressBar(0, 100);


                txtP1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


                setSize(new Dimension(800, 800));
                setLayout(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setTitle("");


                setResizable(false);
                pb1.setVisible(false);
                // pb1.setStringPainted(true);

                // add components
                add(searchBtn);

                add(reverseBtn);

                add(lblTxt1);

                add(txtP1);

                add(pb1);


                // set component bounds (only needed by Absolute Positioning)
                searchBtn.setBounds(15, 15, 100, 25);

                lblTxt1.setBounds(5, 50, 300, 25);

                txtP1.setBounds(5, 75, 735, 190);

                reverseBtn.setBounds(300, 730, 150, 25);

                pb1.setBounds(305, 47, 400, 25);




        searchBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            fileChooser();
                            lblTxt1.setText(fileArray.toString());

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

    }


private String fileChooser() throws FileNotFoundException {
    // Create a file chooser
    JFileChooser fc = new JFileChooser();
    // Button click response:
    int returnVal = fc.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        fileArray = fc.getSelectedFile();


    } else {
        return "No file selected";
    }
    return "No file selected";
}


public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new ProgressBar().setVisible(true);
        }
    });
}
}
4

2 回答 2

4

您似乎从未setProgress(...)在 Task SwingWorker 内部调用过。如果进度属性永远不会改变,那么调用getProgress()将不会显示任何更改。

要解决这个问题,您必须安装您的 doInBackGround 以便setProgress(...)在事情进展时调用。这将通知正在侦听工作人员的任何 PropertyChangeListener。您可能会在处理文件的 while 循环内执行此操作。


顺便说一句,您还有另一个危险的 SwingWorker 似乎正在进行 Swing 调用,例如setText(null)doInBackground()方法内部:

SwingWorker<Void, Void> workerClear = new SwingWorker<Void, Void>() {

  @Override
  protected Void doInBackground() throws Exception {

     txt1.setText(null);
     txt2.setText(null);
     txt3.setText(null);

     return null;
  }
};

您不想这样做,请在 EDT 上进行 Swing 跟注。事实上,我认为没有理由在您的代码中的这个位置使用 SwingWorker,并且有很多理由不这样做。


另外,根据我的评论,您发布了太多与您的问题无关的代码。下次在这里,请通过将您的代码缩减为可编译、运行和显示您的问题的可管理数量的代码,即sscce来发布更少的代码。

于 2013-10-12T15:04:01.563 回答
1

在您的情况下更新 JprogressBar 将取决于任务的长度。而不是使用pb1.setStringPainted(true);你可以使用pb1.setIndeterminate(true);. 它不会显示任务完成的确切百分比,但会允许 JProgressBar 表示任务在任务期间仍在进行中。

于 2013-10-12T19:18:37.543 回答