JFrame中的图像相互覆盖,彼此之间不显示两个图像 [英] 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中的图像相互覆盖,彼此之间不显示两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文