JProgressBar用于顯示任務(wù)的進(jìn)度。它有一個(gè)方向,可以是水平或垂直。
JProgressBar有三個(gè)與之相關(guān)的值:當(dāng)前值,最小值和最大值。
要?jiǎng)?chuàng)建具有當(dāng)前值,最小值和最大值的水平進(jìn)度條分別設(shè)置為0,0和100。
JProgressBar hpBar1 = new JProgressBar();
要?jiǎng)?chuàng)建具有當(dāng)前值,最小值和最大值的水平進(jìn)度條 分別設(shè)置為20,20和200。
JProgressBar hpbar2 = new JProgressBar(SwingConstants.HORIZONTAL, 20, 200);
要?jiǎng)?chuàng)建具有當(dāng)前,最小和最大值的垂直進(jìn)度條 分別設(shè)置為5,5和50。
JProgressBar vpBar1 = new JProgressBar(SwingConstants.VERTICAL, 5, 50);
要使用其setValue(int value)設(shè)置進(jìn)度條的當(dāng)前值,方法來(lái)指示進(jìn)度。
要顯示描述進(jìn)度條當(dāng)前值的字符串,請(qǐng)使用setStringPainted(true)方法,并通過(guò)調(diào)用setString(String s)方法設(shè)置要繪制的String。
我們可以使用setIndeterminate()方法在不確定模式下設(shè)置進(jìn)度條。
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // www . j a va2 s . c o m import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JProgressBar; public class Main extends JFrame { JProgressBar bar = new JProgressBar(); JButton step = new JButton("Step"); public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); step.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int value = bar.getValue() + 7; if (value > bar.getMaximum()) { value = bar.getMaximum(); } bar.setValue(value); } }); getContentPane().add(bar, BorderLayout.NORTH); getContentPane().add(step, BorderLayout.EAST); pack(); setVisible(true); } public static void main(String arg[]) { new Main(); } }
進(jìn)度條用于冗長(zhǎng)的任務(wù)。
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* w w w.j ava 2s.c om*/ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JProgressBar; import javax.swing.Timer; public class Main { public static void main(String[] args) { final Timer timer; final JProgressBar progressBar = new JProgressBar(); final JButton button = new JButton("Start"); JFrame f = new JFrame(); f.setLayout(new FlowLayout()); f.add(progressBar); f.add(button); ActionListener updateProBar = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int val = progressBar.getValue(); if (val >= 100) { // timer.stop(); button.setText("End"); return; } progressBar.setValue(++val); } }; timer = new Timer(50, updateProBar); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (timer.isRunning()) { timer.stop(); button.setText("Start"); } else if (button.getText() != "End") { timer.start(); button.setText("Stop"); } } }); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); } }
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /*from ww w . ja v a2 s. c o m*/ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JProgressBar; public class Main { static class BarThread extends Thread { private static int DELAY = 500; JProgressBar progressBar; public BarThread(JProgressBar bar) { progressBar = bar; } public void run() { int minimum = progressBar.getMinimum(); int maximum = progressBar.getMaximum(); for (int i = minimum; i < maximum; i++) { try { int value = progressBar.getValue(); progressBar.setValue(value + 1); Thread.sleep(DELAY); } catch (InterruptedException ignoredException) { } } } } public static void main(String args[]) { JFrame frame = new JFrame("Stepping Progress"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JProgressBar aJProgressBar = new JProgressBar(0, 50); aJProgressBar.setStringPainted(true); final JButton aJButton = new JButton("Start"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { aJButton.setEnabled(false); Thread stepper = new BarThread(aJProgressBar); stepper.start(); } }; aJButton.addActionListener(actionListener); frame.add(aJProgressBar, BorderLayout.NORTH); frame.add(aJButton, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); } }
要將標(biāo)簽更改為固定字符串,請(qǐng)調(diào)用public void setString(String newValue)方法和setStringPainted(true)。在垂直進(jìn)度條上,字符串被繪制旋轉(zhuǎn),因此更長(zhǎng)的字符串將更好。
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* w ww . j ava2 s .c o m*/ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JProgressBar; class BarThread extends Thread { private static int DELAY = 500; JProgressBar progressBar; public BarThread(JProgressBar bar) { progressBar = bar; } public void run() { int minimum = progressBar.getMinimum(); int maximum = progressBar.getMaximum(); for (int i = minimum; i < maximum; i++) { try { int value = progressBar.getValue(); progressBar.setValue(value + 1); Thread.sleep(DELAY); } catch (InterruptedException ignoredException) { } } } } public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Stepping Progress"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL); aJProgressBar.setStringPainted(true); final JButton aJButton = new JButton("Start"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { aJButton.setEnabled(false); Thread stepper = new BarThread(aJProgressBar); stepper.start(); } }; aJButton.addActionListener(actionListener); frame.add(aJProgressBar, BorderLayout.NORTH); frame.add(aJButton, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); } }
不確定模式:進(jìn)度條從一側(cè)到另一側(cè)反彈。
import java.awt.BorderLayout; // w w w .java 2 s .co m import javax.swing.JFrame; import javax.swing.JProgressBar; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Stepping Progress"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL); aJProgressBar.setStringPainted(true); aJProgressBar.setIndeterminate(true); frame.add(aJProgressBar, BorderLayout.NORTH); frame.setSize(300, 200); frame.setVisible(true); } }
監(jiān)聽(tīng)JProgressBar組件中的值更改
import javax.swing.JProgressBar; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /*from w ww . ja v a2s . c om*/ public class Main { public static void main(String[] argv) throws Exception { int minimum = 0; int maximum = 100; JProgressBar progress = new JProgressBar(minimum, maximum); progress.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent evt) { JProgressBar comp = (JProgressBar) evt.getSource(); int value = comp.getValue(); int min = comp.getMinimum(); int max = comp.getMaximum(); } }); } }
更多建議: