【问题标题】:JPanel are not repainting in mouseClicked methodJPanel 不在 mouseClicked 方法中重绘
【发布时间】:2016-11-30 21:07:20
【问题描述】:

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

动画 GIF 示例:

动画将在 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;
    }
}

【问题讨论】:

  • 您是否尝试过将 mouseClicked() 中的代码移动到 mousePressed() 中? mouseClicked() 是 mousePressed() 和 mouseReleased() 事件。
  • 代码无法在此处编译的原因似乎有很多:1) 为了尽快获得更好的帮助,请发布minimal reproducible example 或Short, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。

标签: java swing animated-gif


【解决方案1】:
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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-03
    • 2021-10-30
    • 2015-03-09
    • 1970-01-01
    • 2014-10-02
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多