Quelque chose
public text v1 · immutableimport javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JComboBox;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;
public class Calendrier
{
private int nbDayInMonth; // nbre de jour dans le mois actuel
private int premierJour; // premierJour du mois
private int jourActuel; // jour actuel
public JPanel createView()
{
JPanel panel = new JPanel(); //Contenu de la fenetre
panel.setLayout(new GridBagLayout());//applique le layout au panel
GridBagConstraints c = new GridBagConstraints();//applique les contraintes au gridbag
GregorianCalendar calendar = new GregorianCalendar();
//creation de la Combo box
JComboBox comb1 = new JComboBox();
String[] tabmois = {"Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Décembre"};
for(int i=0; i<=11; i++)
{
comb1.addItem(tabmois[i]);
}
//Selection du moi courant dans le combobox
comb1.setSelectedIndex(new Integer(calendar.get(java.util.Calendar.MONTH)));
//creation du Spinner
JSpinner spin1 = new JSpinner();
//Selection de l'année courante dans le spinner
spin1.setValue(new Integer(calendar.get(java.util.Calendar.YEAR)));
c.ipadx=5;
c.ipady=5;
c.gridx=0;
c.gridy=0;
c.fill = GridBagConstraints.BOTH;
c.gridwidth=1;
c.gridheight=1;
c.insets= new Insets(5,5,5,5);
panel.add(comb1,c);
c.gridx=1;
c.gridy=0;
panel.add(spin1,c);
//creation du Grid layout -- 2eme Panel
JPanel pan1 = new JPanel();
pan1.setLayout(new GridLayout(7,7));
String[] tabjours = {"Dim","Lun","Mar","Mer","Jeu","Ven","Sam"};
for(int i=0; i<=6; i++)
{
pan1.add(new JLabel (tabjours[i]));
}
nbDayInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
jourActuel = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1);
premierJour = calendar.get(Calendar.DAY_OF_WEEK);
if(premierJour == 1)
{
for(int i=1; i<=nbDayInMonth; i++)
{
if (i==jourActuel)
{
pan1.add(new JButton(""+i)).setBackground(Color.red);
}
else
{
pan1.add(new JButton(""+i));
}
}
int reste = 42 - nbDayInMonth;
for(int j=1; j<=reste; j++)
{
pan1.add(new JButton(""));
}
}
else if(premierJour == 2)
{
pan1.add(new JButton(""));
for(int i=1; i<=nbDayInMonth; i++)
{
if (i==jourActuel)
{
pan1.add(new JButton(""+i)).setBackground(Color.red);
}
else
{
pan1.add(new JButton(""+i));
}
}
int reste = 42 - nbDayInMonth - 1;
for(int j=1; j<=reste; j++)
{
pan1.add(new JButton(""));
}
}
else if(premierJour == 3)
{
pan1.add(new JButton(""));
pan1.add(new JButton(""));
for(int i=1; i<=nbDayInMonth; i++)
{
pan1.add(new JButton(""+i));
}
int reste = 42 - nbDayInMonth - 2;
for(int j=1; j<=reste; j++)
{
pan1.add(new JButton(""));
}
}
else if(premierJour == 4)
{
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
for(int i=1; i<=nbDayInMonth; i++)
{
if (i==jourActuel)
{
pan1.add(new JButton(""+i)).setBackground(Color.red);
}
else
{
pan1.add(new JButton(""+i));
}
}
int reste = 42 - nbDayInMonth - 3;
for(int j=1; j<=reste; j++)
{
pan1.add(new JButton(""));
}
}
else if(premierJour == 5)
{
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
for(int i=1; i<=nbDayInMonth; i++)
{
if (i==jourActuel)
{
pan1.add(new JButton(""+i)).setBackground(Color.red);
}
else
{
pan1.add(new JButton(""+i));
}
}
int reste = 42 - nbDayInMonth - 4;
for(int j=1; j<=reste; j++)
{
pan1.add(new JButton(""));
}
}
else if(premierJour == 6)
{
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
for(int i=1; i<=nbDayInMonth; i++)
{
if (i==jourActuel)
{
pan1.add(new JButton(""+i)).setBackground(Color.red);
}
else
{
pan1.add(new JButton(""+i));
}
}
int reste = 42 - nbDayInMonth - 5;
for(int j=1; j<=reste; j++)
{
pan1.add(new JButton(""));
}
}
else if(premierJour == 7)
{
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
pan1.add(new JButton(""));
for(int i=1; i<=nbDayInMonth; i++)
{
if (i==jourActuel)
{
pan1.add(new JButton(""+i)).setBackground(Color.red);
}
else
{
pan1.add(new JButton(""+i));
}
}
int reste = 42 - nbDayInMonth - 6;
for(int j=1; j<=reste; j++)
{
pan1.add(new JButton(""));
}
}
c.fill = GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=1;
c.gridwidth=2;
panel.add(pan1,c);
//Affichage de la Date du jour -- 3eme Panel
c.fill = GridBagConstraints.CENTER;
c.gridx=0;
c.gridy=2;
c.gridwidth=2;
Date dateDuJour = new Date();
SimpleDateFormat date1 = new SimpleDateFormat("EEEE d MMMM yyyy");
panel.add(new JLabel(""+date1.format(dateDuJour)),c);
return panel;
}
}