首页 > 解决方案 > Gif 在 jpanel GUI 的背景中没有“移动”并覆盖 jbuttons

问题描述

我试图让这个 gif 在我的 jpanel 中处于活动状态,但是每当我运行程序时,面板只显示图像而不是运动。我曾在一个单独的场合使用框架而不是面板尝试过这个,它有效(不确定它们是否相关)。回顾图像显示,但作为静止图像而不是“gif”

更新:我现在可以使用 gif,但它涵盖了诸如“客户端”“帮助”和“所有者”之类的按钮,是否可以将 gif 一直放在后面,并且按钮位于 gif 之上?

-代码

public void panel1() {
        ImagePanel panel = new ImagePanel(new ImageIcon("MenuBG.gif").getImage());
        firstPanel = new JPanel();
        firstPanel.setLayout(null);
        client = new JButton("Client");
        owner = new JButton("Owner");
        client.setSize(90, 20);
        client.setLocation(xPosAlignLeft, 50);
        owner.setSize(90, 20);
        owner.setLocation(xPosAlignLeft, 115);
        firstPanel.add(client);
        firstPanel.add(owner);
        createHelpButton();
        help.setSize(90, 20);
        help.setLocation(xPosAlignLeft, 180);
        firstPanel.add(help);
        firstPanel.add(panel);
    }
    class ImagePanel extends JPanel {
        
private Image img;
public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
    this.img = img;
    Dimension size= new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
}
public void paintComponent(Graphics g) {
    g.drawImage(img,0,0,null);
}
    }

标签: javaswingjframejpanel

解决方案


推荐阅读