java – JTabbedPane:在选项卡中显示任务进度
我有一个简单的Swing
Java应用程序执行搜索,结果显示在一个新选项卡中.搜索正在运行时,我想在选项卡的标题中显示进度图标或动画.我尝试添加一个gif图标,但它没有动画.有没有理由不这样做?
解决方法
Swing tutorial about progress bars(并显示一般进展)是一个非常好的起点.它向您展示了如何使用SwingWorker在工作线程上执行持久操作,并以特定间隔更新UI以向用户显示持久操作的进度.有关
SwingWorker and concurrency in Swing的更多信息,还有另一个教程可供使用
和往常一样,这个网站充满了例子.例如,a previous answer of mine使用SwingWorker类向用户显示进度 编辑 因为我错过了你问题标签部分的标题.您可以创建“进度图标”并在选项卡上进行设置.然后可以使用SwingWorker更新图标. 这种图标的一个示例是,基本上是每次进行某些进展时旋转的图像. tabbed pane tutorial向您展示了如何向选项卡添加图标(甚至使用自定义组件) EDIT2 因为看起来我的Mac与JDK1.7结合使得在其他系统上显示GIF动画更加容易,我创建了一个小型SSCCE,非常类似于Andrew,但带有一个看起来不像它的旋转图标我引用了’疯狂的黑猩猩’.旋转图标代码来自this site(我使用了精简版并添加了计时器).我唯一不高兴的事实是我需要将选项卡式窗格传递给旋转图标才能触发.可能的解决方案是将计时器拉到RotatingIcon类之外,但是,它只是一个SSCCE.图片不包括在内,但可以在Google上找到. import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTabbedPane; import javax.swing.Timer; import java.awt.Component; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; public class ProgressTabbedPane { public static void main( String[] args ) { EventQueue.invokeLater( new Runnable() { @Override public void run() { JFrame frame = new JFrame( "RotatingIcon" ); JTabbedPane tabbedPane = new JTabbedPane( ); tabbedPane.addTab( "Searching",new RotatingIcon( new ImageIcon( "resources/images/progress-indeterminate.png" ),tabbedPane ),new JLabel( new ImageIcon( "resources/images/rotatingIcon.gif" ) ) ); frame.getContentPane().add( tabbedPane ); frame.setDefaultCloSEOperation( JFrame.EXIT_ON_CLOSE ); frame.pack(); frame.setVisible( true ); } } ); } private static class RotatingIcon implements Icon{ private final Icon delegateIcon; private double angleInDegrees = 90; private final Timer rotatingTimer; private RotatingIcon( Icon icon,final JComponent component ) { delegateIcon = icon; rotatingTimer = new Timer( 100,new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { angleInDegrees = angleInDegrees + 10; if ( angleInDegrees == 360 ){ angleInDegrees = 0; } component.repaint(); } } ); rotatingTimer.setRepeats( false ); rotatingTimer.start(); } @Override public void paintIcon( Component c,Graphics g,int x,int y ) { rotatingTimer.stop(); Graphics2D g2 = (Graphics2D )g.create(); int cWidth = delegateIcon.getIconWidth() / 2; int cHeight = delegateIcon.getIconHeight() / 2; Rectangle r = new Rectangle(x,y,delegateIcon.getIconWidth(),delegateIcon.getIconHeight()); g2.setClip(r); AffineTransform original = g2.getTransform(); AffineTransform at = new AffineTransform(); at.concatenate(original); at.rotate(Math.toRadians( angleInDegrees ),x + cWidth,y + cHeight); g2.setTransform(at); delegateIcon.paintIcon(c,g2,x,y); g2.setTransform(original); rotatingTimer.start(); } @Override public int getIconWidth() { return delegateIcon.getIconWidth(); } @Override public int getIconHeight() { return delegateIcon.getIconHeight(); } } } 截图供参考.遗憾的是,图标不会在屏幕截图中旋转. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |