compose a java class having relevant operation that shows a calender for a year and months. inpur for year and months are by parameter.example: SEPTEMBER 2007
sun mon tue wed thu fri sat
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 30thanks
sun mon tue wed thu fri sat
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 30thanks
Here is a example.I hope it could suit to your requirement ! GOOD LUCK~-.-import java.text.*;
import java.util.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;public final class winDateChoose
extends JPanel { private static final long serialVersionUID = 1L; private static final int startX = 10; private static final int startY = 60; private static final Font smallFont = new Font("Arial", Font.PLAIN, 10); private static final Font largeFont = new Font("Arial", Font.PLAIN, 12); private static final Insets insets = new Insets(2, 2, 2, 2); private static final Color highlight = new Color(255, 0, 0); private static final Color white = new Color(255, 255, 255); private static final Color gray = new Color(204, 204, 204); private Component selectedDay = null; private GregorianCalendar selectedDate = null; private GregorianCalendar originalDate = null; private boolean hideOnSelect = true; private final JButton backButton = new JButton(); private final JLabel monthAndYear = new JLabel(); private final JButton forwardButton = new JButton();
private JDialog mParentWin;
public boolean mOkClick = false;
public boolean mTimeShowFlag = true; private final JLabel[] dayHeadings = new JLabel[] {
new JLabel("Sun"),
new JLabel("Mon"),
new JLabel("Tur"),
new JLabel("Wen"),
new JLabel("Thu"),
new JLabel("Fri"),
new JLabel("Sat")}; private final JLabel[][] daysInMonth = new JLabel[][] {
{
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
}; private final JButton todayButton = new JButton(); private final JButton cancelButton = new JButton(); //Time Panel
JSpinner Timespinner; public winDateChoose() {
super();
selectedDate = getToday();
init();
} public void setTimeShowFlag(boolean flag){
mTimeShowFlag = flag;
if(!flag){
this.remove(this.Timespinner);
this.repaint();
}
} public void setParentWin(JDialog win) {
this.mParentWin = win;
} public winDateChoose(final Date initialDate) {
super();
if (null == initialDate) {
selectedDate = getToday();
}
else {
(selectedDate = new GregorianCalendar()).setTime(initialDate);
}
originalDate = new GregorianCalendar(
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
selectedDate.get(Calendar.DATE));
init();
} public boolean isHideOnSelect() {
return hideOnSelect;
} public void setHideOnSelect(final boolean hideOnSelect) {
if (this.hideOnSelect != hideOnSelect) {
this.hideOnSelect = hideOnSelect;
initButtons(false);
}
} public Date getDate() {
if (null != selectedDate) {
return selectedDate.getTime();
}
return null;
} public String getTime() {
if (this.Timespinner != null) {
String t = Timespinner.getValue().toString();
if (t != null) {
String[] tmp = t.split(" ");
String time = "0:0:0";
if (tmp.length >= 4) {
time = tmp[3];
}
return time;
}
}
return "0:0:0";
} private void init() {
setLayout(null);
setBorder(null); this.setMinimumSize(new Dimension(230, 250));
this.setMaximumSize(getMinimumSize());
this.setPreferredSize(getMinimumSize());
this.setBorder(new BorderUIResource.EtchedBorderUIResource()); backButton.setFont(smallFont);
backButton.setText("<");
backButton.setMargin(insets);
backButton.setDefaultCapable(false);
backButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onBackClicked(evt);
}
});
//add(backButton, new AbsoluteConstraints(10, 10, 20, 20));
backButton.setBounds(30, 10, 30, 20);
add(backButton);
//add(backButton, new Border(10, 10, 20, 20));
monthAndYear.setFont(largeFont);
monthAndYear.setHorizontalAlignment(JTextField.CENTER);
monthAndYear.setText(formatDateText(selectedDate.getTime()));
//add(monthAndYear, new AbsoluteConstraints(30, 10, 100, 20));
monthAndYear.setBounds(60, 10, 100, 20);
add(monthAndYear);
forwardButton.setText(">");
forwardButton.setMargin(insets);
forwardButton.setDefaultCapable(false);
forwardButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onForwardClicked(evt);
}
});
//add(forwardButton, new AbsoluteConstraints(130, 10, 20, 20));
forwardButton.setBounds(170, 10, 30, 20);
add(forwardButton); int x = startX;
for (int ii = 0; ii < dayHeadings.length; ii++) {
dayHeadings[ii].setOpaque(true);
dayHeadings[ii].setBackground(Color.lightGray);
dayHeadings[ii].setForeground(Color.white);
dayHeadings[ii].setHorizontalAlignment(JLabel.CENTER);
dayHeadings[ii].setBounds(x, 40, 31, 21);
add(dayHeadings[ii]);
x += 30;
}
x = startX;
int y = startY;
for (int ii = 0; ii < daysInMonth.length; ii++) {
for (int jj = 0; jj < daysInMonth[ii].length; jj++) {
daysInMonth[ii][jj].setOpaque(true);
daysInMonth[ii][jj].setBackground(white);
daysInMonth[ii][jj].setFont(smallFont);
daysInMonth[ii][jj].setHorizontalAlignment(JLabel.CENTER);
daysInMonth[ii][jj].setText("");
daysInMonth[ii][jj].addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent evt) {
onDayClicked(evt);
}
});
});
//add(daysInMonth[ii][jj], new AbsoluteConstraints(x, y, 21, 21));
daysInMonth[ii][jj].setBounds(x, y, 31, 21);
add(daysInMonth[ii][jj]);
x += 30;
}
x = startX;
y += 20;
}
initButtons(true); calculateCalendar();
} public void hideTimeSpinner() {
this.remove(Timespinner);
this.repaint();
} private void initButtons(final boolean firstTime) {
if (firstTime) {
final Dimension buttonSize = new Dimension(68, 24);
todayButton.setFont(new java.awt.Font(DAPUI.FONT_NAME, 0, 12));
todayButton.setText(DAPUI.BUTTON_OK);
//todayButton.setFont(largeFont);
todayButton.setMargin(insets);
todayButton.setMaximumSize(buttonSize);
todayButton.setMinimumSize(buttonSize);
todayButton.setPreferredSize(buttonSize);
todayButton.setDefaultCapable(true);
todayButton.setSelected(true);
todayButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onToday(evt);
}
});
cancelButton.setFont(new java.awt.Font(DAPUI.FONT_NAME, 0, 12));
cancelButton.setText(DAPUI.BUTTON_CANCEL);
//cancelButton.setFont(largeFont);
cancelButton.setMargin(insets);
cancelButton.setMaximumSize(buttonSize);
cancelButton.setMinimumSize(buttonSize);
cancelButton.setPreferredSize(buttonSize);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onCancel(evt);
}
});
}
else {
this.remove(todayButton);
this.remove(cancelButton);
} if (hideOnSelect) {
//add(todayButton, new AbsoluteConstraints(25, 190, 52, -1));
todayButton.setBounds(50, 215, 52, 30);
add(todayButton);
//add(cancelButton, new AbsoluteConstraints(87, 190, 52, -1));
cancelButton.setBounds(125, 215, 52, 30);
add(cancelButton);
}
else {
//add(todayButton, new AbsoluteConstraints(55, 190, 52, -1));
todayButton.setBounds(55, 215, 52, 30);
add(todayButton);
} //init time field
if(mTimeShowFlag){
createTimePanel();
this.Timespinner.setBounds(10, 190, 215, 20);
this.add(Timespinner);
}
} private void onToday(final java.awt.event.ActionEvent evt) {
//selectedDate = getToday();
setVisible(!hideOnSelect);
if (isVisible()) {
monthAndYear.setText(formatDateText(selectedDate.getTime()));
calculateCalendar();
}
if (mParentWin != null) {
mParentWin.dispose();
}
mOkClick = true;
} private void onCancel(final ActionEvent evt) {
selectedDate = originalDate;
setVisible(!hideOnSelect);
if (mParentWin != null) {
mParentWin.dispose();
}
} public void onForwardClicked(final java.awt.event.ActionEvent evt) {
final int day = selectedDate.get(Calendar.DATE);
selectedDate.set(Calendar.DATE, 1);
selectedDate.add(Calendar.MONTH, 1);
selectedDate.set(Calendar.DATE,
Math.min(day, calculateDaysInMonth(selectedDate)));
monthAndYear.setText(formatDateText(selectedDate.getTime()));
calculateCalendar();
} private void onBackClicked(final java.awt.event.ActionEvent evt) {
final int day = selectedDate.get(Calendar.DATE);
selectedDate.set(Calendar.DATE, 1);
selectedDate.add(Calendar.MONTH, -1);
selectedDate.set(Calendar.DATE,
Math.min(day, calculateDaysInMonth(selectedDate)));
monthAndYear.setText(formatDateText(selectedDate.getTime()));
calculateCalendar();
} public void resetDayButton() {
int y = startY;
for (int ii = 0; ii < daysInMonth.length; ii++) {
for (int jj = 0; jj < daysInMonth[ii].length; jj++) {
daysInMonth[ii][jj].setBackground(white);
}
}
} private void onDayClicked(final java.awt.event.MouseEvent evt) {
resetDayButton();
final javax.swing.JLabel fld = (javax.swing.JLabel) evt.getSource();
if (!"".equals(fld.getText())) {
fld.setBackground(highlight);
selectedDay = fld;
selectedDate.set(Calendar.DATE,
Integer.parseInt(fld.getText()));
//setVisible(!hideOnSelect);
}
} public static GregorianCalendar getToday() {
final GregorianCalendar gc = new GregorianCalendar();
gc.set(Calendar.HOUR_OF_DAY, 0);
gc.set(Calendar.MINUTE, 0);
gc.set(Calendar.SECOND, 0);
gc.set(Calendar.MILLISECOND, 0);
return gc;
} private void calculateCalendar() {
if (null != selectedDay) {
selectedDay.setBackground(white);
selectedDay = null;
} final GregorianCalendar c = new GregorianCalendar(
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
1); final int maxDay = calculateDaysInMonth(c); final int selectedDay = Math.min(maxDay, selectedDate.get(
Calendar.DATE)); int dow = c.get(Calendar.DAY_OF_WEEK);
for (int dd = 0; dd < dow; dd++) {
daysInMonth[0][dd].setText("");
} int week;
do {
week = c.get(Calendar.WEEK_OF_MONTH);
dow = c.get(Calendar.DAY_OF_WEEK);
final JLabel fld = this.daysInMonth[week - 1][dow - 1];
fld.setText(Integer.toString(c.get(Calendar.DATE)));
if (selectedDay == c.get(Calendar.DATE)) {
fld.setBackground(highlight);
this.selectedDay = fld;
}
if (c.get(Calendar.DATE) >= maxDay) {
break;
}
c.add(Calendar.DATE, 1);
}
while (c.get(Calendar.DATE) <= maxDay); week--;
for (int ww = week; ww < daysInMonth.length; ww++) {
for (int dd = dow; dd < daysInMonth[ww].length; dd++) {
daysInMonth[ww][dd].setText("");
}
dow = 0;
} c.set(Calendar.DATE, selectedDay);
selectedDate = c;
} public static int calculateDaysInMonth(final Calendar c) {
int daysInMonth = 0;
switch (c.get(Calendar.MONTH)) {
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
daysInMonth = 31;
break;
case 3:
case 5:
case 8:
case 10:
daysInMonth = 30;
break;
case 1:
final int year = c.get(Calendar.YEAR);
daysInMonth =
(0 == year % 1000) ? 29 :
(0 == year % 100) ? 28 :
(0 == year % 4) ? 29 : 28;
break;
}
return daysInMonth;
} private static String formatDateText(final Date dt) {
final DateFormat df = DateFormat.getDateInstance(DateFormat.LONG); final StringBuffer dd = new StringBuffer();
final StringBuffer mm = new StringBuffer();
final StringBuffer yy = new StringBuffer();
final FieldPosition ddfp = new FieldPosition(DateFormat.DATE_FIELD);
final FieldPosition mmfp = new FieldPosition(DateFormat.MONTH_FIELD);
final FieldPosition yyfp = new FieldPosition(DateFormat.YEAR_FIELD);
df.format(dt, dd, ddfp);
df.format(dt, mm, mmfp);
df.format(dt, yy, yyfp);
return (yy.toString().substring(yyfp.getBeginIndex(), yyfp.getEndIndex()) +
"-" +
mm.toString().substring(mmfp.getBeginIndex(), mmfp.getEndIndex()));
} //Create time panel
private void createTimePanel() {
SpinnerDateModel model2 = new SpinnerDateModel();
model2.setCalendarField(Calendar.WEEK_OF_MONTH);
Timespinner = new JSpinner(model2);
JSpinner.DateEditor editor2 = new JSpinner.DateEditor(Timespinner,
"HH:mm:ss");
Timespinner.setEditor(editor2);
}
}
public class TestCalender
{
public TestCalender(int year,int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month,1);
int week = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday ");
for(int i=0;i<(week-1)%7;i++)
{
System.out.print(" ");
}
for(int i=1;i<=DaysInMonth(year,month);i++)
{
if((week+i-2)%7==0)
System.out.println();
if(i<=9)
{
System.out.print(i+" ");
}
else
{
System.out.print(i+" ");
}
}
}
public int DaysInMonth(int year,int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month+1,0);
return calendar.get(Calendar.DAY_OF_MONTH);
}
public static void main(String[]args)
{
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args[1]);
System.out.println(year+" "+month+" ");
new TestCalender(year-1,month-1);
}
}
import java.util.Calendar;
public class TestCalender
{
public TestCalender(int year,int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month,1);//设置为该月一号
int week = calendar.get(Calendar.DAY_OF_WEEK);
//返回一号是周几:周日返回1,等等
System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday ");
//这句写的不好,有没有办法能设置输出宽度?就象c中%10d那样
for(int i=0;i<(week-1)%7;i++)
{
System.out.print(" ");//输出一号前面的空格
}
for(int i=1;i<=DaysInMonth(year,month);i++)
{
if((week+i-2)%7==0)//逢周六换行
System.out.println();
if(i<=9)
//这句写的非常不好,有没有办法能设置输出宽度?就象c中%10d那样,我是初学者
{
System.out.print(i+" ");
}
else
{
System.out.print(i+" ");
}
}
}
public int DaysInMonth(int year,int month)//返回当前月一共有多少天
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month+1,0);//这周最后一天即是下周"o号"
return calendar.get(Calendar.DAY_OF_MONTH);
}
public static void main(String[]args)
{
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args[1]);
System.out.println(year+" "+month+" ");
new TestCalender(year-1,month-1);
//month-1因为:一月是常量0,见Constant Field Values
//Month value is 0-based. e.g., 0 for January
}
}
because java is written in english.
it is troublesome to translate to mandarin, then u need to translate to english again.
anyway... tq.....actually i dunno how to write in mandarin(convert the question) because in my country, java is taught in english not chinese.... dun worry, i am chinese...
thanks...
but still cannot work... got error.
如果你不明白就javac TestCalender.java(拼错了,现在才看出来,呵呵) 然后
java TestCalender 2007 1
我的那个应该能运行,我这里可以。
the three parts together(Sorry,for the issue limit~I have to divide it).
If you wanna use the demo,you can extends and show it~
For the details you can use QQ(38690157) to ask me ~
when i run it, this error msg come out::
Exception in thread "main" java.lang.NoClassDefFoundError: TestCalenderhow come??
your coding really long long oh....
for amateur like me really hard to digest la...
javac TestCalender.java
java TestCalender 2007 1
再不行联系我:qq 28846142
public class TestCalender
{
public TestCalender(int year,int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month,1);
int week = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday ");
for(int i=0;i<(week-1)%7;i++)
{
System.out.print(" ");
}
for(int i=1;i<=DaysInMonth(year,month);i++)
{
if((week+i-2)%7==0)
System.out.println();
if(i<=9)
{
System.out.print(i+" ");
}
else
{
System.out.print(i+" ");
}
}
}
public int DaysInMonth(int year,int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month+1,0);
return calendar.get(Calendar.DAY_OF_MONTH);
}
public static void main(String[]args)
{
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args[1]);
System.out.println(year+" "+month+" ");
new TestCalender(year-1,month-1);}
}
2007 1
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
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
tq anyway...
my java program still cannot work....
how to use JCREATOR to key the 2007 1 value?
1. calendar.set(year + 1900, month + 1, 0); //去掉了加1900,直接写成year
2. new TestCalender(year - 1, month - 1); //这里的不需要year - 1; 直接写成year;
3. for (int i = 0; i < (week - 1) % 7; i++) { //有改动
4. if ((week + i - 2) % 7 == 0) // 有改动import java.util.Calendar;public class TestCalender2 {
public TestCalender2(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, 1);// ?置??月一号
int week = calendar.get(Calendar.DAY_OF_WEEK);
// 返回一号是周几:周日返回1,等等
System.out
.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday ");
// ?句写的不好,有没有?法能?置?出?度?就象c中%10d那?
for (int i = 0; i < week - 1; i++) {
System.out.print(" ");// ?出一号前面的空格
}
for (int i = 1; i <= DaysInMonth(year, month); i++) {
if (((week + i - 2) % 7) == 0 && (week + i - 2) != 0)// 逢周六?行
System.out.println();
if (i <= 9)
// ?句写的非常不好,有没有?法能?置?出?度?就象c中%10d那?,我是初学者
{
System.out.print(i + " ");
} else {
System.out.print(i + " ");
}
}
}
public int DaysInMonth(int year, int month)// 返回当前月一共有多少天
{
Calendar calendar = Calendar.getInstance();
calendar.set(year, month + 1, 0);// ?周最后一天即是下周"o号"
return calendar.get(Calendar.DAY_OF_MONTH);
} public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args[1]);
System.out.println(year + " " + month + " ");
new TestCalender2(year, month - 1);
// month-1因?:一月是常量0,?Constant Field Values
// Month value is 0-based. e.g., 0 for January
}
}
it is the classpath problem....
thanks all, especially china8848(永远在一起) thank you.... the program works....
calendar.set(year+1900,month,1);的year+1900
new TestCalender(year-1,month-1);的year-1
分别是什么意思?
谢谢