📄 speechcalendar.java
字号:
{
temp=getYear(Integer.parseInt(val3));
speak=getMntName(val1)+" "+val2+" "+temp;
System.out.println(speak);
}
else
/*
Checks if the selected format is dd/mm/yy or dd/mm/yyyy
*/
if(format.equals("dd/mm/yy")||format.equals("dd/mm/yyyy"))
{
temp=getYear(Integer.parseInt(val3));
speak=val1+" "+getMntName(val2)+" "+temp;
System.out.println(speak);
}
}
}
else
/*
Checks if the JRadioButton, Enter Date is selected
*/
if(selDate.isSelected())
{
/*
Create an instance of StringTokenizer class
*/
StringTokenizer st=new StringTokenizer(dateText.getText(),"/");
/*
Get the count of total number of tokens in the StringTokenizer class
*/
int count=st.countTokens();
/*Checks if the value of count is 3*/
if(count==3)
{
/*
Checks and executes if the StringTokenizer class contains any token
*/
while(st.hasMoreTokens())
{
/*
Retrieve the tokens in String variables.
*/
String chk1=st.nextToken();
String chk2=st.nextToken();
String chk3=st.nextToken();
String tmp;
/*
Convert the string values of tokens to integers
*/
int i=Integer.parseInt(chk1);
int j=Integer.parseInt(chk2);
int k=Integer.parseInt(chk3);
/*
Checks if the selected format is yyyy/mm/dd or yy/mm/dd
*/
if (format.equals("yyyy/mm/dd")||format.equals("yy/mm/dd"))
{
/*
Checks if the date entered is valid or not.
*/
boolean res=dateIsValid(i,j,k);
if(res)
{
tmp=getYear(i);
speak=tmp+" "+getMntName(chk2)+" "+chk3;
}
else
{
/*
Prompts that an invalid date is entered
*/
speak="Invalid date, please try again";
dateText.setText("");
dateText.grabFocus();
}
}
else
/*
Checks if the selected format is mm/dd/yy or mm/dd/yyyy
*/
if(format.equals("mm/dd/yy")||format.equals("mm/dd/yyyy"))
{
/*
Checks for a valid date
*/
boolean res=dateIsValid(k,i,j);
if (res)
{
tmp=getYear(k);
speak=getMntName(chk1)+" "+chk2+" "+tmp;
}
else
{
/*
Prompts for an invalid date entry
*/
speak="Invalid date, please try again";
dateText.setText("");
dateText.grabFocus();
}
}
else
/*
Checks if the selected date format is dd/mm/yy or dd/mm/yyyy
*/
if(format.equals("dd/mm/yy")||format.equals("dd/mm/yyyy"))
{
/*
Checks if a date is valid
*/
boolean res=dateIsValid(k,j,i);
if(res)
{
tmp=getYear(k);
speak=chk1+" "+getMntName(chk2)+" "+tmp;
}
else
{
/*
Prompts that the date entered is invalid
*/
speak="Invalid date, please try again";
dateText.setText("");
dateText.grabFocus();
}
}
}
}
else
{
/*
Prompts for an invalid date entry format
*/
speak="Invalid date format, enter again";
/*
Clear the dateText text field
*/
dateText.setText("");
/*
Set the focus to the dateText textfield
*/
dateText.grabFocus();
}
}
}
/*
This code executes when the user clicks the Speak button in the Time pane of JTabbedPane
*/
else if(ev.getSource()==timeSpeak)
{
speak="no entry specified";
/*
Checks if the JRadioButton, Current Time is selected
*/
if(cTime.isSelected())
{
/*
Create an object of StringTokenizer class
*/
StringTokenizer st=new StringTokenizer(time.getText(),":");
/*
Checks if the StringTokenizer class has any element
*/
while(st.hasMoreTokens())
{
speak="The time is"+st.nextToken()+" hours "+st.nextToken()+" minutes"+st.nextToken()+" seconds";
}
}
else
/*
Checks if the JRadioButton, Enter Time is selected
*/
if(selTime.isSelected())
{
/*
Create an object of StringTokenizer class
*/
StringTokenizer st=new StringTokenizer(timeText.getText(),":");
/*
Count the number of tokens in the StringTokenizer class
*/
int count=st.countTokens();
/*
Checks if three tokens exists
*/
if (count==3)
{
/*
Executes if the STringTokenizer class contains any token
*/
while (st.hasMoreTokens())
{
/*
Convert the values of current hour, current minutes, and current seconds to integer values
*/
int hr=Integer.parseInt(st.nextToken());
int min=Integer.parseInt(st.nextToken());
int sec=Integer.parseInt(st.nextToken());
/*
checks if the time specified is valid
*/
if(hr>=0 && hr<=24 && min>=0 && min<60 && sec>=0 && sec<60)
speak=" The time is"+String.valueOf(hr)+"hour"+String.valueOf(min)+"minutes"+String.valueOf(sec)+"seconds";
else
{
/*
Prompt for invalid entry of time
*/
speak=" The time specified is invalid, please enter again";
/*
clear the text field to enter time
*/
timeText.setText("");
/*
Set the focus to the cleasred text field
*/
timeText.grabFocus();
}
}
}
else
{
/*
Prompts that the entry for time is invalid
*/
speak="Invalid format for time, enter again";
timeText.setText("");
timeText.grabFocus();
}
}
}
/*
create and initializes an instance of SpeakText class.
*/
SpeakText hw=new SpeakText(speak);
}
}
/*
setFormat():Select the date formet
Parameters:
s: An object of String class
Return Type:NA
*/
public void setFormat(String s)
{
format=s;
System.out.println(format);
StringTokenizer dateToken=new StringTokenizer(dateValue,"/");
/*
Checks if the selected date format is yyyy/mm/dd
*/
if (s.equals("yyyy/mm/dd"))
{
selDate.setLabel("Enter Date (yyyy/mm/dd)");
date.setText(k1+"/"+k2+"/"+k3);
}
else
/*
Checks if the selected date format is yy/mm/dd
*/
if(s.equals("yy/mm/dd"))
{
selDate.setLabel("Enter Date (yy/mm/dd)");
date.setText(k1.substring(2,4)+"/"+k2+"/"+k3);
}
else
/*
Checks if the selected date format is mm/dd/yy
*/
if(s.equals("mm/dd/yy"))
{
selDate.setLabel("Enter Date (mm/dd/yy)");
date.setText(k2+"/"+k3+"/"+k1.substring(2,4));
}
else
/*
Checks if the selected date format is mm/dd/yyyy
*/
if(s.equals("mm/dd/yyyy"))
{
selDate.setLabel("Enter Date (mm/dd/yyyy)");
date.setText(k2+"/"+k3+"/"+k1);
}
else
/*
Checks if the selected date format is dd/mm/yy
*/
if(s.equals("dd/mm/yy"))
{
selDate.setLabel("Enter Date (dd/mm/yy)");
date.setText(k3+"/"+k2+"/"+k1.substring(2,4));
}
else
/*
Checks if the selected date format is dd/mm/yyyy
*/
if(s.equals("dd/mm/yyyy"))
{
selDate.setLabel("Enter Date (dd/mm/yyyy)");
date.setText(k3+"/"+k2+"/"+k1);
}
dateText.setText("");
dateText.grabFocus();
}
/*
getMntName(): Retrieve the string value of the month passed as a parameter to the method
Parameters:
mnt: An object of String class.
Return Type: String
*/
public String getMntName(String mnt)
{
String month=null;
/*
Retrieve the corresponding string value of the month
*/
if(mnt.equals("1")||mnt.equals("01")) month = "January";
else if(mnt.equals("2")||mnt.equals("02")) month = "Feburary";
else if(mnt.equals("3")||mnt.equals("03")) month = "March";
else if(mnt.equals("4")||mnt.equals("04")) month = "April";
else if(mnt.equals("5")||mnt.equals("05")) month = "May";
else if(mnt.equals("6")||mnt.equals("06")) month = "June";
else if(mnt.equals("7")||mnt.equals("07")) month = "July";
else if(mnt.equals("8")||mnt.equals("08")) month = "August";
else if(mnt.equals("9")||mnt.equals("09")) month = "September";
else if(mnt.equals("10")) month = "October";
else if(mnt.equals("11")) month = "November";
else if(mnt.equals("12")) month = "December";
/*Return month*/
return month;
}
/*
dateIsValid():Checks if a date is valid
Parameters:
year: Integer value for the year.
month: Integer value for the month.
day: Integer value for the day.
Return Type: boolean
*/
private static boolean dateIsValid(int year, int month, int day)
{
boolean result = true;
try
{
/*
Create an object of DateFormat class
*/
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
df.setLenient(false);
final String dateString = month + "/" + day + "/" + year;
df.parse(dateString);
}
catch (Exception e)
{
result = false;
}
/*
Retun the boolean variable, result
*/
return result;
}
/*
getYear(): Returns the current year.
Parameters:
a: Integer value for the year.
Return Type: String
*/
public String getYear(int a)
{
String yearSpeak;
if(a>=80 && a<=99)
{
yearSpeak="Nineteen"+String.valueOf(a);
}
else
if (a>=01 && a<=04)
{
yearSpeak="Two thousand"+String.valueOf(a);
}
else
yearSpeak=String.valueOf(a);
return yearSpeak;
}
/*
main():Create the main window for the application
Parameters
str[]: An array of String values.
Return Type: NA
*/
public static void main(String str[])
{
/*
Initialize an instance of SpeechCalendar class
*/
SpeechCalendar sc=new SpeechCalendar();
sc.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -