Código de Java - CalendarioVer3.java

Requerimientos

Para poder ejecutar este código es necesario tener instalado NetBeans IDE 8.2 para aplicaciones en JAVA en un sistema operativo Windows 10, 7, Vista, XP.

Versión 3.0

Actualizado el 13 de Enero del 2017 (Publicado el 23 de Diciembre del 2016)gráfica de visualizaciones de la versión: Versión 3.0
2.885 visualizaciones desde el 23 de Diciembre del 2016
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
 
public class CalendarioVer3 extends JFrame implements ActionListener, MouseWheelListener
{
	Fecha f;
	JPanel p,p1,p2;
	JTextField tfFecha;
	//JTextArea new taMes;
	TitledBorder tBorder1;
	JButton b1,b2,tbB[], tbD[] ;
	//JButton aceptar;
	JTextField tfd,tfm,tfa,tfh;
	JLabel ldia,lmes, laño,l;
 
	public CalendarioVer3()
	{
		Font font = new Font("Dialog",Font.BOLD|Font.ITALIC,20);
		String[] mm= {"mes","Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"};
		String[] dd= {"L","M","X","J","V","S","D"};
 
 
		tBorder1 = new TitledBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.WHITE,Color.white,new Color(93, 93, 193),new Color(234)),"MES");
    	//tBorder1 = new TitledBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(93, 93, 193),new Color(134, 134, 134)),"MES");
 
		p= new JPanel();
		p1 = new JPanel(new GridLayout(2,4));
		p2 = new JPanel(new GridLayout(7,7,5,5));
		p2.setBackground(Color.LIGHT_GRAY);//BLACK);//YELLOW);
		p.setBackground(Color.BLUE);
 
		tfFecha = new JTextField("Mueve rueda raton sobre mes-dia-año",20);
 
		p2.setBorder(tBorder1);
		p2.setMinimumSize(new Dimension(200,300));
		p2.setBackground(Color.YELLOW);
 
		//b1 = new JButton("LEER FECHA");
		//b1.addActionListener(this);
		b2 = new JButton("Cerrar");
 
		b2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{ System.exit(-1);
			}});
 
		//aceptar= new JButton("ACEPTAR FECHA");
		//aceptar.addActionListener(this);
 
		tbD = new JButton[dd.length];
		for(int i=0; i<tbD.length; i++)
		{
			tbD[i]=new JButton(dd[i]);
			tbD[i].setForeground(Color.WHITE);
			tbD[i].setBackground(Color.BLACK);
			tbD[i].setVisible(false);
			tbD[i].setFont(font);
			p2.add(tbD[i]);
		}
 
		tbB = new JButton[42];
		for(int i=0; i<tbB.length; i++)
		{
			tbB[i]=new JButton(" ");
			tbB[i].setVisible(false);
			tbB[i].addActionListener(this);
			tbB[i].setFont(font);
			p2.add(tbB[i]);
		}
 
		ldia= new JLabel(" DIA");
		lmes= new JLabel(" MES");
		laño= new JLabel(" AÑO");
		l   = new JLabel(" FIN   ");
 
		tfd= new JTextField("1",2);
		tfm= new JTextField("1",2);
		tfa= new JTextField("2009",3);
 
		//tfd.setEditable(false);
		//tfm.setEditable(false);
		p1.setBackground(Color.BLUE);
 
		tfd.setBackground(Color.PINK);
		ldia.setForeground(Color.PINK);
		tfm.setBackground(Color.ORANGE);
		lmes.setForeground(Color.ORANGE);
		tfa.setBackground(Color.LIGHT_GRAY);
		laño.setForeground(Color.LIGHT_GRAY);
 
		tfd.setFont(font);
		tfm.setFont(font);
		tfa.setFont(font);
		/*
		tfd.setToolTipText("para cambiar dia: mover rueda raton");
		tfm.setToolTipText("mover rueda raton");
		tfa.setToolTipText("mover rueda raton");
		*/
		tfd.addMouseWheelListener(this);
		tfm.addMouseWheelListener(this);
		tfa.addMouseWheelListener(this);
		tfd.addActionListener(this);
		tfm.addActionListener(this);
		tfa.addActionListener(this);
 
		//p.add(aceptar);
 
		p.add(tfFecha);
		p1.add(ldia);
		p1.add(lmes);
		p1.add(laño);
		p1.add(l);
 
		p1.add(tfd);
		p1.add(tfm);
		p1.add(tfa);
 
		//p1.add(b1);
 
		p1.add(b2);
 
		p.add(p1);
		p.add(p2);
		getContentPane().add(p);
 
		f = new Fecha();
	}
 
	private void escribirMes()
	{
		Fecha ff;
		int i;
		ff= new Fecha(1, f.mes, f.ano);// primer dia del mes
    	f.numSemComMes= f.getNumDiaSemana(ff);	// cargar el dia de comienzo del mes
    	f.nombreMes=f.getNombreMes();
 
    	tBorder1 = new TitledBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED),f.dia+" - "+f.nombreMes+" - "+f.ano+" - "+f.getDiaSemana(f));
    	//tBorder1 = new TitledBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED/*LOWERED*/,Color.white,Color.white,new Color(93, 93, 193),new Color(134, 134, 134)),f.dia+" - "+f.nombreMes+" - "+f.ano+" - "+f.getDiaSemana(f));
    	p2.setBorder(tBorder1);
 
    //	p.setBackground(Color.BLACK);
    	for(i=0; i<tbD.length; i++)
			tbD[i].setVisible(true);
 
		//p2.setBackground(new Color((int)(Math.random()*10000)+ 1500));
		for(i=0; i < f.numSemComMes; i++)
			//tbB[i].setText("");
			tbB[i].setVisible(false);
		for(i=1; i <= f.diasMes(f.mes,f.ano); i++)
		{
			tbB[i+f.numSemComMes-1].setVisible(true);
			tbB[i+f.numSemComMes-1].setText(String.valueOf(i));
			// colores por defecto
			tbB[i+f.numSemComMes-1].setBackground(Color.WHITE);//GRAY);
			tbB[i+f.numSemComMes-1].setForeground(Color.BLACK);
 
			if((i+f.numSemComMes)%7==0)
			{
				tbB[i+f.numSemComMes-1].setBackground(Color.RED);
				tbB[i+f.numSemComMes-1].setForeground(Color.WHITE);
			}
			if((i+f.numSemComMes)%7==6)
			{
				tbB[i+f.numSemComMes-1].setBackground(Color.GREEN);//GRAY);
				tbB[i+f.numSemComMes-1].setForeground(Color.WHITE);
			}
 
			if(i==f.dia)
			{
				tbB[i+f.numSemComMes-1].setBackground(Color.BLUE);
				tbB[i+f.numSemComMes-1].setForeground(Color.WHITE);
			}
		}
		for( ; i < tbB.length-f.numSemComMes+1; i++)
		{
			tbB[i+f.numSemComMes-1].setVisible(false);
		}
		tfFecha.setText(f.toString());
	}
 
	public void actionPerformed(ActionEvent ae)
	{
		JButton b;
		if(ae.getSource() instanceof JButton)
		{
			// pulsando un dia, se pone cmo fecha actual.
			b = (JButton) ae.getSource();
			f.dia= Integer.parseInt(b.getLabel());
			tfd.setText(b.getLabel());
			escribirMes();
		/*	if(b==b1)
			{
				f.leerFecha();
				tfd.setText(String.valueOf(f.dia));
				tfm.setText(String.valueOf(f.mes));
				tfa.setText(String.valueOf(f.ano));
				tfFecha.setText(f.toString());	
				escribirMes();
			}
			if(b==b2)
				System.exit(0);
			
			if(b == aceptar)
			{
				actualizarFecha();
				if(!f.fechaNoValida())
					escribirMes();
			}				
	*/
		}
		if(ae.getSource() instanceof JTextField)
		{
			f.dia=Integer.parseInt(tfd.getText());
			f.mes=Integer.parseInt(tfm.getText());
			f.ano=Integer.parseInt(tfa.getText());
			escribirMes();
		}
	}
 
 	public void mouseWheelMoved(MouseWheelEvent e)
	{
		if(e.getSource() == tfa)
			if (e.getWheelRotation()<=0)
				tfa.setText(String.valueOf((Integer.parseInt(tfa.getText())+e.getScrollAmount()/3 )));
			else
				tfa.setText(String.valueOf((Integer.parseInt(tfa.getText())-e.getScrollAmount()/3 )));
		if(e.getSource() == tfm)
			if (e.getWheelRotation()<=0)
				tfm.setText(String.valueOf(circular((Integer.parseInt(tfm.getText())+e.getScrollAmount()/3 ),12)));
			else
				tfm.setText(String.valueOf(circular((Integer.parseInt(tfm.getText())-e.getScrollAmount()/3 ),12)));
		if(e.getSource() == tfd)
			if (e.getWheelRotation()<=0)
				tfd.setText(String.valueOf(circular((Integer.parseInt(tfd.getText())+e.getScrollAmount()/3 ),f.diasMes(f.mes,f.ano))));
			else
				tfd.setText(String.valueOf(circular((Integer.parseInt(tfd.getText())-e.getScrollAmount()/3 ),f.diasMes(f.mes,f.ano))));
		actualizarFecha();
		escribirMes();
	}
 
 	public void actualizarFecha()
 	{
 		f.dia= Integer.parseInt(tfd.getText());
		f.mes= Integer.parseInt(tfm.getText());
		f.ano= Integer.parseInt(tfa.getText());
		f.nombreMes = f.getNombreMes();
 
 	}
 	private int circular(int x,int n)
 	{
 		if(x>n)
 			return 1;
 		else
 			if(x<1)
 				return n;
 			else
 				return x;
 	}
 
	public static void main(String [] ar)
	{
		CalendarioVer3 cal = new CalendarioVer3();
		cal.setSize(445,440);
		cal.setTitle("CalendarioVer3 Perpetuo Enrique's");
		cal.setVisible(true);
		cal.setResizable(false);
		cal.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				System.exit(-1);
			}
		});
	}
}
 
