[英]Paint JLabel above another JLabel without JLayeredPane
Here's my application structure : one JFrame ---> which contain one Board1 (Board1 extends JPanel) ---> which contains some "Zone" (Zone extends JLabel with FlowLayout manager) ----> which contain some "Personnage" (extends JLabel) 这是我的应用程序结构:一个JFrame --->其中包含一个Board1(Board1扩展了JPanel)--->其中包含一些“区域”(Zone使用FlowLayout manager扩展了JLabel)---->其中包含一些“人事”(扩展JLabel)
First I create all my JLabels in the JPanel's class: 首先,我在JPanel的类中创建所有的JLabel:
public class Board1 extends JPanel implements Board {
private List<Zone> zones = new ArrayList<Zone>();
private List<Personnage> personnages = new ArrayList<Personnage>();
public Board1() {
this.setLayout(null);
zones.add(new Zone(1, false, true, null, "/zone1D1C.jpg", this));
zones.add(new Zone(2, false, false, null, "/zone2D1C.jpg", this));
zones.add(new Zone(3, false, false, null, "/zone3D1C.jpg", this));
zones.add(new Zone(4, true, false, null, "/zone4D1C.jpg", this));
zones.add(new Zone(5, false, false, null, "/zone5D1C.jpg", this));
zones.add(new Zone(6, true, false, null, "/zone6D1C.jpg", this));
zones.add(new Zone(7, true, false, null, "/zone7D1C.jpg", this));
zones.add(new Zone(8, false, false, null, "/zone8D1C.jpg", this));
personnages.add(new Survivant("Phil", zones.get(0), 3, "/phil.jpg"));
for (Zone zone : zones) {
this.add(zone);
for (Personnage personnage : zone.getPersonnages()) {
zone.add(personnage);
}
}
}
public void move_personnage(Zone zone) {
running_personnage.moveZone(zone);
zone.add(running_personnage);
this.repaint();
}
public void try_add_personnage() {
Personnage douglas = new Survivant("Douglas", zones.get(3), 3, "/douglas.jpg");
zones.get(3).add(douglas);
this.repaint();
}
}
The constructor is working good, I see my "personnage" above the "zone". 构造函数运行良好,我在“区域”上方看到我的“人物”。 move_personnage method is working too !!! move_personnage方法也起作用! It remove automatically the personnage from the previous zone. 它将自动从先前区域中删除人员。
The problem is when I'm running try_add_personnage method, there is no JLabel above the zone. 问题是当我运行try_add_personnage方法时,该区域上方没有JLabel。 However I checked that the method is called etc... Whatever I do after the Board1 constructor, I can't add any kind of new visible Personnage above Zone. 但是,我检查了该方法是否被调用等等。在Board1构造函数之后,无论做什么,我都无法在Zone上方添加任何新的可见Personnage。
I guess it's with paintComponent method or something like this, but I didn't find the solution. 我猜是用paintComponent方法或类似的方法,但是我没有找到解决方案。 I know that's possible with JLayeredPane but I don't want to use it because I will use a GridBagLayout manager for the board in the future. 我知道JLayeredPane可以实现,但我不想使用它,因为将来我将为板使用GridBagLayout管理器。
I Hope I was understandable 我希望我可以理解
I found the solution. 我找到了解决方案。 It was my repaint which wasn't enough, I had to make JPanel.revalidate();
仅仅重JPanel.revalidate();
是不够的,我必须制作JPanel.revalidate();
and after JPanel.repaint();
和JPanel.repaint();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.