Код: Выделить всё
import java.awt.*;
import javax.swing.*;
public class GridLayoutDemo {
private static void GridLayoutDemo(Container btnPanel){
int rows = 3;
int cols = 2;
int hgap = 5;
int vgap = 5;
btnPanel.setLayout(new GridLayout(rows, cols, hgap, vgap));
JButton btnOne = new JButton("One");
btnOne.setPreferredSize(new Dimension(80, 24));
btnPanel.add(btnOne);
JButton btnTwo = new JButton("Two");
btnTwo.setPreferredSize(new Dimension(80, 24));
btnPanel.add(btnTwo);
JButton btnThree = new JButton("Three");
btnThree.setPreferredSize(new Dimension(80, 24));
btnPanel.add(btnThree);
JButton btnFour = new JButton("Four");
btnFour.setPreferredSize(new Dimension(80, 24));
btnPanel.add(btnFour);
JButton btnFive = new JButton("Five");
btnFive.setPreferredSize(new Dimension(80, 24));
btnPanel.add(btnFive);
JButton btnSix = new JButton("Six");
btnSix.setPreferredSize(new Dimension(80, 24));
btnPanel.add(btnSix);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frmRoot = new JFrame("GLD");
frmRoot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayoutDemo(frmRoot.getContentPane());
frmRoot.pack();
frmRoot.setLocationRelativeTo(null);
frmRoot.setResizable(false);
frmRoot.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... gridlayout
Мобильная версия