class Fecha
{
	int dia,mes , ano;
	private String diaSemana;
	int numSemComMes;
	int numDiaSem;
	String nombreMes;
 
	public Fecha()
	{
		dia=1;
		mes=1;
		ano =0;
	}
 
	public Fecha(int d, int m, int a)
	{
		dia = d;
		mes = m;
		ano = a;
		//diaSemana = getDiaSemana(this);
		//numDiaSem = getNumDiaSemana(this);
		nombreMes = this.getNombreMes();
		numSemComMes = -1;
	}
/*	public void leerFecha()
	{
		Integer oEnt;
		do
		{
			try
			{	
				dia = Integer.parseInt(JOptionPane.showInputDialog(null,"Introduce dia","LEER FECHA",JOptionPane.INFORMATION_MESSAGE));
				mes = Integer.parseInt(JOptionPane.showInputDialog(null,"Introduce mes","LEER FECHA",JOptionPane.INFORMATION_MESSAGE));
				ano = Integer.parseInt(JOptionPane.showInputDialog(null,"Introduce año","LEER FECHA",JOptionPane.INFORMATION_MESSAGE));
			}
			catch(NumberFormatException nfe){dia=1; mes=1;ano=1;}
			nombreMes = getNombreMes();
			diaSemana = getDiaSemana(this);
			numDiaSem= getNumDiaSemana(this);
			//System.out.println(" en LeerFecha" + numDiaSem);
		} while(fechaNoValida());
	}
*/
	public boolean fechaNoValida()
	{
		boolean novale = true;
		if(mes >=1 && mes <=12)
			if(dia>=1 && dia <= diasMes(mes,ano))
				novale=false;
		return novale;
	}
 
