0

I'm trying to position 5 buttons on a JFrame. Four buttons should be in first row and the fifth one should be in the second row. But here all of the buttons appear in one row and half of the fifth button is out of the panel. By the way if I use frame.pack(); my frame become smaller than what I expected. I don't want to change the size of frame. I want to locate the fifth one in the second row.

import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.*;


public class test {

    public static void main(String[] args) {


        JFrame frame = new JFrame();
        frame.setBounds(100, 100, 529, 300);
        frame.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));


        JPanel panel = new JPanel();
        frame.getContentPane().add(panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));


        JButton btnNewButton_4 = new JButton("New button");
        panel.add(btnNewButton_4);

        JButton btnNewButton_3 = new JButton("New button");
        panel.add(btnNewButton_3);

        JButton btnNewButton = new JButton("New button");
        panel.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New button");
        panel.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("New button");
        panel.add(btnNewButton_2);


        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
Bernard
  • 4,240
  • 18
  • 55
  • 88
  • 1
    By "locate" do you really mean "set the location of" them? When I first read your title, I thought you were trying to find them. – splungebob Apr 18 '13 at 14:00

4 Answers4

3

Four buttons should be in first row and the fifth one should be in the second row.

  • standard and booking examples in Oracle tutorial about How to use Layout Managers

  • use GridLayout (two rows) for JPanel, but all JButtons will have the same dimension

  • use GridBagLayout, then each of JButtons will have diferrent size or not too

  • invisible JComponents or empty JLabel can help you in most complicated variations

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You are using a BoxLayout, where components will not wrap (check the API documentation: http://docs.oracle.com/javase/6/docs/api/javax/swing/BoxLayout.html)

About the size, try to use panel.setMinimumSize(Dimension d)

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
1

What I do is use null as the layout manager and place the components my self. A manager works well if you want your components to be relayed when the window is resized or you do not know the final size of applet etc

But most of the time that is not true or over kill

Instead just place the components on a grid by pixel using setBounds(x,y,width,height); Example:

    JFrame frame = new JFrame();
    frame.setBounds(100, 100, 529, 300);
    frame.getContentPane().setLayout(null);//over ride default
    Container c = frame.getContentPane();


    JButton btnNewButton_4 = new JButton("New button");
    c.add(btnNewButton_4);
    c.seBounds(4,10,40,25);

    JButton btnNewButton_3 = new JButton("New button 3");
    c.add(btnNewButton_3);
    c.seBounds(40,10,40,25);//...etc

Samples

tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • Please don't suggest `null` layout; the non-resizable components are almost certain to look bad on some platforms, for [example](http://stackoverflow.com/a/12532237/230513). – trashgod Apr 18 '13 at 16:09
  • that is one example. have used successfully in other places. updated a sample – tgkprog Apr 18 '13 at 16:20
0

You can also use setBounds(arg1 , arg2 , arg3 , arg4) method. Use this for a better help. Doing Without a Layout Manager (Absolute Positioning)

Erfan
  • 169
  • 1
  • 1
  • 11