Bonjour,

Je suis en train de développer une application qui permet d'afficher un calendrier. Pour l'instant, j'ai réussi à faire l'interface graphique et à afficher mon calendrier mais mon problème est que je ne sais pas comment faire pour ajouter un événement sur mon calendrier. Voici mon code Java :

La classe GUI :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class GUI extends JPanel {
 
	/**
         * 
         */
	private static final long serialVersionUID = 6411499808530678723L;
 
	  /** The buttons to be displayed */
	  protected JButton labs[][];
 
	  private Date date=new Date();
	  String[] months = { "January", "February", "March", "April", "May", "June",
		      "July", "August", "September", "October", "November", "December" };
	  public int dom[] = { 31, 28, 31, 30, /* jan feb mar apr */
			  31, 30, 31, 31, /* may jun jul aug */
			  30, 31, 30, 31 /* sep oct nov dec */
			  };
	  /** The month choice */
	  private JComboBox<String> monthChoice;
 
	  /** The year choice */
	  private JComboBox<String> yearChoice;
 
	  GUI(){
		    super();
		    buildGUI();
		    recompute();
		  }
 
	protected void recompute() {
		// TODO Auto-generated method stub
		// Blank out all
		for (int i = 0; i < 6; i++)
		      for (int j = 0; j < 7; j++) 
		         labs[i][j].setText("");
 
		 int daysInMonth = dom[date.getMonth()];
		 if (isLeap(date.getYear()) && date.getMonth() == 1)
		      daysInMonth++;
		// Blank out the labels before 1st day of month
		    for (int i = 0; i < date.firstDayOfTheMonth(); i++) {
		      labs[0][i].setText("");
		    }
 
		 // Fill in numbers for the day of month.
		    for (int i = 1; i <= daysInMonth; i++) {
		      JButton b = labs[(date.firstDayOfTheMonth() + i - 2) / 7][(date.firstDayOfTheMonth() + i - 2) % 7];
		      b.setText(Integer.toString(i));
		    }
 
 
		    repaint();
	}
 
 
	private boolean isLeap(int year) {
		// TODO Auto-generated method stub
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		      return true;
		return false;
	}
 
	private void buildGUI() {
		getAccessibleContext().setAccessibleDescription(
		        "Calendar not accessible yet. Sorry!");
		    setBorder(BorderFactory.createEtchedBorder());
 
		    setLayout(new BorderLayout());
 
		    JPanel tp = new JPanel();
		    tp.add(monthChoice = new JComboBox<String>());
		    for (int i = 0; i < months.length; i++)
		      monthChoice.addItem(months[i]);
		    monthChoice.setSelectedItem(months[date.getMonth()]);
		    monthChoice.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent ae) {
		        int i = monthChoice.getSelectedIndex();
		        if (i >= 0) {
		          date.setMonth(i);
		          recompute();
		        }
		      }
		    });
		    monthChoice.getAccessibleContext().setAccessibleName("Months");
		    monthChoice.getAccessibleContext().setAccessibleDescription(
		        "Choose a month of the year");
		    tp.add(yearChoice = new JComboBox<String>());
		    yearChoice.setEditable(true);
		    for (int i = date.getYear() - 5; i < date.getYear() + 5; i++)
		      yearChoice.addItem(Integer.toString(i));
		    yearChoice.setSelectedItem(Integer.toString(date.getYear()));
		    yearChoice.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent ae) {
		        int i = yearChoice.getSelectedIndex();
		        if (i >= 0) {
		          date.setYear(Integer.parseInt(yearChoice.getSelectedItem().toString()));
		          recompute();
		        }
		      }
		    });
		    add(BorderLayout.CENTER, tp);
 
		    JPanel bp = new JPanel();
		    bp.setLayout(new GridLayout(7, 7));
		    labs = new JButton[6][7]; // first row is days
 
		    bp.add(new JButton("Sunday"));
		    bp.add(new JButton("Monday"));
		    bp.add(new JButton("Tuesday"));
		    bp.add(new JButton("Wednsday"));
		    bp.add(new JButton("Thersday"));
		    bp.add(new JButton("Friday"));
		    bp.add(new JButton("Saturday"));
 
		    ActionListener dateSetter = new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		        String num = e.getActionCommand();
		        if (!num.equals("")) {
		          // set the current day highlighted
		          setDayActive(Integer.parseInt(num));
 
		          // When this becomes a Bean, you can
		          // fire some kind of DateChanged event here.
		          // Also, build a similar daySetter for day-of-week btns.
		        }
		      }
 
		    };
 
 
 
		    // Construct all the buttons, and add them.
		    for (int i = 0; i < 6; i++)
		      for (int j = 0; j < 7; j++) {
		        bp.add(labs[i][j] = new JButton(""));
		        labs[i][j].addActionListener(dateSetter);
		      }
 
		    add(BorderLayout.SOUTH, bp);
 
	}
 
	protected void setDayActive(int parseInt) {
		// TODO Auto-generated method stub
 
	}
 
	public static void main(String[] argv)
	  {
	    JFrame f = new JFrame("Mon Calendrier");
	    Container c = f.getContentPane();
	    c.setLayout(new FlowLayout());
	    c.add(new GUI());
 
	    f.pack();
	    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    f.setVisible(true);
	  }
 
}
la classe Date:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Calendar;
import java.util.GregorianCalendar;
 
public class Date {
	int year,month,day;
 
	private Calendar calendar=new GregorianCalendar();
 
	Date(){
	    year=calendar.get(Calendar.YEAR);
	    month=calendar.get(Calendar.MONTH);
	    day=calendar.get(Calendar.DAY_OF_MONTH);
	}
 
	Date(int year,int month,int day){
		this.year=year;
		this.month=month;
		this.day=day;
	}
 
	public int firstDayOfTheMonth(){
 
		return new GregorianCalendar(year, month, 1).get(Calendar.DAY_OF_WEEK);
 
	}
 
	int getYear(){
		return year;
	}
	int getMonth(){
		return month;
	}
	int getDay(){
		return day;
	}
	void setYear(int year){
		this.year=year;
	}
	void setMonth(int month){
		this.month=month;
	}void setDay(int day){
		this.day=day;
	}
}