当我尝试对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);
}
}
提前感谢您的帮助!
编辑:我添加了特定的图层,但没有看到结果的变化,因为它会立刻出现问题。
答案 0 :(得分:1)
阅读How to Use Layered Panes上Swing教程中的部分,了解一个显示多个图层的工作示例。
当你"添加"在分层窗格的标签上,您需要指定"层"你想要添加标签。该教程解释了分层的工作原理。
此外,在查看教程时,请查看构建程序的更好方法,以便:
最好从工作示例开始,然后根据您的要求慢慢定制。