我有两个JPanel,我是在左上角,但由于某种原因,它们位于y轴的一半(但仍然离开,因此在x轴上为0)。无论如何,我会在这里发布我的代码,我认为它更容易理解我的问题。提前感谢您的帮助。
JFrame scrabbleBoard = new JFrame();
scrabbleBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container topPane = scrabbleBoard.getContentPane();
topPane.setLayout(new BoxLayout(topPane, BoxLayout.X_AXIS));
JButton done = new JButton ("Done");
JLabel player1 = new JLabel ("Player 1");
topPane.add(player1);
topPane.add(done);
scrabbleBoard.pack();
scrabbleBoard.setVisible(true);
答案 0 :(得分:4)
使用:
done.setAlignmentY(Component.TOP_ALIGNMENT);
player1.setAlignmentY(Component.TOP_ALIGNMENT);