我有两个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);

最佳答案

采用:

done.setAlignmentY(Component.TOP_ALIGNMENT);
player1.setAlignmentY(Component.TOP_ALIGNMENT);


参见:Fixing Alignment Problems

10-05 17:47