gpt4 book ai didi

java - 将 JLabel 移到前面

转载 作者:行者123 更新时间:2023-11-29 03:45:50 25 4
gpt4 key购买 nike

我正在放置两张图片。一张应该是背景图片,另一张应该是火柴人的图片。我想把简笔画放在背景前面。我可以通过在显示简笔画的代码之后插入代码来放置背景图片来实现这一点。我想知道是否有办法完成同样的事情,通过在背景代码之后插入火柴人代码,这样我就可以继续在背景之上放置新的 JLabel。

有效代码:

    guy.setBounds(0,0,100,100);
panel.add(guy);

backgroundPic.setBounds(0,0,550,550);
panel.add(backgroundPic);

setVisible(true);

我想使用的代码:

    backgroundPic.setBounds(0,0,550,550);
panel.add(backgroundPic);

guy.setBounds(0,0,100,100);
panel.add(guy);


setVisible(true);

最佳答案

如果您只担心一张背景图片,那么我建议扩展 JPanel,覆盖 paintComponent 并绘制背景图片。

如果你必须关心几个组件的Z-order,那么JLayeredPane是您的最佳选择。

这是显示第一个选项的小片段:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test2 {
private static class PanelWithBackground extends JPanel {

private Image backgroundImage;
private Point backgroundLocation = new Point();

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (getBackgroundImage() != null) {
g.drawImage(getBackgroundImage(), backgroundLocation.x, backgroundLocation.y, this);
}
}

public Image getBackgroundImage() {
return backgroundImage;
}

public void setBackgroundImage(Image backgroundImage) {
this.backgroundImage = backgroundImage;
repaint();
}

public Point getBackgroundLocation() {
return backgroundLocation;
}

public void setBackgroundLocation(Point backgroundLocation) {
this.backgroundLocation = backgroundLocation;
repaint();
}
}

protected static void initUI() throws MalformedURLException {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PanelWithBackground panelWithBackground = new PanelWithBackground();
panelWithBackground.setLayout(null);
panelWithBackground.setBackgroundImage(new ImageIcon(new URL(
"http://2.bp.blogspot.com/-PqKuKt5mRmc/Tvi-K-4FVVI/AAAAAAAACKg/YwzkME5gGvk/s1600/black+background.jpg")).getImage());
JLabel guy = new JLabel(new ImageIcon(new URL(
"http://www.clipproject.info/Cliparts_Free/Menschen_Free/Clipart-Cartoon-Design-04.gif")));
// Next 2 lines should rather be performed by a LayoutManager
panelWithBackground.setPreferredSize(new Dimension(1024, 768));
guy.setBounds(50, 200, guy.getPreferredSize().width, guy.getPreferredSize().height);

panelWithBackground.add(guy);
frame.add(panelWithBackground);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
try {
initUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

关于java - 将 JLabel 移到前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10961023/

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