Pergunta

I would like the JLabel item3 to be located at the bottom of the JPanel, since sometimes it appears on top of the 2d array I have in the window. I tried to use the item3.setVerticalAlignment(JLabel.BOTTOM). method but it did not work. Any suggestions?

public class Sokoban extends JPanel {

private Contents[][] field;
private Rectangle2D.Double r1;
private static final Color[] colours = {
        Color.BLACK, Color.RED, Color.GREEN, Color.CYAN, Color.MAGENTA, Color.BLUE, Color.YELLOW
    };

private LevelReader lr = new LevelReader();
private int currentLevel;
private Contents [][] levelData;
private int countMoves = 1;
JLabel item3 = new JLabel("ccc");


long time1;


public Sokoban(){
    lr.readLevels("m1.txt");

    this.setPreferredSize(new Dimension(900,500));
    this.setFocusable(true);
    this.requestFocus();
    this.addKeyListener(new MyKeyListener());
    initLevel(0);
    this.add(item3);
    item3.setVerticalAlignment(JLabel.BOTTOM);





}

public static void main (String[] args) {
    JFrame f = new JFrame("Sokoban");
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLayout(new FlowLayout());

    f.add(new Sokoban());


    f.pack();
    f.setVisible(true);  


}
Foi útil?

Solução

You could use BorderLayout and add the label to the PAGE_END position of the container

setLayout(new BorderLayout());
add(itemLabel, BorderLayout.PAGE_END);

Of course you'll need to set the horizontal alignment to achieve the alignment along the X-axis as produced originally by the label's JPanel FlowLayout container

JLabel itemLabel = new JLabel("ccc", JLabel.CENTER);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top