简体   繁体   中英

Setting a fixed size for JButtons in a grid-style layout

I want to make 16 buttons display as a 4x4 grid. Each button should be the same size, and have an equal gap.

I have been able to set the gap size, but I can't reduce the size of the button. I've basically used just this for group layout...

layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(button1)
                    .addComponent(button5)
                    .addComponent(button9)
                    .addComponent(button13))
               .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(button2)
                    .addComponent(button6)
                    .addComponent(button10)
                    .addComponent(button14))
               .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(button3)
                    .addComponent(button7)
                    .addComponent(button11)
                    .addComponent(button15))
               .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(button4)
                    .addComponent(button8)
                    .addComponent(button12)
                    .addComponent(button16))
            );

            layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(BASELINE)
                    .addComponent(button1)
                    .addComponent(button2)
                    .addComponent(button3)
                    .addComponent(button4))
               .addGroup(layout.createParallelGroup(BASELINE)
                    .addComponent(button5)
                    .addComponent(button6)
                    .addComponent(button7)
                    .addComponent(button8))
               .addGroup(layout.createParallelGroup(BASELINE)
                    .addComponent(button9)
                    .addComponent(button10)
                    .addComponent(button11)
                    .addComponent(button12))
               .addGroup(layout.createParallelGroup(BASELINE)
                    .addComponent(button13)
                    .addComponent(button14)
                    .addComponent(button15)
                    .addComponent(button16))

Could someone please help me with a better approach.

You could also try using a GridLayout(). This arranges all your components in a grid, the rows and columns are defined by the parameters. You create it using the line

GridLayout g = new GridLayout(rows, columns)

You need to import the AWT so your code would look like:

GridLayout g = new GridLayout(4,4);
//Add it to your JPanel
myJpanel.setLayout(g);
//then
myJpanel.add(button1);
//the rest of your code

The size of each component in a GroupLayout is constrained by three values; minimum size, preferred size and maximum size

Try:

button.setPreferredSize(new Dimension(50, 10));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM