Swing中的旋转轮 [英] Rotating wheel in Swing

查看:52
本文介绍了Swing中的旋转轮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have searched here but did not get my answer (though I feel it should be here).

我想执行一些活动(但我不知道完成需要多长时间),并且在运行此任务时,我想向用户显示一个带有消息正在处理...请稍等".一旦活动完成,旋转轮也应该消失.

I want to perform some activity(but I don't know how much time it will take to complete) and while this task is being run, I want to show a rotating wheel to the user with a message "Processing...please wait". Once the activity gets completed,rotating wheel should also disappear.

如何实现这个功能?

推荐答案

很多都归结为您想要实现的目标以及您想要进行多少定制和工作.

A lot will come down to what it is you want to achieve and how much customization and work your want to go to.

您可以使用 ImageIcon 加载 gif 并将其应用到 JLabel 以实现您想要的效果.此示例演示了如何使用自定义绘画和 javax.swing.Timer 为等待周期设置动画.

You could just load a gif with ImageIcon and apply it to a JLabel to achieve the effect you're after. This example demonstrates the use of custom painting and a javax.swing.Timer to animate the wait cycle.

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 继续绘制,您需要创建某种后台线程来执行事件调度线程之外的工作,直到您完成...

In order for Swing to continue painting, you will need to create a background thread of some kind to perform the work outside of the Event Dispatching Thread until you are finished...

例如

SwingWorker worker = new SwingWorker() {

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

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

};

有关更多详细信息,请查看Swing 中的并发 了解更多详细信息

For more details, check out Concurrency in Swing for more details

这篇关于Swing中的旋转轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