source code here......

breathe (breathe)
本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
this is Date.java:

public class Date
{
private int year, month, day;
int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public Date(int y, int m, int d)
{
setYear(y);
setMonth(m);
setDay(d);
}

public void setYear(int pYear)
{
year = pYear;
}

public void setMonth(int pMonth)
{
month = (pMonth > 0 && pMonth < 13) ? pMonth : 1;
}

public void setDay(int pDay)
{
if (checkDay(pDay))
day = pDay;
else
day = 1;
}

private boolean checkDay(int pDay)
{
if (pDay > 0 && pDay <= daysPerMonth[month])
return true;
else
if (month == 2 && pDay == 29 && year % 4 == 0)
return true;
else
return false;
}

public int getYear()
{
return year;
}

public int getMonth()
{
return month;
}

public int getDay()
{
return day;
}

public void tickYear()
{
year++;
}

public void tickMonth()
{
month++;
if (month > 12)
{
month = 1;
year++;
}
if (day > daysPerMonth[month])
day = daysPerMonth[month];
}

public void tickDay()
{
day++;
setDay(day);
if (day == 1)
month++;
}

public String toString()
{
String monthNames[] = {"", "January",
"February",
"March",
"April",
"May", "June",
"July", "August", "September",
"October", "November", "December"};
return monthNames[month] + " " + day + ", " + year;
}
}


this is DateTest.java:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class DateTest extends Applet implements ActionListener
{
private Date date;

private int year,
month,
day;

private Label lSetYear,
lSetMonth,
lSetDay;

private TextField tYear,
tMonth,
tDay,
tSetDate;

private Button setButton;
private Button[] addButton = new Button[3];

public void init()
{
lSetYear = new Label("Set Year");
lSetMonth = new Label("Set Month");
lSetDay = new Label("Set Day");

tYear = new TextField(5);
tMonth = new TextField(5);
tDay = new TextField(5);
tSetDate = new TextField(25);

setButton = new Button("Set Date");
addButton[0] = new Button("Add a Year");
addButton[1] = new Button("Add a Month");
addButton[2] = new Button("Add a Day");

add(lSetMonth);
add(tMonth);
add(lSetDay);
add(tDay);
add(lSetYear);
add(tYear);
add(tSetDate);
add(setButton);
add(addButton[1]);
add(addButton[2]);
add(addButton[0]);

for (int i = 0; i < addButton.length; i++)
addButton[i].addActionListener(this);

setButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
year = Integer.parseInt(tYear.getText());
month = Integer.parseInt(tMonth.getText());
day = Integer.parseInt(tDay.getText());

if (e.getSource() == setButton)
date = new Date(year, month, day);

if (e.getSource() == addButton[0])
date.tickYear();

if (e.getSource() == addButton[1])
date.tickMonth();

if (e.getSource() == addButton[2])
date.tickDay();

tSetDate.setText(date.toString());
}
}
更多精彩文章及讨论,请光临枫下论坛. 网址: rolia.net/zh
(#241484@0)
2001-10-29 -05:00

回到话题: 初级java问题请教...

回到论坛: HOME枫下论坛枫下论坛主坛工作学习IT技术讨论

URL:   
http://www.rolia.net/zh/post.php?f=0&p=241484