Reputation: 402
I have a JPanel using GridLayout which contains 6 JLabels. If i add only this JPanel to a JFrame everything works fine, but when I add it on BorderLayout.WEST (along with 3 other panels on EAST, CENTER and SOUTH) it just won't show up.
Here's the code I'm using:
public class SwingView extends JFrame {
private DeckLabel[] terrains={
new DeckLabel(new ImageIcon("assets/graphics/mountains.png"),0),
new DeckLabel(new ImageIcon("assets/graphics/planes.png"),1),
new DeckLabel(new ImageIcon("assets/graphics/forest.png"),2),
new DeckLabel(new ImageIcon("assets/graphics/fields.png"),3),
new DeckLabel(new ImageIcon("assets/graphics/swamp.png"),4),
new DeckLabel(new ImageIcon("assets/graphics/desert.png"),5)};
public SwingView() {
super("Frame");
this.setSize(680, 740);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Utilities.center(this);
// panels
//terrains
JPanel terrainsPanel = new JPanel();
terrainsPanel.setSize(100, 640);
terrainsPanel.setLayout(new GridLayout(6, 1));
//map
JPanel mapPanel = new JPanel();
mapPanel.setSize(480, 640);
//info
JPanel infoPanel = new JPanel();
infoPanel.setSize(100, 640);
//chat
JPanel chatPanel = new JPanel();
chatPanel.setSize(680, 100);
chatView.setEditable(false);
txtChat.addKeyListener(this);
chatPanel.add(txtChat, BorderLayout.SOUTH);
chatPanel.add(chatView);
// terrains
for (int i = 0; i < 6; i++) {
terrainsPanel.add(terrains[i]);
}
this.add(mapPanel,BorderLayout.CENTER);
this.add(terrainsPanel, BorderLayout.WEST);
this.add(infoPanel, BorderLayout.EAST);
this.add(chatPanel, BorderLayout.SOUTH);
}
}
public class DeckLabel extends JLabel{
private Image image;
private int index;
public DeckLabel(ImageIcon icon,int index){
this.image=icon.getImage();
this.index=index;
}
@Override
public void paint(Graphics g){
BufferedImage bi = new BufferedImage(85, 78,
BufferedImage.TYPE_INT_RGB);
Graphics2D tg = bi.createGraphics();
tg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// draw basic tile
tg.drawImage(image, 0, 0, null);
g.drawImage(bi,0,0,null);
}
}
Thank you
Upvotes: 1
Views: 2089
Reputation: 1210
As your DeckLabel class sets no text for the JLabel, the component has no minimum and preferred size. So the border layout will assume a size of 0 -> component not visible. The same happens when you use a JPanel.
Call setPreferredSize(), setMinimumSize() or override getMinimumSize()/getPreferredSize().
Upvotes: 2
Reputation: 109813
plenty issues
Upvotes: 3