netbeans创建桌面应用程序-状态栏的应用(进度条、busyicon和message)

   用netbeans新建桌面应用程序时会自动生成状态栏,包括一个进度条一个busyicon和一个信息提示。最初我看的很纠结,不知道应该如何让这些东西动起来。研究了一下nb生成的代码,总算会用了,分享给大家。

   先看看生成的代码:

 // status bar initialization - message timeout, idle icon and busy animation, etc ResourceMap resourceMap = getResourceMap(); int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout"); messageTimer = new Timer(messageTimeout, new ActionListener() { public void actionPerformed(ActionEvent e) { statusMessageLabel.setText(""); } }); messageTimer.setRepeats(false); int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate"); for (int i = 0; i < busyIcons.length; i++) { busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]"); } busyIconTimer = new Timer(busyAnimationRate, new ActionListener() { public void actionPerformed(ActionEvent e) { busyIconIndex = (busyIconIndex + 1) % busyIcons.length; statusAnimationLabel.setIcon(busyIcons[busyIconIndex]); } }); idleIcon = resourceMap.getIcon("StatusBar.idleIcon"); statusAnimationLabel.setIcon(idleIcon); progressBar.setVisible(false); // connecting action tasks to status bar via TaskMonitor TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext()); taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); if ("started".equals(propertyName)) { if (!busyIconTimer.isRunning()) { statusAnimationLabel.setIcon(busyIcons[0]); busyIconIndex = 0; busyIconTimer.start(); } progressBar.setVisible(true); progressBar.setIndeterminate(true); } else if ("done".equals(propertyName)) { busyIconTimer.stop(); statusAnimationLabel.setIcon(idleIcon); progressBar.setVisible(false); progressBar.setValue(0); } else if ("message".equals(propertyName)) { String text = (String)(evt.getNewValue()); statusMessageLabel.setText((text == null) ? "" : text); messageTimer.restart(); } else if ("progress".equals(propertyName)) { int value = (Integer)(evt.getNewValue()); progressBar.setVisible(true); progressBar.setIndeterminate(false); progressBar.setValue(value); } } });

 

是不是很纠结呢……如果单独用progressBar的话还是很简单的:

分别用setMinimum setMaxmum 方法来设定进度条的起止量,用setValue更新进度条的显示。 setIndeterminate可以用于不确定的任务长度,你会看到进度条在不断摇摆。

 

   上面的代码如果仔细看看也不难,主要是用两个timer分别实现了busyicon和定时清楚提示信息。用法如下:

//不确定模式 //这样你就可以看到进度条在摇摆,busyicon在转,信息显示了一会消失 this.progressBar.setVisible(true); this.progressBar.setIndeterminate(true); this.statusMessageLabel.setText("I am busy!"); this.messageTimer.start(); this.busyIconTimer.start(); //确定模式 //进度条每秒走十分之一 this.progressBar.setVisible(true); this.progressBar.setMaximum(10); this.progressBar.setMinimum(0); //this.progressBar.setIndeterminate(true); this.statusMessageLabel.setText("I am busy!"); this.messageTimer.start(); this.busyIconTimer.start(); new Timer(1000,new ActionListener() { public void actionPerformed(ActionEvent e) { progressBar.setValue(i++); } }).start();

 

不过很明显到目前为止我们还没有用到最初代码中的后半部分:taskMonitor.addPropertyChangeListener。。。

这个涉及到org.jdesktop.application.TaskMonitor类的用法,靠大家去探索了。。。

你可能感兴趣的:(timer,String,Integer,animation,Netbeans,initialization)