📄 generalizedtimeeditor.java
字号:
// Add SECOND components...
dateTimePanel.add(secondLabel);
dateTimePanel.add(secondCombo);
dateTimePanel.add(new JLabel(" "));
// Add MILLISECOND components...
dateTimePanel.add(milliSecondLabel);
dateTimePanel.add(milliSecondCombo);
// Add border...
dateTimePanel.setBorder(new TitledBorder(CBIntText.get(" Date/Time ")));
// Add 'Now' button and a check box to a new panel...
CBPanel btnPanel = new CBPanel();
btnPanel.makeLight();
btnPanel.addln(btnNow);
btnPanel.addln(checkBox);
// Set selected indexes and field text...
setSelectedIndexes();
// Add panels to main display panel...
display.makeHeavy();
display.add(dateTimePanel);
display.makeHigh();
display.add(btnPanel);
display.newLine();
display.makeHeavy();
// Padding at bottom...
display.addln(new JLabel(" "));
setSize(500, 200);
// Get the number of days in the selected month...
numDays = getDaysInMonth(userMonth);
}
/**
* Initialises the date and time variables depending on the current attribute value.
* For example: 20010920235959.999 would display...
* year - 2001
* month - September
* day - 20
* hour - 23 (11pm)
* minute - 59
* second - 59
* millisecond - 999
* If there is no value the date and time variables default to zero (0).
*/
protected void initDateTimeVariables()
{
int size = 0;
try
{
size = value.length();
}
catch (Exception e)
{
size = 0;
}
try
{
// Init month...
userMonth = Integer.parseInt(value.substring(4, 6));
// Init year...
userYear = Integer.parseInt(value.substring(0, 4));
// Init day...
userDay = Integer.parseInt(value.substring(6, 8));
// Init hour...
userHour = Integer.parseInt(value.substring(8, 10));
// Init minute...
userMinute = Integer.parseInt(value.substring(10, 12));
// Init second...
if (size > 13)
userSecond = Integer.parseInt(value.substring(12, 14));
// Init milli-seconds...
if (value.indexOf(".") == 14)
{
// Where is the Z?...
int zIndex = value.indexOf("Z");
String milli = null;
if (zIndex == 16)
milli = value.substring(15, 16) + "00"; // .9Z.
else if (zIndex == 17)
milli = value.substring(15, 17) + "0"; // .99Z.
else if (size == 18 || zIndex == 18)
milli = value.substring(15, 18); // .999 or .999Z.
else if (size == 17)
milli = value.substring(15, 17) + "0"; // .99.
else if (size == 16)
milli = value.substring(15, 16) + "00"; // .9.
userMilliSecond = Integer.parseInt(milli);
}
}
catch (Exception e)
{
userMonth = 0;
userYear = 0;
userDay = 0;
userHour = 0;
userMinute = 0;
userSecond = 0;
userMilliSecond = 0;
}
}
/**
* Returns the days in the month that is supplied.
* @param month an integer representation of the month
* for example: 1 = Jan, 2 = Feb...12 = Dec.
* 0 = an unspecified month which has 0 days.
* @return the days in the month for example:
* Jan = 31, Feb = 28 (29 if leap) etc.
*/
protected int getDaysInMonth(int month)
{
return daysInMonth[month] + ((isLeapYear(getUserYear()) &&
(userMonth == FEBRUARY)) ? 1 : 0);
}
/**
* Gets the current date and time from the user's machine and displays it.
*/
protected void displayCurrent()
{
// Get the date and time of right now...
rightNow = Calendar.getInstance();
userYear = rightNow.get(Calendar.YEAR);
// Jan is meant to be 1 but is it actually is 0 and so on dec = 11...
userMonth = rightNow.get(Calendar.MONTH) + 1;
userDay = rightNow.get(Calendar.DAY_OF_MONTH);
userHour = rightNow.get(Calendar.HOUR_OF_DAY);
userMinute = rightNow.get(Calendar.MINUTE);
userSecond = rightNow.get(Calendar.SECOND);
userMilliSecond = rightNow.get(Calendar.MILLISECOND);
yearTextField.setText(String.valueOf(userYear));
monthCombo.setSelectedIndex(userMonth);
dayCombo.setSelectedIndex(userDay);
hourCombo.setSelectedIndex(userHour);
minuteCombo.setSelectedIndex(userMinute);
secondCombo.setSelectedIndex(userSecond);
milliSecondCombo.setSelectedIndex(userMilliSecond);
}
/**
* Sets the index of the combo boxes and the year field to the already initiated
* date an time variables.
*/
protected void setSelectedIndexes()
{
// Year...
yearTextField.setText(String.valueOf(userYear));
// Month...
if (userMonth <= 13)
monthCombo.setSelectedIndex(userMonth);
else
userMonth = 0;
// Day...
if (userDay <= getDaysInMonth(userMonth))
dayCombo.setSelectedIndex(userDay);
// Hour...
if (userHour <= 23)
hourCombo.setSelectedIndex(userHour);
// Minute...
if (userMinute <= 59)
minuteCombo.setSelectedIndex(userMinute);
// Second...
if (userSecond <= 59)
secondCombo.setSelectedIndex(userSecond);
// Millisecond...
if (userMilliSecond <= 999)
milliSecondCombo.setSelectedIndex(userMilliSecond);
}
/**
* Initialises the milli-second combo with integers from 0-999.
*/
protected void initMilliSeconds()
{
for (int i = 0; i < 1000; i++)
milliSecondCombo.addItem(new Integer(i));
}
/**
* Initialises the second combo with integers from 0-59.
*/
protected void initSeconds()
{
for (int i = 0; i < 60; i++)
secondCombo.addItem(new Integer(i));
}
/**
* Initialises the minute combo with integers from 0-59.
*/
protected void initMinutes()
{
for (int i = 0; i < 60; i++)
minuteCombo.addItem(new Integer(i));
}
/**
* Initialises the hour combo with integers from 0-23.
*/
protected void initHours()
{
for (int i = 0; i < 24; i++)
hourCombo.addItem(new Integer(i));
}
/**
* Initialises the second combo with integers from 0-31, 0-30, 0-29 or 0-28
* depending on the number of days supplied.
* @param days the maximum number of days to display. Can be 0, 28, 29, 30 ,31.
*/
protected void initDays(int days)
{
for (int i = 0; i <= days; i++)
{
if (i == 0)
dayCombo.addItem("--");
else
dayCombo.addItem(new Integer(i));
}
}
/**
* Initialises the month combo with months of the year.
* For example: --, January, Febuarary...December.
*/
protected void initMonths()
{
for (int i = 0; i < 13; i++)
monthCombo.addItem(months[i]);
}
/**
* Re-populates the day combo to reflect the number of
* days of the month that is selected in the month combo.
* Accounts for leap year also.
*/
protected void updateDayCombo()
{
// Remember the selected position in the day combo...
int dayPos = dayCombo.getSelectedIndex();
// Get the index of the selected month...
userMonth = monthCombo.getSelectedIndex();
// Empty the day combo...
dayCombo.removeAllItems();
// Get the days in the month (account for leap year)...
numDays = getDaysInMonth(userMonth);
// Add the days to the day combo...
initDays(numDays);
if (dayPos > -1)
{
setDayComboSelectedIndex(dayPos);
// Make a backup of the last day index incase something goes wrong......
dateIndexBackup = dayPos;
}
else
{
// Something has gone wrong so set the day combo to the backup index...
setDayComboSelectedIndex(dateIndexBackup);
}
dayCombo.revalidate();
}
/**
* Returns the year from the year field as an integer. If there is a problem
* parsing the value (which there shouldn't be because the year field is restricted to
* whole numbers), the year is set to zero (0).
* @return the year as displayed in the year field.
*/
protected int getUserYear()
{
int userYearInt = 0;
try
{
// Get the year from the text field and parse it...
userYearInt = Integer.parseInt(yearTextField.getText(), 10);
}
catch (Exception e)
{
// If the year is something weird (other than blank) show the message...
if (!(yearTextField.getText().length() == 0))
JOptionPane.showMessageDialog(this, CBIntText.get("Please enter a valid year."),
CBIntText.get("Invalid Year"), JOptionPane.INFORMATION_MESSAGE);
userYear = 0;
yearTextField.setText(String.valueOf(userYear));
}
if (userYearInt > 1581)
userYear = userYearInt;
return userYear;
}
/**
* Displays the value in the day combo at the index supplied.
* If there is a problem with the index being out of bounds (or the like),
* the index displayed is the last available in the combo.
* @param index the index of the value to display in the day combo.
*/
protected void setDayComboSelectedIndex(int index)
{
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -