Images In JFrame are overwriting each other not displaying both images over eachother(JFrame中的图像相互覆盖,而不是相互显示两个图像)
问题描述
public class Board extends JFrame
{
public void bd()
{
JFrame frame=new JFrame();
JLabel background1 = new JLabel(new ImageIcon("background.png"));
JLabel knight=new JLabel(new ImageIcon("knight.jpg"));
frame.add(background1);
frame.add(knight);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
}
我的代码遇到了一些问题
I've been having some trouble with my code
当我添加骑士图像时,背景图像将消失,只有骑士图像出现.我如何使图像重叠或使背景图像像背景一样
when i add the knight image the background image will disappear and only the knight image appears. How would i make the images overlap or make the background image act like a background
推荐答案
JLabel background1 = new JLabel(new ImageIcon("background.png"));
JLabel knight=new JLabel(new ImageIcon("knight.jpg"));
frame.add(background1);
frame.add(knight);
我猜您真正想要的是将骑士显示在顶部(在 z 轴上),因此您需要执行以下操作:
I'm guessing what you really want is have the knight displayed on top (on the z axis) so you need to do something like:
JLabel background1 = new JLabel(new ImageIcon("background.png"));
background.setLayout( new BorderLayout() );
JLabel knight=new JLabel(new ImageIcon("knight.jpg"));
background1.add(knight);
frame.add(background1);
也就是说,您需要遵循父/子层次结构:
That is you need to follow a parent/child hierarchy:
- 为框架添加背景
- 将骑士添加到背景中
这篇关于JFrame中的图像相互覆盖,而不是相互显示两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JFrame中的图像相互覆盖,而不是相互显示两个图像
基础教程推荐
- Maven:无效的目标版本:10 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
