有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么JProgressbar的setProgress方法不接受超过100的值?

我正在设计一个进度条,它将持续[long]并开始倒计时,直到达到零。所以我附加了一段代码[test unite]来模拟这个动作。我向task对象传递了一个值=500(比如int),以在包含其余程序代码之前测试进度条的行为,但不幸的是,除了传递值=100之外,它不起作用,我不知道为什么?有人能告诉我我错过了什么吗

public class ProgressBarDemo extends JPanel implements ActionListener, PropertyChangeListener {

private JProgressBar progressBar;
private JButton startButton;
private JTextArea taskOutput;
private Task task;

class Task extends SwingWorker<Void, Void> {
    int progress;

    public Task(int progress) {
        super();
        this.progress = progress;
    }

    /*
     * Main task. Executed in background thread.
     */
    @Override
    public Void doInBackground() {

        // Initialize progress property.
        setProgress(progress);
        while (progress > 1) {
            // Sleep for up to one second.
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignore) {
            }
            // Make  progress go down by 10.
            progress -= 10;
            setProgress(progress);
            // if you want to stop the timer
            if (progress == 20) {
                progress = 100;

            }

        }
        return null;
    }

    /*
     * Executed in event dispatching thread
     */
    @Override
    public void done() {
        Toolkit.getDefaultToolkit().beep();
        startButton.setEnabled(true);
        setCursor(null); // turn off the wait cursor
        taskOutput.append("Done!\n");
    }
}

public ProgressBarDemo() {
    super(new BorderLayout());

    // Create the demo's UI.
    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(this);

    progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
    progressBar.setValue(0);
    progressBar.setStringPainted(true);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setMargin(new Insets(5, 5, 5, 5));
    taskOutput.setEditable(false);

    JPanel panel = new JPanel();
    panel.add(startButton);
    panel.add(progressBar);

    add(panel, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

}

/**
 * Invoked when the user presses the start button.
 */
public void actionPerformed(ActionEvent evt) {
    startButton.setEnabled(false);
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

    task = new Task(500);
    task.addPropertyChangeListener(this);
    task.execute();

}

/**
 * Invoked when task's progress property changes.
 */
public void propertyChange(PropertyChangeEvent evt) {
    if ("progress" == evt.getPropertyName()) {
        int progress = (Integer) evt.getNewValue();
        progressBar.setValue(progress);
        taskOutput.append(String.format("Completed %d%% of task.\n", task.getProgress()));
    }
}

/**
 * Create the GUI and show it. As with all GUI code, this must run on the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("ProgressBarDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    JComponent newContentPane = new ProgressBarDemo();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}


共 (0) 个答案