Add buttons in Border Layout
I would like to add buttons in Border Layout one after another . But I do not know how . Thats a part of my code :
setLayout(new BorderLayout()); JPanel jp = new JPanel(); jp.setBackground(Color.BLUE); JPanel jp2 = new JPanel(); jp2.setBackground(Color.LIGHT_GRAY); JPanel jp3 = new JPanel(); jp3.setBackground(Color.WHITE); add(jp, BorderLayout.SOUTH); add(jp2, BorderLayout.EAST); add(jp3, BorderLayout.WEST); jp.add(btn, BorderLayout.WEST); jp.add(btn2, BorderLayout.CENTER); jp.add(btn3, BorderLayout.EAST); jp2.add(btn4, BorderLayout.NORTH); jp2.add(btn5, BorderLayout.NORTH); jp2.add(btn6, BorderLayout.CENTER); jp2.add(btn7, BorderLayout.SOUTH); jp2.add(btn8, BorderLayout.SOUTH);
But it add buttons side by side . I am beginner programmer and I really do not idea how to do it .
BorderLayout is not suited for what you want - it supports only one component for each constraint (NORTH, SOUTH...)
Use another layout, there are a lot of options, like Gridlayout, GridBagLayout.
I would use TableLayout, it's a very flexible layout - see the homepage address below. www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/
JPanel initially has FlowLayout, which places components side by side. Instead of new JPanel(), use new JPanel(new BorderLayout())