简体   繁体   English

在另一个 JLabel 前面显示一个 JLabel

[英]Displaying a JLabel in front of another JLabel

 Game(){
    JFrame frame = new JFrame("Display Image");
    JPanel panel = (JPanel)frame.getContentPane();
    frame.setSize(1000,625);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel();
    label.setIcon(new ImageIcon("C:/Users/Ragnar/Desktop/GameBoard.png"));
    panel.add(label);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setVisible(true);
}

So i have this constructor ,and i want to add some new JLabels with Images,but i want them to be displayed on top of first image which is the image of the first jLabel label.Can anyone guide me how to achieve this please?I tryed to add them as usually but they are displayed behind the label.所以我有这个构造函数,我想添加一些带有图像的新 JLabel,但我希望它们显示在第一个图像的顶部,这是第一个 jLabel 标签的图像。谁能指导我如何实现这一点?我尝试像往常一样添加它们,但它们显示在标签后面。

If you have a background image and you want to display the JLabel on top of the background image, you can use a JPanel to hold the background image, then add your JLabel .如果您有背景图片并且想要在背景图片之上显示JLabel ,您可以使用JPanel来保存背景图片,然后添加您的JLabel

Usually if you try to let 2 JLabel overlap each other it won't succeed due to the default layout manager used by the container (such as FlowLayout in JPanel or BorderLayout in JFrame ).通常,如果您尝试让 2 个JLabel相互重叠,由于容器使用的默认布局管理器(例如JPanel FlowLayoutJFrame BorderLayout ),它不会成功。

If you really want to let them over lap, you will have to set the layout as null.如果您真的想让它们重叠,则必须将布局设置为空。 But they may introduce new problems as you lose control over the appearance of your components.但是当您无法控制组件的外观时,它们可能会引入新的问题。

Hence, in cases like this I would usually go for custom painting and draw the images you want in any particular order you are interested in.因此,在这种情况下,我通常会进行自定义绘画,并以您感兴趣的任何特定顺序绘制您想要的图像。

For example : How to create a background and foreground image which overlaps?例如如何创建重叠的背景和前景图像?

If you are working with eclipse, and you have installed the windowbuilder plugin you can use the graphical editing view.如果您正在使用 eclipse,并且您已经安装了 windowbuilder 插件,您可以使用图形编辑视图。

在此处输入图片说明

Within this view use the contextual menu to order the elements.在此视图中,使用上下文菜单对元素进行排序。 在此处输入图片说明

What worked for me was, when adding the components with the method add(component), adding them in order from front to back.对我有用的是,当使用 add(component) 方法添加组件时,按从前到后的顺序添加它们。 In the following example I add a lot of components to a JFrame, and the last one to be added is the wallpaper, so it stays at the background.在下面的示例中,我向 JFrame 添加了很多组件,最后添加的是墙纸,因此它留在背景中。

import java.awt.Image;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;




public class LogIn extends JFrame implements ActionListener{
    public static JFrame operador;`enter code here`
    private JLabel logo, foot, mensaje, wallpaper;
    private JTextField fldUser;
    private JPasswordField fldPass;
    private JButton entrar;
    private int ancho =400, largo= 530;
    public static String user="", pass="", name;

    public LogIn() {
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(ancho,largo);
        setResizable(false);
        setTitle("Acceso al sitema");
        setLocationRelativeTo(null);
        setIconImage(getIconImage());

        fldUser = new JTextField();
        fldUser.setHorizontalAlignment(JTextField.CENTER);
        fldUser.setBounds(125,320,150,25);
        fldUser.setBackground(new Color(50,50,255));
        fldUser.setForeground(Color.WHITE);
        fldUser.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
        add(fldUser);

        fldPass = new JPasswordField();
        fldPass.setHorizontalAlignment(JTextField.CENTER);
        fldPass.setBounds(125,360,150,25);
        fldPass.setBackground(new Color(50,50,255));
        fldPass.setForeground(Color.WHITE);
        fldPass.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
        add(fldPass);

        mensaje= new JLabel();
        mensaje.setBounds(0,390,ancho,15);
        mensaje.setForeground(Color.RED);
        mensaje.setHorizontalAlignment(SwingConstants.CENTER);
        add(mensaje);
        entrar =new JButton("Entrar");
        entrar.setBounds(125,410,150,40);
        entrar.setForeground(new Color(50,50,255));
        //entrar.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
        entrar.addActionListener(this);
        add(entrar);

        logo = new JLabel();
        logo.setBounds(50,0,300,300);
        ImageIcon imgLogo= new ImageIcon("src/images/DS.png");
        Icon iconoLogo = new ImageIcon(imgLogo.getImage().getScaledInstance(logo.getWidth(),logo.getHeight(), Image.SCALE_DEFAULT));
        logo.setIcon(iconoLogo);


        foot = new JLabel("Desarrollado por Gabriel Santos");
        foot.setBounds((ancho-200)/2,largo-60,200,30);

    //When JLabels overlap, the ones that come to the front are the first to be added to the window.
        add(foot);
        add(logo);

        wallpaper = new JLabel();
        wallpaper.setBounds(0,0,window.getWidth(),window.getHeight());
        ImageIcon img = new ImageIcon("src/images/wallpaperPrincipal.jpg");
        Icon icono = new ImageIcon(img.getImage().getScaledInstance(this.getWidth(),this.getHeight(), Image.SCALE_DEFAULT));    
        wallpaper.setIcon(icono);

        add(wallpaper);

        setVisible(true);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM