Java Swing JProgressBar
2018-01-09 19:23 更新
Java Swing教程 - Java Swing JProgressBar
JProgressBar用于显示任务的进度。它有一个方向,可以是水平或垂直。
JProgressBar有三个与之相关的值:当前值,最小值和最大值。
要创建具有当前值,最小值和最大值的水平进度条分别设置为0,0和100。
JProgressBar hpBar1 = new JProgressBar();
要创建具有当前值,最小值和最大值的水平进度条 分别设置为20,20和200。
JProgressBar hpbar2 = new JProgressBar(SwingConstants.HORIZONTAL, 20, 200);
要创建具有当前,最小和最大值的垂直进度条 分别设置为5,5和50。
JProgressBar vpBar1 = new JProgressBar(SwingConstants.VERTICAL, 5, 50);
要使用其setValue(int value)设置进度条的当前值,方法来指示进度。
要显示描述进度条当前值的字符串,请使用setStringPainted(true)方法,并通过调用setString(String s)方法设置要绘制的String。
我们可以使用setIndeterminate()方法在不确定模式下设置进度条。
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(); } }
带定时器的JProgressBar
进度条用于冗长的任务。
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); } }
标记JProgressBar
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); } }
Virtical JProgressBar
要将标签更改为固定字符串,请调用public void setString(String newValue)方法和setStringPainted(true)。在垂直进度条上,字符串被绘制旋转,因此更长的字符串将更好。
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); } }
不确定JProgressBar
不确定模式:进度条从一侧到另一侧反弹。
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); } }
JProgressBar中的值更改
监听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(); } }); } }
以上内容是否对您有帮助:
更多建议: