Menubar and Statusbar in GridLayout
Page 1 of 1
Menubar and Statusbar in GridLayout
#3
Posted 05 June 2009 - 02:24 PM
Where is the VerticalMenuBar method at?
Here is my current code with just the buttons:
I would like to add a menu bar above the buttons and a status bar below it. I'm not talking about the components on these bars..or at least i don't think I am
haha I'm new to GUI.
Thanks for the help
Here is my current code with just the buttons:
public class PlayerField extends JFrame
{
private boolean clicked;
private int inputCoords[] = new int[2]; //[0] = x coord, [1] = y coord
private Button[][] buttons = new Button[8][8];
public PlayerField()
{
setTitle("Player Field");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setLayout(new GridLayout(8, 8, 2, 2));
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
buttons[x][y] = new Button();
buttons[x][y].setActionCommand((x+1) + " " + (y+1));
buttons[x][y].setBackground(Color.CYAN);
panel.add(buttons[x][y]);
}
}
setContentPane(panel);
}
I would like to add a menu bar above the buttons and a status bar below it. I'm not talking about the components on these bars..or at least i don't think I am
Thanks for the help
This post has been edited by saychanh: 05 June 2009 - 02:24 PM
#4
Posted 05 June 2009 - 02:58 PM
I would take that code out of the constructor and put it in its own method. I would then create a method for the Menu Bar and a method for the Status Bar. I would place each onto its on JPanel using whatever layout you find appropiate. I would then add each panel to the content pane using whatever layout you need to get them how you want, maybe BorderLayout. You dont need to worry about a layout being null. If you want the layout of your MenuBar to be a GridLayout, do somthing like
Do this for the other Panels too. Then you can create a mainPanel, like your content pane, and add your three panels to it
Initialise all your Panels in the constructor, so that they are created
Then you should be good to go. Need any advise just ask
private void menuBarPanel() {
JPanel menuPanel = new JPanel();
menuPanel .setLayout(new FlowLayout());
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("My Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription("My Items");
menuBar.add(menu);
//add your items to the menu
menuPanel.add(menuBar, 0);
}
}
Do this for the other Panels too. Then you can create a mainPanel, like your content pane, and add your three panels to it
private void mainPanel(){
JComponent mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(5, 5));
mainPanel.add(menuPanel, BorderLayout.NORTH);
//then other panels
add(mainPanel);
}
Initialise all your Panels in the constructor, so that they are created
public PlayerField()
{
setTitle("Player Field");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
menuBarPanel();
//other panels
mainPanel();
}
Then you should be good to go. Need any advise just ask
Page 1 of 1

Start a new topic
Add Reply




MultiQuote

| 


