Search code examples
javaswingjbuttongrid-layout

In Grid Layout the Jbutton size is not changing with the use of setBounds() in java


While making a frame using GridLayout the button size is not changing using setBound(). The code for the frame is :

f1=new JFrame("Select any one");
            ch=new JCheckBox("SUBSTITUTION CYPHER");
            ch1=new JCheckBox("two");
            ch2=new JCheckBox("three");
            ch3=new JCheckBox("four");
            ch4=new JCheckBox("five");

            f1.add(ch);
            f1.add(ch1);
            f1.add(ch2);
            f1.add(ch3);
            f1.add(ch4);
            f1.setLayout(new GridLayout(5, 1));
            b10.addActionListener(this);
            b10.setBounds(280,380,10,10);
            f1.add(b10);
            f1.setSize(300,400);
            f1.setVisible(true);

Solution

  • GridLayout isn't the best choice for buttons as GridLayout will take up all space in its container. So no setBounds() won't be the best choice for what you're trying to achieve.

    I would suggest having a look at this reference for LayoutManagers: Reference for LayoutManagers

    I would suggest looking at some alternative LayoutManagers. You could try GridBagLayout.