JPanel没有在mouseClicked方法中重新绘制

时间:2016-11-30 21:07:20

标签: java swing animated-gif

我需要在运行一些代码的MouseListener.mouseClicked方法中运行gif图像。但是当运行gif图像的mouseClicked方法是静态的时。

动画GIF示例:

gif image

动画将在mouseClicked结束后启动。对于gif我使用LayerUI<JPanel>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.LayerUI;

public class Test {
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().createUI();
            }
        });
    }

    private JButton mOrderButton;

    public void createUI() {
        JFrame f = new JFrame ();

        final WaitLayerUI layerUI = new WaitLayerUI(f);
        JPanel panel = createPanel();
        JLayer<JPanel> jlayer = new JLayer<JPanel>(panel, layerUI);

        mOrderButton.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                layerUI.start();
                // response imitation from server
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                layerUI.stop();
            }

            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
        });

        f.add (jlayer);
        f.setSize(500, 500);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible (true);
    }

    private JPanel createPanel() {
        JPanel p = new JPanel();
        mOrderButton = new JButton("btn");
        p.add(mOrderButton);
        return p;
    }
}

class WaitLayerUI extends LayerUI<JPanel> {
    private static final int IMAGE_WIDTH = 120;
    private static final int IMAGE_HEIGHT = 105;
    private static final String IMAGE_PATH = "/busy_indicator_login_window.gif";
    private final Image busyIndicatorImage;
    private final JFrame loginWindowFrame;
    private boolean isRunning;

    public WaitLayerUI(JFrame loginWindowFrame) {
        busyIndicatorImage = new ImageIcon(this.getClass().getResource(IMAGE_PATH)).getImage();
        this.loginWindowFrame = loginWindowFrame;
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        super.paint(g, c);

        if (!isRunning) {
            return;
        }

        Graphics2D g2 = (Graphics2D) g.create();
        g2.setColor(new Color(0, 0, 0, 0.1f));
        g2.fillRect(0,0,500, 500);

        g2.drawImage(busyIndicatorImage,
                loginWindowFrame.getWidth() / 2 - IMAGE_WIDTH / 2,
                loginWindowFrame.getHeight() / 2 - IMAGE_HEIGHT / 2,
                IMAGE_WIDTH, IMAGE_HEIGHT, loginWindowFrame);

        g2.dispose();
    }

    public void start() {
        isRunning = true;
    }

    public void stop() {
        isRunning = false;
    }
}

1 个答案:

答案 0 :(得分:0)

public void mouseClicked(MouseEvent e) {
    new Thread(new Runnable() {
        layerUI.start();
        // response imitation from server
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        layerUI.stop();
    }).start();
}