[英]How to align a JLabel to the bottom of a JPanel
我希望JLabel
item3位於JPanel
的底部,因為有時它會出現在窗口中我擁有的2d數組的頂部。 我試圖使用item3.setVerticalAlignment(JLabel.BOTTOM)
。 方法,但是沒有用。 有什么建議么?
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);
}
您可以使用BorderLayout
並將標簽添加到容器的PAGE_END
位置
setLayout(new BorderLayout());
add(itemLabel, BorderLayout.PAGE_END);
當然,您需要設置水平對齊方式,以實現標簽的JPanel
FlowLayout
容器最初生成的沿X軸的對齊方式
JLabel itemLabel = new JLabel("ccc", JLabel.CENTER);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.