JLabel is in a wrong position in my JPanel
I have a class which extends JPanel and I want to add some labels (actually a class which extends JLabel) on it. The layout of this panel in null. i've set the size and the location of my label, but the problem is that i can't see it in the corr开发者_如何学编程ect place :( I printed getComponentAt(....).getClass().getName() in console to see what really exists in that location and got the correct answer (i mean getComponent says that there is a lable in the place where i added it, but the problem is that i see my label in position (0,0) which is wrong ) :-S i can't find any mistake in my code :-/ Any help is appreciated, thanks in advance. :) This the relevant part of my code:
public class ServerViewManager extends JPanel implements Serializable {
private ArrayList<String> map;
// *tankview extends JLabel
private ArrayList<TankView> tanks = new ArrayList<TankView>();
private ArrayList<BulletView> bullets = new ArrayList<BulletView>();
private int rows;
private int columns;
public ServerViewManager(ArrayList<String> map) {
super(null);
this.map = map;
rows = map.size();
columns = map.get(0).length();
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map.get(i).length(); j++) {
if (map.get(i).charAt(j) == 'i')
add(new IceBlock(i, j));
else if (map.get(i).charAt(j) != 'g')
add(new Block(map.get(i).charAt(j), i * 50, j * 50));
}
}
}
public void paintComponent(Graphics g) {
g.drawImage(Resources.GROUNDBLOCK.getImage(), 0, 0, columns * 50,
rows * 50, this);
}
//************************************************
// Here is where i wanna put the label
public void addTank(Color color, int xpos, int ypos) {
tanks.add(new TankView(color, xpos, ypos));
if (iceBlock(xpos, ypos)) {
IceBlock ice = (IceBlock) this.getComponentAt(ypos * 50, xpos * 50);
ice.putItem(color + "1");
} else
this.add(tanks.get(tanks.size() - 1));
repaint();
}
and here is my label:
public class TankView extends JLabel implements Serializable{
private int xpos;
private int ypos;
private char direction;
private int directionNum;
private Color color;
public TankView(Color color, int x, int y) {
xpos=x;
ypos=y;
direction='u';
directionNum=1;
setLocation(ypos *50, xpos*50);
setSize(50, 50);
setOpaque(false);
setVisible(true);
this.color=color;
if (color==Color.Blue)
setIcon(Resources.BlueTank1);
else if (color==Color.Green)
setIcon(Resources.GreenTank1);
else if (color==Color.Red)
setIcon(Resources.OrangeTank1);
else if (color==Color.Yellow)
setIcon(Resources.PinkTank1);
repaint();
}
any of JComponents must be visible or you have to call pack(); to the top-level container, otherwise returns zero value for getBounds(); or getSize or getWhatever
it may seems ridiculous, but i just made the methods below a comment and the problem was solved :O I don't really know why this happened. i hadn't even called them :-?? i was trying to post an SSCCE and i realized that everything was OK. Therefore I began making my own program exactly like the one i wanted to post (by making methods a comment)
// public int getX(){
// return xpos;
// }
//
// public int getY(){
// return ypos;
// }
these methods had to return the position of my tanks. (They were in TankView class)
精彩评论