Use a horizontal glue between button2 and button3:
buttonPanel.add(Box.createHorizontalGlue());
See How to Use BoxLayout for more details.
Pergunta
Current BoxLayout setup [Button 1, Button 2, Button 3 ||||||||||||||||||||||||||||||||||||||] | denoting empty space.
How do I make it so that it appears [Button 1, Button 2, ||||||||||||||||||||||| Button3] These buttons are JButtons and the boxlayout is inside a BorderLayout at North position.
private JPanel makeButtonPanel(ObservableMazeGame observableMazeGame) {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
ButtonPanel buttonPanelMaze = new ButtonPanel(observableMazeGame);
buttonPanel.add(buttonPanelMaze.newGameButton());
buttonPanel.add(buttonPanelMaze.helpButton());
buttonPanel.add(buttonPanelMaze.aboutButton());
return buttonPanel;
}
This is added from
ObservableMazeGame observableMazeGame = new ObservableMazeGame();
JFrame baseFrame = new JFrame();
baseFrame.setTitle("Maze Game");
baseFrame.setLayout(new BorderLayout());
baseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
baseFrame.add(makeButtonPanel(observableMazeGame), BorderLayout.NORTH);
baseFrame.pack();
baseFrame.setVisible(true);
I just want the 3rd button aligned to the right and even when stretched, it follows the stretch. I've tried setAlignment but it does not work.
Solução
Use a horizontal glue between button2 and button3:
buttonPanel.add(Box.createHorizontalGlue());
See How to Use BoxLayout for more details.