Java Swing 绘制 gif 作为多个 JButton 和 JLabels 的背景

问题描述 投票:0回答:1

我想绘制一个 gif 作为我的 java 项目(学校用 Java swing 中的小游戏)的启动窗口的背景。到目前为止,这是我的代码:

package graphic;

import javax.swing.*;
import java.awt.*;
import java.io.File;

//Frame di inizio gioco usato per la selezione del numero di giocatori e la modalità di gioco
public class selectionWindow extends JFrame {

    //Panelli
    private final JPanel logoPanel = new JPanel();
    private final JPanel selectionPanel = new JPanel();
    private final JPanel offlinePanel = new JPanel();
    private final JPanel onlinePanel = new JPanel();
    private final JPanel foregroundPanel = new JPanel();
    private final JPanel backgroundPanel = new JPanel();
    //Componenti offline
    private final JButton offlineButton = new JButton("Offline");
    private final JButton offlineButton_twoPlayer = new JButton("2 Players");
    private final JButton offlineButton_threePlayer = new JButton("3 Players");
    private final JButton offlineButton_fourPlayer = new JButton("4 Players");
    private final Component[] offlineComponents;
    //Componenti online
    private final JButton onlineButton = new JButton("Online");
    private final JButton onlineButton_twoPlayer = new JButton("2 Players");
    private final JButton onlineButton_threePlayer = new JButton("3 Players");
    private final JButton onlineButton_fourPlayer = new JButton("4 Players");
    private final JButton joinButton = new JButton("Join Game");
    private final Component[] onlineComponents;;
    //Immagine decoration
    ImageIcon imageIcon = new ImageIcon( "graphics"+File.separator+"decoration"+File.separator+"BombermanLogo.png");
    JLabel logoLabel = new JLabel(imageIcon);
    //Immagine di sfondo
    ImageIcon backgroundGIF = new ImageIcon("graphics"+File.separator+"decoration"+File.separator+"background.gif");
    JLabel backgroundLabel = new JLabel(backgroundGIF);
    //Font
    Font pixelFont;

    public selectionWindow(){  // ForegroundPanel contiene logoPanel e selectionPanel ed ha un layout a griglia 2x1
        //Caratteristiche e comportamento della frame
        super("BomberMan Game");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setSize(800, 600);
        //this.setLayout(new FlowLayout());
        // Posiziona la finestra al centro dello schermo
        this.setLocationRelativeTo(null);

        //Background Panel
        backgroundPanel.add(backgroundLabel);
        this.add(backgroundPanel);
        //this.setComponentZOrder(backgroundPanel, 1);

        //Foreground Panel
        foregroundPanel.setLayout(new GridLayout(2,1));
        foregroundPanel.add(logoPanel);
        foregroundPanel.add(selectionPanel);
        this.add(foregroundPanel);
        //this.setComponentZOrder(backgroundPanel, 0);

        //JPanel per la decorazione
        logoPanel.add(logoLabel);

        //JPanel per la selezione
        selectionPanel.setLayout(new GridLayout(1,2));
        selectionPanel.add(offlinePanel);
        selectionPanel.add(onlinePanel);

        //JPanel per la selezione offline
        offlinePanel.setLayout(new BoxLayout(offlinePanel, BoxLayout.Y_AXIS));
        offlinePanel.add(offlineButton);
        offlinePanel.add(offlineButton_twoPlayer);
        offlinePanel.add(offlineButton_threePlayer);
        offlinePanel.add(offlineButton_fourPlayer);
        //Centrare gli elementi nel pannello (perchè il box layout altrimenti li allinea a sinistra)
        offlineButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineButton_twoPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineButton_threePlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineButton_fourPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineComponents = offlinePanel.getComponents(); //Lista dei componenti del panello offline (viene poi utilizzata per i listener)

        //JPanel per la selezione online
        onlinePanel.setLayout(new BoxLayout(onlinePanel, BoxLayout.Y_AXIS));
        onlinePanel.add(onlineButton);
        onlinePanel.add(onlineButton_twoPlayer);
        onlinePanel.add(onlineButton_threePlayer);
        onlinePanel.add(onlineButton_fourPlayer);
        onlinePanel.add(joinButton);
        //Centrare gli elementi nel pannello (perchè il box layout altrimenti li allinea a sinistra)
        onlineButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineButton_twoPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineButton_threePlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineButton_fourPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        joinButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineComponents = onlinePanel.getComponents(); //Lista dei componenti del panello online (viene poi utilizzata per i listener)

        //Carica il font
        pixelFont = new pixelFont().getPixelFont();

        //Gestione componenti offline
        offlineActionListener offlineAL = new offlineActionListener(offlineComponents,onlineComponents, this); ;
        for (Component component : offlineComponents){
            JButton button = (JButton) component;
            if (button != offlineButton) { //Escludo il bottone offline perchè deve rimanere sempre visibile e attivo
                button.setVisible(false);
                button.setEnabled(false);
            }
            button.setFont(pixelFont);
            button.setContentAreaFilled(false); //Rimuove il background del bottone
            button.setBorderPainted(false); //Rimuove il bordo del bottone
            button.addActionListener(offlineAL);
        }

        //Gestione componenti online
        onlineActionListener onlineAL = new onlineActionListener(onlineComponents,offlineComponents, this); ;
        for (Component component : onlineComponents){
            JButton button = (JButton) component;
            if (button != onlineButton) { //Escludo il bottone online perchè deve rimanere sempre visibile e attivo
                button.setVisible(false);
                button.setEnabled(false);
            }
            button.setFont(pixelFont);
            button.setContentAreaFilled(false); //Rimuove il background del bottone
            button.setBorderPainted(false); //Rimuove il bordo del bottone
            button.addActionListener(onlineAL);
        }
        //Modifica icona della finestra
        this.setIconImage(new ImageIcon("graphics"+File.separator+"decoration"+File.separator+"icon.png").getImage());
    }

    public static void main(String[] args) {
        selectionWindow window = new selectionWindow();
    }
}



我尝试使用 JLayeredPane 和 setComponentZOrder 方法,但我无法使它们工作,我错过了什么? 我 100% 确定所有地址都是正确的(如果我评论 this.add(foregroundPanel),gif 就会正确显示)。 感谢您的回复<3

我尝试使用 JLayeredPane 和 setComponenetZOrder 方法(在这些情况下,我还会添加 foregroundPanel.setOpaque(false)),期望它们以这种方式组织窗口,以便 foregroundPanel 的所有内容都可见并且 gif 会在其中播放背景不与任何其他组件重叠。 我尝试以多种不同的方式实现它们,有时完全改变 GUI 的结构,但我总是得到相同的两个结果:只有一个面板可见,或者背景面板位于前景面板之上,我只能看到按钮当我将鼠标悬停在它们上方时的一瞬间。

java swing background gif jlayeredpane
1个回答
0
投票

它不显示的原因是因为您将

foregroundPanel
放在了最上面。你称之为
this.add(backgroundPanel);
然后几行之后,你称之为
this.add(foregroundPanel);

//Background Panel
backgroundPanel.add(backgroundLabel);
this.add(backgroundPanel);
//this.setComponentZOrder(backgroundPanel, 1);

//Foreground Panel
foregroundPanel.setLayout(new GridLayout(2,1));
foregroundPanel.add(logoPanel);
foregroundPanel.add(selectionPanel);
this.add(foregroundPanel); // you just placed this panel over your background panel

我不确定你想要完成什么,但这绝对是错误的。

© www.soinside.com 2019 - 2024. All rights reserved.