gpt4 book ai didi

java - 分层 JLabel 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:27 24 4
gpt4 key购买 nike

当我尝试对 JLabel 进行分层时,最终结果是它们彼此相邻出现,gif 与 Logo 图像重叠,即使我告诉它位于底层。

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);
}
}

在此先感谢您的帮助!

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

最佳答案

阅读 How to Use Layered Panes 上的 Swing 教程部分对于一个在另一个之上显示多个图层的工作示例。

当您将标签“添加”到分层 Pane 时,您需要指定要将标签添加到的“层”。本教程解释了分层的工作原理。

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

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

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

关于java - 分层 JLabel 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29585864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com