Java AWT GridLayout layout nine buttons in 3 by 3 grid

Description

Java AWT GridLayout layout nine buttons in 3 by 3 grid

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {

  public Main() {
    super("java2s.com");
    setSize(260, 260);/*  w  w  w . j av a2s.c om*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel pane = new JPanel();
    GridLayout family = new GridLayout(3, 3, 10, 10);
    pane.setLayout(family);

    pane.add(new JButton("1"));
    pane.add(new JButton("2"));
    pane.add(new JButton("3"));
    pane.add(new JButton("4"));
    pane.add(new JButton("5"));
    pane.add(new JButton("6"));
    pane.add(new JButton("7"));
    pane.add(new JButton("8"));
    pane.add(new JButton("9"));
    add(pane);
    pack();
    setVisible(true);
  }

  public static void main(String[] arguments) {
    Main frame = new Main();
  }
}



PreviousNext

Related