Search code examples
javaswinglayoutjpanelflowlayout

can't get a simple layout in java


I am trying to make an GUI in java ,this is my first venture with GUI in java and I am trying to learnTHIS IS THE LAYOUT I WANT TO ARCHIVE

Above is what I trying to create.But I simple can't get to design it in that way ,here is my code:

//Frame:   
     JFrame frame;
     //Menu :
     JMenuBar menuBar;
     JMenu menu1,menu2;
     JMenuItem menuItem;
     //Panels:
     JPanel topPanel;
     JPanel centerPanel;
     JPanel bpttomPanel;  
     String[] vTypeStrings = { "Select vehicle","Car", "Boat", "Truck", };
     //Labels:
     JLabel typeLabel;
     //ComboBoxes:
     JComboBox vList;;

     //Frame creation   
     frame= new JFrame("frame1");
     frame.setSize(450,250);
     frame.setLocation(200,300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setLayout(new GridLayout(3,1));

     //Create the menu bar.
     menuBar = new JMenuBar();

     //Create menu bar items
     menu1 = new JMenu("File");
     menu1.setMnemonic('F');
     menuBar.add(menu1);

     menu2 = new JMenu("Help");
     menu2.setMnemonic('H');
     menuBar.add(menu2);

     //Adding items to  each menu 
     menuItem = new JMenuItem("Load", 'L');
     menu1.add(menuItem);
     menuItem = new JMenuItem("Exit", 'X');
     menu1.add(menuItem);
     //Second menu
     menuItem = new JMenuItem("About",'A');
     menu2.add(menuItem);

     //Adding menu to frame
     frame.setJMenuBar(menuBar);



     //Top Panel
      topPanel = new JPanel(new FlowLayout());
      frame.add(topPanel,BorderLayout.NORTH);
      JLabel headLabel=new JLabel("Snedden's Ordering system");//Heading label
      topPanel.add(headLabel);
      headLabel.setFont(new Font("Serif", Font.PLAIN, 24));
      headLabel.setForeground(new Color(0xff0000));


     //Center Panel
     centerPanel = new JPanel();
     centerPanel.setLayout(new GridLayout(2,2,2,2));
     vList = new JComboBox(vTypeStrings);
     vList.setSelectedIndex(0);
     typeLabel=new JLabel("Vehicle Type");
     typeLabel.setLabelFor(vList);
     centerPanel.add(typeLabel);
     centerPanel.add(vList);
     frame.add(centerPanel,BorderLayout.CENTER);



     frame.setVisible(true);

Here is what I getMY layout,still midway

THing is I get the label and the field on the same line, don't understand why,please help thanks.


Solution

  • frame.setLayout(new GridLayout(3,1));
    

    This is your first mistake. The cells of a GridLayout are all the same size, while you want the central part to be higher.

    frame.add(topPanel,BorderLayout.NORTH);
    ....
    frame.add(centerPanel,BorderLayout.CENTER);
    

    This is the second one, you set frame to have a GridLayout, so you shouldn't use BorderLayout constraints.

    The correct thing to do, in my opinion, is to remove the first line, and leave the frame with the default border layout.

    As for your issue with the grid, it's due to the fact that you're not filling the grid. If you insert 4 controls in the grid, or initialize the grid with (1,2), you will get the expected outcome.

    This is with centerPanel.setLayout(new GridLayout(1,2));: Layout with 1x2 grid

    And this is with `

    centerPanel.add(typeLabel);
    centerPanel.add(vList);
    centerPanel.add(new JLabel());
    centerPanel.add(new JLabel());
    

    Layout with 2x2 grid and dummy labels