	public int diasMes(int mes, int ano)
	{
		switch(mes)
		{
		case 1:	  case 3:	case 5:	  case 7: 	case 8:	case 10: 	case 12: 	return 31;
		case 4:	  case 6:	case 9:	  case 11: 									return 30;
		case 2: if (bisiesto(ano)) return 29;
				else return 28;
		}
		return -1;
	}
 
	private boolean bisiesto(int ano)
	{
		if((ano%4 == 0 && ano % 100 !=0) ||(ano%400==0))
			return true;
		else return false;
	}
 
	public String getNombreMes()
	{
		switch(mes)
		{
			case 1: nombreMes = "enero";		break;
			case 2: nombreMes = "febrero";		break;
			case 3: nombreMes = "marzo";		break;
			case 4: nombreMes = "abril";		break;
			case 5: nombreMes = "mayo";			break;
			case 6:nombreMes = "junio";			break;
			case 7: nombreMes = "julio";		break;
			case 8: nombreMes = "agosto";		break;
			case 9: nombreMes = "septiembre";	break;
			case 10: nombreMes = "octubre";		break;
			case 11: nombreMes = "noviembre";	break;
			case 12: nombreMes = "diciembre";	break;
			default: nombreMes="ERROR";
		}
		return nombreMes;
	}
 
