mark79_24
2006-07-27 23:34:09 UTC
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/*
* Created on 2003/12/02
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author ljdina
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
class CalendarPanel extends JPanel { //calendar‚ð•\Ž¦‚·‚éƒpƒlƒ‹
int firstday;
int lastday;
JPanel panel1;
JPanel panel2;
JLabel thismonth;
JLabel thisyear;
JTable calendartable;
JTable daystable;
JLabel weekdays[] = new JLabel[7];
JLabel[][] tabledata;
int length = 5;
String days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
public final int DaysInMonth[] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
CalendarPanel(int month, int year) {
super();
thismonth = new JLabel(Integer.toString(month) + "ŒŽ");
thisyear = new JLabel(Integer.toString(year) + "”N");
Font monthfont = new Font("MS P–¾’©", Font.BOLD, 18);
thismonth.setFont(monthfont);
thismonth.setForeground(Color.black);
thisyear.setFont(monthfont);
thisyear.setForeground(Color.black);
panel2 = new JPanel();
panel2.setSize(238, 180);
FirstDay(month, year);
LastDay(month, year);
if (firstday == 6 && lastday == 0) length = 6;
if(DaysInMonth[month-1]==31 && firstday == 5 && lastday == 0)length = 6;
tabledata = new JLabel[length][7];
panel2.setLayout(new GridLayout(length,7));
PutInData(month,year,length);
panel1 = new JPanel();
panel1.setSize(238, 30);
panel1.setBackground(Color.gray);
panel1.setLayout(new GridLayout(1, 7));
for (int i = 0; i < 7; i++) {
weekdays[i] = new JLabel(days[i]);
weekdays[i].setSize(34, 30);
weekdays[i].setBackground(Color.gray);
weekdays[i].setBorder(BorderFactory.createLineBorder(Color.black));
weekdays[i].setFont(monthfont);
panel1.add(weekdays[i]);
}
setSize(280, 260);
setLayout(null);
thismonth.setSize(70, 26);
thismonth.setLocation(145, 10);
thisyear.setSize(80, 26);
thisyear.setLocation(45, 10);
add(thisyear);
add(thismonth);
panel1.setLocation(20, 40);
add(panel1);
panel2.setLocation(20, 70);
panel2.setBackground(Color.gray);
add(panel2);
setBackground(Color.gray);
}
public void FirstDay(int month, int year) {
firstday =
(year
+ year / 4
- year / 100
+ year / 400
+ (13 * month + 8) / 5
+ 1)
% 7;
}
public void LastDay(int month, int year) {
lastday =
(year
+ year / 4
- year / 100
+ year / 400
+ (13 * month + 8) / 5
+ DaysInMonth[month
- 1])
% 7;
}
public void PutInData(int month, int year,int length) {
if (month == 2 && isLeapYear(year))
DaysInMonth[1] = 29;
for (int i = 0; i < length; i++) {
for (int j = 0; j < 7; j++) {
tabledata[i][j] = new JLabel();
tabledata[i][j].setSize(34, 30);
tabledata[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
if (i == 0) {
if (j < (firstday))
tabledata[i][j].setText("");
else{
tabledata[i][j].setText(Integer.toString((j - firstday) + 1));
}
} else if (i == length) {
if(j <= lastday)tabledata[i][j].setText(Integer.toString((i * 7) - firstday +1+ j));
else tabledata[i][j].setText("");
} else {
if (((i * 7) - firstday+1 + j) > DaysInMonth[month - 1]) {
tabledata[i][j].setText("");
} else {
tabledata[i][j].setText(Integer.toString((i * 7) - firstday +1+ j));
}
}
if(j==0)tabledata[i][j].setForeground(Color.red);
if(j==6)tabledata[i][j].setForeground(Color.blue);
tabledata[i][j].setHorizontalAlignment(SwingConstants.RIGHT);
panel2.add(tabledata[i][j]);
}
}
}
public boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}
}
public class CalendarFrame extends JFrame implements ActionListener {
JLabel yearlabel;
JTextField useryear;
JLabel monthlabel;
JComboBox monthlist;
JButton userCalendar;
JButton lastmonth;
JButton nextmonth;
JButton lastyear;
JButton nextyear;
JButton recover;
public CalendarFrame(CalendarPanel panel) {
super();
yearlabel = new JLabel("”N");
useryear = new JTextField();
monthlabel = new JLabel("ŒŽ");
String monthitem;
monthlist = new JComboBox();
for (int i = 0; i < 12; i++) {
monthitem = Integer.toString(i + 1);
monthlist.addItem(monthitem);
}
userCalendar = new JButton("");
lastmonth = new JButton("挎");
nextmonth = new JButton("—ˆŒŽ");
lastyear = new JButton("‹Ž”N");
nextyear = new JButton("—ˆ”N");
recover = new JButton("–ß‚é");
yearlabel.setHorizontalAlignment(SwingConstants.RIGHT);
monthlabel.setHorizontalAlignment(SwingConstants.RIGHT);
userCalendar.setHorizontalAlignment(SwingConstants.CENTER);
userCalendar.setVerticalAlignment(SwingConstants.CENTER);
lastmonth.setHorizontalAlignment(SwingConstants.CENTER);
lastmonth.setVerticalAlignment(SwingConstants.CENTER);
nextmonth.setHorizontalAlignment(SwingConstants.CENTER);
nextmonth.setVerticalAlignment(SwingConstants.CENTER);
lastyear.setHorizontalAlignment(SwingConstants.CENTER);
lastyear.setVerticalAlignment(SwingConstants.CENTER);
nextyear.setHorizontalAlignment(SwingConstants.CENTER);
nextyear.setVerticalAlignment(SwingConstants.CENTER);
recover.setHorizontalAlignment(SwingConstants.CENTER);
recover.setVerticalAlignment(SwingConstants.CENTER);
getContentPane().setLayout(null); //ƒŒƒCƒAƒEƒgƒ}ƒl[ƒWƒƒ‚ðŽg—p‚µ‚È‚¢‚悤‚É
setSize(400, 400);
setLocation(200, 200);
setResizable(false);
getContentPane().setBackground(Color.WHITE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
yearlabel.setSize(50, 26);
yearlabel.setLocation(10, 10);
useryear.setSize(80, 26);
useryear.setLocation(62, 10);
monthlabel.setSize(50, 26);
monthlabel.setLocation(150, 10);
monthlist.setSize(50, 26);
monthlist.setLocation(202, 10);
userCalendar.setSize(80, 26);
userCalendar.setLocation(300, 10);
panel.setLocation(50, 65);
getContentPane().add(yearlabel);
getContentPane().add(useryear);
getContentPane().add(monthlabel);
getContentPane().add(monthlist);
getContentPane().add(userCalendar);
getContentPane().add(panel);
userCalendar.addActionListener(this);
lastmonth.setSize(65, 26);
lastmonth.setLocation(20, 330);
nextmonth.setSize(65, 26);
nextmonth.setLocation(90, 330);
lastyear.setSize(65, 26);
lastyear.setLocation(160, 330);
nextyear.setSize(65, 26);
nextyear.setLocation(230, 330);
recover.setSize(65, 26);
recover.setLocation(300, 330);
getContentPane().add(lastmonth);
getContentPane().add(nextmonth);
getContentPane().add(lastyear);
getContentPane().add(nextyear);
getContentPane().add(recover);
lastmonth.addActionListener(this);
nextmonth.addActionListener(this);
lastyear.addActionListener(this);
nextyear.addActionListener(this);
recover.addActionListener(this);
if(panel.thismonth.getText().equals("9ŒŽ")){
JOptionPane.showMessageDialog(
this,
"9ŒŽ24“ú‰F’Ö؈©‚Ì’a¶“ú‚Å‚·‚æBƒvƒŒƒ[ƒ“ƒg‚ð—pˆÓ‚µ‚ĉº‚³‚¢B",
"Œxƒ_ƒCƒAƒƒO",
JOptionPane.WARNING_MESSAGE);
}
}
public static void main(String[] args) { //ƒƒCƒ“ƒƒ\ƒbƒh
Calendar todaydate = Calendar.getInstance();
int month = todaydate.get(Calendar.MONTH) + 1;
int year = todaydate.get(Calendar.YEAR);
CalendarPanel todaypanel = new CalendarPanel(month, year);
CalendarFrame frame = new CalendarFrame(todaypanel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(userCalendar)) {
if (useryear.getText() != null
&& useryear.getText().length() != 0) {
int year = Integer.parseInt(useryear.getText());
int month =
Integer.parseInt((String) monthlist.getSelectedItem());
calendarDisplay(month, year);
} else {
JOptionPane.showMessageDialog(
this,
"”N‚Ì’l‚ð“ü—Í‚µ‚ĉº‚³‚¢B",
"Œxƒ_ƒCƒAƒƒO",
JOptionPane.WARNING_MESSAGE);
}
} else if (e.getSource().equals(lastmonth)) {
Calendar date = Calendar.getInstance();
int month = date.get(Calendar.MONTH) + 1;
int year = date.get(Calendar.YEAR);
if (month == 1) {
month = 12;
year = year - 1;
} else
month = month - 1;
calendarDisplay(month, year);
} else if (e.getSource().equals(nextmonth)) {
Calendar date = Calendar.getInstance();
int month = date.get(Calendar.MONTH) + 1;
int year = date.get(Calendar.YEAR);
if (month == 12) {
month = 1;
year = year + 1;
} else
month = month + 1;
calendarDisplay(month, year);
} else if (e.getSource().equals(lastyear)) {
Calendar date = Calendar.getInstance();
int month = date.get(Calendar.MONTH) + 1;
int year = date.get(Calendar.YEAR) - 1;
calendarDisplay(month, year);
} else if (e.getSource().equals(nextyear)) {
Calendar date = Calendar.getInstance();
int month = date.get(Calendar.MONTH) + 1;
int year = date.get(Calendar.YEAR) + 1;
calendarDisplay(month, year);
} else if (e.getSource().equals(recover)) {
Calendar date = Calendar.getInstance();
int month = date.get(Calendar.MONTH) + 1;
int year = date.get(Calendar.YEAR);
calendarDisplay(month, year);
}
}
public void calendarDisplay(int month, int year) {
CalendarPanel newpanel = new CalendarPanel(month, year);
CalendarFrame frame = new CalendarFrame(newpanel);
dispose();
frame.useryear.setText(Integer.toString(year));
frame.monthlist.setSelectedItem(Integer.toString(month));
frame.setVisible(true);
}
}