旋转中的旋转轮

时间:2013-07-11 06:02:58

标签: java swing progress

我在这里搜索但没有得到我的答案(虽然我觉得它应该在这里)。

我想要执行一些活动(但我不知道需要多长时间才能完成)并且在运行此任务时,我想向用户显示一个旋转轮,并显示消息“Processing .. 。请耐心等待”。一旦活动完成,旋转轮也应该消失。

如何实现此功能?

2 个答案:

答案 0 :(得分:3)

我似乎记得,Swing中的GlassPane可用于:

  1. 拦截用户输入。
  2. 显示图像/动画
  3. 有人已经实现了这一点:http://www.curious-creature.org/2005/02/15/wait-with-style-in-swing/

    页面顶部有源代码的下载链接。

    另请参阅:http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

    它更详细地解释了GlassPane。

答案 1 :(得分:3)

很多事情将归结为您想要实现的目标以及您希望获得多少定制和工作。

您只需加载带有ImageIcon的gif并将其应用于JLabel即可获得您所效果的效果。此示例演示如何使用自定义绘制和javax.swing.Timer来设置等待周期的动画。

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Waiting {

    public static void main(String[] args) {
        new Waiting();
    }

    public Waiting() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Timer paintTimer;
        private float cycle;
        private boolean invert = false;

        public TestPane() {
            paintTimer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    cycle += 0.05f;
                    if (cycle > 1f) {
                        cycle = 0f;
                        invert = !invert;
                    }
                    repaint();
                }
            });
            paintTimer.setRepeats(true);
            setRuning(true);
        }

        public void setRuning(boolean running) {
            if (running) {
                paintTimer.start();
            } else {
                paintTimer.stop();
            }
        }

        public boolean isRunning() {
            return paintTimer.isRunning();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(40, 40);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (isRunning()) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
                g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

                int width = getWidth() - 1;
                int height = getHeight() - 1;
                int radius = Math.min(width, height);

                int x = (width - radius) / 2;
                int y = (height - radius) / 2;
                int start = 0;
                int extent = Math.round(cycle * 360f);

                if (invert) {
                    start = extent;
                    extent = 360 - extent;
                }

                g2d.setColor(Color.RED);
                g2d.fill(new Arc2D.Float(x, y, radius, radius, start, extent, Arc2D.PIE));
                g2d.setColor(Color.YELLOW);
                g2d.draw(new Arc2D.Float(x, y, radius, radius, start, extent, Arc2D.PIE));
                g2d.dispose();
            }
        }
    }
}

为了让Swing继续绘画,您需要创建某种背景线程来执行Event Dispatching Thread之外的工作,直到完成...

例如

SwingWorker worker = new SwingWorker() {

    @Override
    protected Object doInBackground() throws Exception {
        // Done some work...
    }

    @Override
    protected void done() {
        // stop the animation...
    }

};

有关详情,请查看Concurrency in Swing了解详情