	public String getDiaSemana(Fecha f1mes)
	{
		long f1,fref;
		int x;
		String ds;
		Fecha fRef= new Fecha(2,3,2009); // lunes
 
		x=contardias(fRef,f1mes);	// 
		x = x % 7;
		if(x<0) x = (x +7)%7;
 
		switch(x)
		{
			case 0: ds = "Lunes"; 		break;
			case 1: ds = "Martes"; 		break;
			case 2: ds = "Miercoles"; 	break;
			case 3: ds = "Jueves"; 		break;
			case 4: ds = "Viernes"; 	break;
			case 5: ds = "Sabado"; 		break;
			case 6: ds = "Domingo"; 	break;
			default: ds= "ERROR";
		}
		f1mes.diaSemana=ds;
		return ds;
	}
 
	public int getNumDiaSemana(Fecha f1mes)
	{
		long f1,fref;
		int x;
		String ds;
		Fecha fRef= new Fecha(2,3,2009); // lunes
 
		x=contardias(fRef,f1mes);	// 
		x = x % 7;
		if(x<0) x = (x +7)%7;
		f1mes.numDiaSem=x;
		return x;
	}
 
	public int contardias(Fecha f1, Fecha f2)
	{
		int fi,ff, tot;
		int a,m,mc,mf,ac,af;
		Fecha f;
		boolean neg;
		neg = false;
 
		fi = f1.dia + f1.mes * 100 + f1.ano * 10000;
		ff = f2.dia + f2.mes * 100 + f2.ano * 10000;
 
		if(fi > ff)
		{
			neg = true;
			f = f1;
			f1= f2;
			f2= f;
		}
		tot=f2.dia-f1.dia;
		for(a= f1.ano; a <= f2.ano; a++ )
		{
			if(a==f1.ano) 	mc = f1.mes;
			else		 	mc = 1;
			if(a == f2.ano) mf = f2.mes-1;
			else			mf = 12;
			for(m = mc ; m <= mf ; m++)
				tot += diasMes(m,a);
		}
		if(neg) tot = -tot;
		return tot;
	}
	public String toString()
	{
		return String.valueOf(dia)+"-"+nombreMes+"-"+String.valueOf(ano)+"-"+diaSemana;
	}
}



Comentarios sobre la versión: Versión 3.0 (0)


No hay comentarios
 

Comentar la versión: Versión 3.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s3816