Reputation: 1197
I am creating a simple game using Java. I have created the game's menu using JFrame. I am having confusion about what layouts to be used to place the Menu Buttons(Start,High Scores,Instructions,Exit) at the center. I have an approach in mind that is : Creating a grid layout of three columns and in the middle column adding a box layout(having the menu buttons) positioned at the center of this column.
Should I use this approach? if not then please tell me the solution.
Upvotes: 0
Views: 367
Reputation: 347194
Use a GridBagLayout
JButton startButton = new JButton("Start");
JButton scoreButton = new JButton("High Score");
JButton instructButton = new JButton("Instructions");
JButton exitButton = new JButton("Exit");
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = java.awt.gbc.HORIZONTAL;
gbc.insets = new java.awt.Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
getContentPane().add(startButton, gbc);
gbc.gridy = 1;
getContentPane().add(scoreButton, gbc);
gbc.gridy = 2;
getContentPane().add(instructButton, gbc);
gbc.gridy = 3;
getContentPane().add(exitButton, gbc);
Upvotes: 1
Reputation: 2137
Madprogrammer is right, use a gridbaglayout. I myself am actually too lazy to fool around with layout managers so I use WindowBuilder for Eclipse SDK. Search for it on google. WindowBuilder has bi directional code generation (ie, it can parse swing code and then generate it from what you do in the gui), and its all drag and drop.
Upvotes: 0