I am trying to make a gui, with a frame, 2 panels which are half of the height of the frame, and in the bottom panel 7 buttons next to each other using FlowLayout.
this works, but it does not fill up the available space, so if I say the preferred size of the buttons is screenWidth / 7 to make room for all 7, it places 6 buttons on one row, puts blank space on both sides, and places the last button which could fit without the blank space a row below.
Posted full code so you can copy paste and run for yourself,
but the main question is how to fill up the remaining space with that 7th button using FlowLayout.
In any case, Thank you for your time!
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.awt.Container; public class newGui { public newGui() { initUI(); } public void addComponents(Container container) { JPanel display = new JPanel(); JPanel interact = new JPanel(); //sets Layout for containers container.setLayout(new BorderLayout()); display.setLayout(new FlowLayout()); interact.setLayout(new FlowLayout()); display.setBackground(Color.BLACK); //creates icons for buttons //to run change with own jpg's ImageIcon[] icons = new ImageIcon[7]; icons[0] = new ImageIcon(("hearts1.jpg")); icons[1] = new ImageIcon(("hearts2.jpg")); icons[2] = new ImageIcon(("hearts3.jpg")); icons[3] = new ImageIcon(("hearts4.jpg")); icons[4] = new ImageIcon(("hearts5.jpg")); icons[5] = new ImageIcon(("hearts6.jpg")); icons[6] = new ImageIcon(("hearts7.jpg")); //makes buttons JButton button1 = new JButton("derp"); JButton button2 = new JButton("derp"); JButton button3 = new JButton("derp"); JButton button4 = new JButton("derp"); JButton button5 = new JButton("derp"); JButton button6 = new JButton("derp"); JButton button7 = new JButton("derp"); //sets icons to buttons button1.setIcon(icons[0]); button2.setIcon(icons[1]); button3.setIcon(icons[2]); button4.setIcon(icons[3]); button5.setIcon(icons[4]); button6.setIcon(icons[5]); button7.setIcon(icons[6]); //gets screen size and sets up dimension to use for button Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; display.setPreferredSize(new Dimension(screenWidth, screenHeight/2)); Dimension buttonDimension = new Dimension(screenWidth / 7, screenHeight / 2 -40 ); button1.setPreferredSize(buttonDimension); button2.setPreferredSize(buttonDimension); button3.setPreferredSize(buttonDimension); button4.setPreferredSize(buttonDimension); button5.setPreferredSize(buttonDimension); button6.setPreferredSize(buttonDimension); button7.setPreferredSize(buttonDimension); //adds buttons to bottom panel FlowLayout flow = new FlowLayout(FlowLayout.LEFT); interact.add(button1,flow); interact.add(button2,flow); interact.add(button3,flow); interact.add(button4,flow); interact.add(button5,flow); interact.add(button6,flow); interact.add(button7,flow); //adds panels to container container.add(display,BorderLayout.NORTH); container.add(interact,BorderLayout.SOUTH); } //sets up frame and adds the components private void initUI() { JFrame frame = new JFrame("My App"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(screenSize); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponents(frame.getContentPane()); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run() { newGui gui = new newGui(); } }); } }