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中的图像相互覆盖,而不是相互显示两个图像


- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01