Java AWT GridLayout layout monthly Calendar

Description

Java AWT GridLayout layout monthly Calendar

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame {

  public Main() {
    super("Demo2s.com");
    setSize(260, 260);/*ww  w.  jav  a  2  s  .co m*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flow = new FlowLayout();
    setLayout(flow);

    JLabel[] days = new JLabel[31];
    JLabel[] dayTitles = new JLabel[7];
    JLabel monthTitle = new JLabel("March 2015");

    JPanel titlePane = new JPanel();
    titlePane.setLayout(flow);
    titlePane.add(monthTitle);
    add(titlePane);
    JPanel monthPane = new JPanel();
    GridLayout calLayout = new GridLayout(6, 7);
    monthPane.setLayout(calLayout);
    String[] dayNames = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    for (int i = 0; i < 7; i++) {
      dayTitles[i] = new JLabel(dayNames[i]);
      monthPane.add(dayTitles[i]);
    }
    for (int i = 0; i < days.length; i++) {
      days[i] = new JLabel("" + (i + 1));
      monthPane.add(days[i]);
    }
    add(monthPane);
    
    setVisible(true);
  }

  public static void main(String[] arguments) {

    Main main = new Main();
  }
}



PreviousNext

Related