Having a JLabel on top of another JLabel that has an image

↘锁芯ラ 提交于 2019-12-02 07:48:37

You could make use of an OverlayLayout or you could simply add the second JLabel to the first. The trick here though, is to set up a layout manager for the first label.

Beware though, a JLabel only use the icon and text properties its determine the preferred layout size, which could end up truncating it's children

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                JLabel background = new JLabel(new ImageIcon(ImageIO.read(...)));
                background.setLayout(new BorderLayout());
                JLabel text = new JLabel("I'm just drawn this way");
                text.setFont(text.getFont().deriveFont(128f));
                text.setHorizontalAlignment(JLabel.RIGHT);
                text.setVerticalAlignment(JLabel.BOTTOM);
                text.setForeground(Color.WHITE);

                background.add(text);

                add(background);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

}

i do the below way in my GUI project: 1.make a jPanel with OverlayLayout 2.put two jlabels into the jPanel 3.make a keyEvent or something like that

enter image description here enter image description here my GUI project example code:

    private void jLabel2KeyPressed(java.awt.event.KeyEvent evt) {
int i = evt.getKeyCode();
  switch (i) {
 // some parts of my code }
if(k5==true&&ke==true&&k1==false&&k3==false) {
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/audit/images/tie.png")));

 jLabel177.setIcon(new javax.swing.ImageIcon(getClass().getResource("/audit/images/tie1forbigroad.png")));
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!