为什么进度条走不到100%? 运行后,每次结果proc的值不都是100,0~100都有,既是最终进度条走完,数值停在25%,56%,89%,各种可能都有是否Math.round()有问题?应该如何改。或者是否有更好的方法,请不吝赐教。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我做了一下没出来你去考虑getcurrentvalue();吧 import java.awt.*;import java.awt.event.*;import javax.swing.*; public class ProgressBarDemo extends JFrame { public final static int ONE_SECOND = 1000; private JProgressBar progressBar; private Timer timer; private JButton startButton; private LongTask task; private JTextArea taskOutput; private String newline = "\n"; public ProgressBarDemo() { super("ProgressBarDemo"); task = new LongTask(); //Create the demo's UI. startButton = new JButton("Start"); startButton.setActionCommand("start"); startButton.addActionListener(new ButtonListener()); progressBar = new JProgressBar(0, task.getLengthOfTask()); 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); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(panel, BorderLayout.NORTH); contentPane.add(new JScrollPane(taskOutput), BorderLayout.CENTER); contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); setContentPane(contentPane); //Create a timer. timer = new Timer(ONE_SECOND, new ActionListener() { public void actionPerformed(ActionEvent evt) { progressBar.setValue(task.getCurrent()); taskOutput.append(task.getMessage() + newline); taskOutput.setCaretPosition( taskOutput.getDocument().getLength()); if (task.done()) { Toolkit.getDefaultToolkit().beep(); timer.stop(); startButton.setEnabled(true); progressBar.setValue(progressBar.getMinimum()); } } }); } /** * The actionPerformed method in this class * is called when the user presses the start button. */ class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { startButton.setEnabled(false); task.go(); timer.start(); } } public static void main(String[] args) { JFrame frame = new ProgressBarDemo(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); }} 这就是sun公司处理例题你看过没没看过就看一下看过就不要给我分 不行吧,太突然了,20%直接跳到100%。 wyqiang(强)给了我不少启发。 java.awt.Graphics 能不能把画笔设置成半透明 大家看看这样的 字符串怎么删除? 由java对象生成xml字符串,怎么办 如何让一个字符串换行????谢谢!!! 菜鸟问问题,求解答!! ★★★ 难题求解!请帮忙! ★★★ (能写几个就写几个吧) 求java内存模型详细资料 JTable表怎么在表格中保存正在编辑的数据 jsp + tomcat的配置问题!(在线等待) Java中字符串相等的判断! 关于滚屏的问题 关于线程的问题。
你去考虑getcurrentvalue();
吧
import java.awt.event.*;
import javax.swing.*; public class ProgressBarDemo extends JFrame {
public final static int ONE_SECOND = 1000; private JProgressBar progressBar;
private Timer timer;
private JButton startButton;
private LongTask task;
private JTextArea taskOutput;
private String newline = "\n"; public ProgressBarDemo() {
super("ProgressBarDemo");
task = new LongTask(); //Create the demo's UI.
startButton = new JButton("Start");
startButton.setActionCommand("start");
startButton.addActionListener(new ButtonListener()); progressBar = new JProgressBar(0, task.getLengthOfTask());
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); JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(taskOutput), BorderLayout.CENTER);
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
setContentPane(contentPane); //Create a timer.
timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progressBar.setValue(task.getCurrent());
taskOutput.append(task.getMessage() + newline);
taskOutput.setCaretPosition(
taskOutput.getDocument().getLength());
if (task.done()) {
Toolkit.getDefaultToolkit().beep();
timer.stop();
startButton.setEnabled(true);
progressBar.setValue(progressBar.getMinimum());
}
}
});
} /**
* The actionPerformed method in this class
* is called when the user presses the start button.
*/
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
startButton.setEnabled(false);
task.go();
timer.start();
}
}
public static void main(String[] args) {
JFrame frame = new ProgressBarDemo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); frame.pack();
frame.setVisible(true);
}
}
你看过没
没看过就看一下
看过就不要给我分
wyqiang(强)给了我不少启发。