分层 JLabel 不起作用

Layered JLabels not working

当我尝试对 JLabel 进行分层时,最终结果是它们并排出现,gif 与徽标图像重叠,即使我告诉它位于底层。

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.io.*;
class GameFrame extends JFrame
{
    ImageIcon logo, bg;
    JTextPane l1;
    JTextArea l2;
    JLabel i1, i2;
    JButton b1;
    GridBagLayout g;
    int count;
    JLayeredPane jlp;
    GameFrame() throws IOException, UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        super("Simple2.0");
        setSize(400, 250);
        //setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        count = 0;
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        jlp = new JLayeredPane();
        jlp.setSize(400, 250);
        jlp.setVisible(false);

        bg = new ImageIcon("fire.gif");
        i2 = new JLabel(bg);
        i2.setBackground(new Color(0, 0, 0, 0));
        i2.setVisible(false);
        logo = new ImageIcon("logo.png");
        i1 = new JLabel(logo);
        i1.setBackground(new Color(0, 0, 0, 0));
        i1.setVisible(false);

        g =  new GridBagLayout();
        GridBagConstraints gb = new GridBagConstraints();
        gb.gridy = 1;
        gb.insets = new Insets(10, 0, 0, 0);
        gb.anchor = GridBagConstraints.SOUTH;

        setLayout(g);

        l1 = new JTextPane();
        l1.setBackground(getBackground());
        l1.setEnabled(false);
        l1.setDisabledTextColor(Color.BLACK);
        l1.setSize(350, 200);
        l1.setText("Hello and welcome to SIMPLE!");

        l2 = new JTextArea();
        l2.setSize(350, 200);
        l2.setBackground(getBackground());
        l2.setEnabled(false);
        l2.setDisabledTextColor(Color.BLACK);
        l2.setLineWrap(true);
        l2.setWrapStyleWord(true);
        l2.setVisible(false);

        b1 = new JButton("continue");

        b1.addActionListener((ActionEvent e) ->
        {
        if(count == 0)
        {
            l1.setVisible(false);
            l2.setText("    This game was a rework of a text based game made by me and a friend of mine during our first semester in Java.");
            l2.setVisible(true);
            count++;
        }
        else if(count == 1)
        {
            l2.setText("    It was a simple attempt to make a combat and inventory system that would function within a Java operated terminal, and was"
                    + " full of bugs, errors, and long workarounds to problems that can now easily be solved with our new ability.");
            count++;
        }
        else if(count == 2)
        {
            l2.setVisible(false);
            l1.setText("And as such I present our new work, SIMPLE.");
            l1.setVisible(true);
            count++;
        }
        else
        {
            l1.setVisible(false);
            b1.setVisible(false);
            i1.setVisible(true);
            i2.setVisible(true);
            jlp.setVisible(true);
        }
    });

    jlp.add(i1, 0);
    jlp.add(i2, 1);
    add(l1);
    add(l2);
    add(i1);
    add(i2);
    add(b1, gb);
    add(jlp);
    setVisible(true);
}
}

在此先感谢您的帮助!

编辑:我添加了特定的层,但没有看到结果发生变化,因为这似乎是一个很明显的问题。

阅读 Swing 教程中关于 How to Use Layered Panes 的部分,了解一个在彼此之上显示多个层的工作示例。

当您 "add" 将标签添加到分层窗格时,您需要指定要将标签添加到的 "layer" 。本教程解释了分层的工作原理。

此外,在查看教程的同时,了解构建程序的更好方法,以便:

  1. GUI 是在事件调度线程上创建的
  2. 您没有扩展 JFrame。

最好从工作示例开始,然后根据您的要求慢慢定制它。