U E D R , A S I H C RSS

The Java Man/달력

애플릿으로 짠거구

전에 C로 짠던걸 많이 이용했다(위키가 이럴땐 정말 좋다)

공부좀 열심히 해야쥐 ㅡ.ㅡ

~cpp 
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class CalendarApplet extends JApplet
{
	public void init()
	{
		Container contentPane = getContentPane();
		CalendarPanel panel = new CalendarPanel();
		contentPane.add(panel);
	}
}

class CalendarPanel extends JPanel
{
	JPanel selectPanel;
	JPanel showPanel;
	JTextField tfYear;
	JComboBox cbMonth;
	int year;
	int month;
	int days;

	public CalendarPanel()
	{
		setLayout(new BorderLayout());
		selectPanel = new JPanel();
		showPanel = new JPanel();
		tfYear = new JTextField(5);

		tfYear.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					changeData();
				}
			}
		);

		selectPanel.add(tfYear);
		selectPanel.add(new JLabel("년"));
		cbMonth = new JComboBox();
		cbMonth.setEditable(false);
		cbMonth.addItem("1");
		cbMonth.addItem("2");
		cbMonth.addItem("3");
		cbMonth.addItem("4");
		cbMonth.addItem("5");
		cbMonth.addItem("6");
		cbMonth.addItem("7");
		cbMonth.addItem("8");
		cbMonth.addItem("9");
		cbMonth.addItem("10");
		cbMonth.addItem("11");
		cbMonth.addItem("12");
		cbMonth.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					changeData();
				}
			}
		);

		selectPanel.add(cbMonth);
		selectPanel.add(new JLabel("월"));
		add(selectPanel, BorderLayout.NORTH);
		showPanel.setLayout(new GridLayout(7, 7));
		add(showPanel, BorderLayout.CENTER);

		tfYear.setText(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		cbMonth.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
	}

	public void alignDay()
	{
		int weekDay = (1 + (year - 1) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400)) % 7;
		for(int i = 0 ; i < month - 1 ; i++)
		{
			weekDay = (weekDay + monthDays(year, i + 1)) % 7;
		}
		showPanel.removeAll();

		showPanel.add(new JLabel("일", SwingConstants.CENTER));
		showPanel.add(new JLabel("월", SwingConstants.CENTER));
		showPanel.add(new JLabel("화", SwingConstants.CENTER));
		showPanel.add(new JLabel("수", SwingConstants.CENTER));
		showPanel.add(new JLabel("목", SwingConstants.CENTER));
		showPanel.add(new JLabel("금", SwingConstants.CENTER));
		showPanel.add(new JLabel("토", SwingConstants.CENTER));

		for (int i = 0; i < weekDay; i++)
		{
			showPanel.add(new JLabel(""));
		}

		for (int i = 0; i < days; i++)
		{
			showPanel.add(new JLabel(String.valueOf(i + 1), SwingConstants.CENTER));
		}

		for (int i = 0; i < 42 - weekDay - days; i++)
		{
			showPanel.add(new JLabel(""));
		}

		showPanel.updateUI();
	}

	public int monthDays(int ye, int mo)
	{
		if (mo == 1 || mo == 3 || mo == 5 || mo == 7 || mo == 8 || mo == 10 || mo == 12)
			return 31;
		else if (mo == 2)
		{
			if (isYunYear(ye) == true)
				return 29;
			return 28;
		}
		else
			return 30;
	}

	public boolean isYunYear(int ye)
	{
		if (ye % 4 == 0)
		{
			if (ye % 100 == 0)
			{
				if (ye % 400 == 0)
					return true;
				return false;
			}
			return true;
		}
		return false;
	}

	public void changeData()
	{
		if (!tfYear.getText().equals(""))
		{
			try
			{
				year = Integer.parseInt(tfYear.getText().trim());
				month = Integer.parseInt(cbMonth.getSelectedItem().toString());
				days = monthDays(year, month);
				alignDay();
			} catch (Exception e)
			{
				//System.out.println(e);
			}
		}
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:12
Processing time 0.0138 sec