如何获取`JProgressBar`组件的值并显示在控制台中?
假设我们已经为`JProgressBar`设置了以下值 −
int min = 0; int max = 1000; progressBar = new JProgressBar(min, max);
现在,获取以上值并显示在控制台中 −
int value = progressBar.getValue(); System.out.println("Value = "+value); System.out.println("Minimum = "+progressBar.getMinimum()); System.out.println("Maximum = "+progressBar.getMaximum());
以下示例用于获取进度条组件的值 −
示例
package my; import javax.swing.*; public class SwingDemo extends JFrame { JProgressBar progressBar; int i = 0; SwingDemo() { int min = 0; int max = 1000; progressBar = new JProgressBar(min, max); int value = progressBar.getValue(); System.out.println("Value = "+value); System.out.println("Minimum = "+progressBar.getMinimum()); System.out.println("Maximum = "+progressBar.getMaximum()); progressBar.setBounds(70, 50, 120, 30); progressBar.setValue(0); progressBar.setStringPainted(true); add(progressBar); setSize(550, 150); setLayout(null); } public void inc() { while (i <= 1000) { progressBar.setValue(i); i = i + 50; try { Thread.sleep(100); } catch (Exception e) {} } } public static void main(String[] args) { SwingDemo s = new SwingDemo(); s.setVisible(true); s.inc(); } }
这将产生以下输出 −
以下内容在控制台中可见 −
广告