1
我希望JLabel
item3位於JPanel
的底部,因爲它有時出現在我在窗口中的第2個數組的頂部。我試圖使用item3.setVerticalAlignment(JLabel.BOTTOM)
。方法,但它不起作用。有什麼建議麼?如何將JLabel與JPanel的底部對齊
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);
}
這真的取決於你想要什麼最初生產。請查看以查看可用的不同類型的LayoutManagers及其示例:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – Matthew
其他答案告訴您可以做什麼,所以我只會解釋爲什麼你當前的解決方案不起作用。你誤解了'setVerticalAlignment(int)'做了什麼。它設置文本相對於標籤的位置,而不是標籤出現在其容器中的位置。 – gla3dr