⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 the speechcalendar.java file.txt

📁 Creating a Speech-Enabled Calendar Application
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/* Import required packages*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.*;
import javax.swing.event.*;
import java.text.DateFormat;
/*
Class: SpeechCalendar-Creates the user interface for the speech-enabled calendar.
Methods:
CalculateDay(): Retrieves the current day from the system calendar.
CalculateDate(): Retrieves the current date from the system calendar.
CalculateTime(): Retrieves the current time from the system clock.
actionPerformed(): Defines theactions to be performed when user clicks the buttons in 
the user interface.
getMntName(): Returns the string value of the month that is passed as a parameter.
dateIsValid(): Validates the date entered by the user.
getYear(): Returns the string value for the year.
main(): Creates an object of SpeechCalendar class.
setFormat(): Sets the date in the format specified by the user.
*/
public class SpeechCalendar extends JFrame implements ActionListener
{
   /*Declare variables*/
   Calendar c;
   String format;
   String  dayValue,dateValue,timeValue,speak,k1,k2,k3;
   JTabbedPane pane;
   JPanel mainPanel;
   JPanel datePanel,dayPanel,timePanel;
   ButtonGroup dayBg,dateBg,timeBg;
   JRadioButton cDate,selDate,cDay,selDay,cTime,selTime;
   JLabel appTitle;
   JButton dateSpeak,daySpeak,timeSpeak,dateCancel,dayCancel,timeCancel,selFormat;
   JTextField date,day,time,dateText,dayText,timeText;
   String dayString,dateString,timeString;
   SelectFormat sf = null;
   Font f,f1;
   /*
   SpeechCalendar(): Default constructor of SpeechCalendar class.
   Parameters:NA
   Return Type:NA
   */
   public SpeechCalendar()
   {
      /*Set the look and feel for the application*/
      try
      {
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      }
      catch(Exception e)
      {
         System.out.println("Unknown Look and  Feel." + e);      
      }
      /*Initialize object of SelectFormat class*/
      sf = new SelectFormat(this);
      sf.setSize(250,250);
      sf.setResizable(false);
      /*Create an instance of Calendar class*/
      Calendar c=Calendar.getInstance();
      /* Create objects of Font class*/
      f=new Font("Verdana",Font.BOLD,12);
      f1=new Font("Verdana",Font.BOLD,10);
      /*Initialize main panel*/
      appTitle=new JLabel("Speech-Enabled Calendar");
      appTitle.setFont(f);
      mainPanel=new JPanel();
      /*Initialize contents of JTabbedPane*/
      pane=new JTabbedPane();
      datePanel=new JPanel();
      dayPanel=new JPanel();
      timePanel=new JPanel();
      /*Add tabs to JTabbedPane*/
      pane.addTab("Date",datePanel);
      pane.addTab("Day",dayPanel);
      pane.addTab("Time",timePanel);
      /*Create button groups*/
      dateBg=new ButtonGroup();
      dayBg=new ButtonGroup();
      timeBg=new ButtonGroup();
      /*Initialize JRadioButtons*/
      cDate=new JRadioButton("Current Date",true);
      cDay=new JRadioButton("Current Day",true);
      cTime=new JRadioButton("Current Time",true);
      selDate=new JRadioButton("Enter Date (dd/mm/yyyy)",false);
      selDay=new JRadioButton("Enter Day",false);
      selTime=new JRadioButton("Enter Time (hh:mm:ss)",false);
      /* Set font of JRadioButtons*/
      cDate.setFont(f1);
      cDay.setFont(f1);
      cTime.setFont(f1);
      selDate.setFont(f1);
      selDay.setFont(f1);
      selTime.setFont(f1);
      /*Add JRadioButtons to ButtonGroup*/
      dateBg.add(cDate);
      dateBg.add(selDate);
      dayBg.add(cDay);
      dayBg.add(selDay);
      timeBg.add(cTime);
      timeBg.add(selTime);
      /*Initialize JTextFields*/
      dateText=new JTextField(15);
      dayText=new JTextField(15);
      timeText=new JTextField(15);
      date=new JTextField(15);
      day=new JTextField(15);
      time=new JTextField(15);
      /*Initialize JButtons*/
      dateSpeak=new JButton("Speak");
      daySpeak=new JButton("Speak");
      timeSpeak=new JButton("Speak");
      dateCancel=new JButton("Exit");
      dayCancel=new JButton("Exit");
      timeCancel=new JButton("Exit");
      selFormat=new JButton("Select Date Format");
      /* Set Font of buttons*/
      dateSpeak.setFont(f1);
      daySpeak.setFont(f1);
      timeSpeak.setFont(f1);
      dateCancel.setFont(f1);
      dayCancel.setFont(f1);
      timeCancel.setFont(f1);
      selFormat.setFont(f1);
      /*Add action listeners to the JButtons*/
      dateSpeak.addActionListener(this);
      daySpeak.addActionListener(this);
      timeSpeak.addActionListener(this);
      dateCancel.addActionListener(this);
      dayCancel.addActionListener(this);
      timeCancel.addActionListener(this);
      selFormat.addActionListener(this);
      /* Set the contents of datePanel*/
      datePanel.setLayout(new GridLayout(4,2));
      datePanel.add(cDate);
      datePanel.add(date);
      datePanel.add(selDate);
      datePanel.add(dateText);
      datePanel.add(selFormat);
      JLabel tmp=new JLabel("");
      datePanel.add(tmp);
      datePanel.add(dateSpeak);
      datePanel.add(dateCancel);
      /*Set contents of day panel*/
      dayPanel.setLayout(new GridLayout(3,2));
      dayPanel.add(cDay);
      dayPanel.add(day);
      dayPanel.add(selDay);
      dayPanel.add(dayText);
      dayPanel.add(daySpeak);
      dayPanel.add(dayCancel);
      /*Set contents of time panel*/
      timePanel.setLayout(new GridLayout(3,2));
      timePanel.add(cTime);
      timePanel.add(time);
      timePanel.add(selTime);
      timePanel.add(timeText);
      timePanel.add(timeSpeak);
      timePanel.add(timeCancel);
      /* Set contents of mainPanel*/      
      mainPanel.add(appTitle);
      mainPanel.add(pane);
      /*Initialize the main window*/
      setSize(400,200);
      setResizable(false);
      /*Add mainPanel to the current window*/
      getContentPane().add(mainPanel);
      /*
      Call CalculateDay() method to retrieve the current day
      */
      dayValue=CalculateDay(c);
      /* 
      Call CalculateDate() method to retrieve the current date
      */
      dateValue=CalculateDate(c);
      /* 
      Call CalculateTime() method to retrieve the current time
      */
      timeValue=CalculateTime(c);
      /*
      Set current date value in the date textfield
      */
      date.setText(dateValue);
      /*
      Set current day value in the day textfield
      */
      day.setText(dayValue);
      /*
      Set current time value in the time textfield
      */
      time.setText(timeValue);
      /*
      Disable the text fields displaying the current date, day, and time
      */
      date.setEnabled(false);
      day.setEnabled(false);
      time.setEnabled(false);
      format="dd/mm/yyyy";
   }
   /*
   CalculateDay(): Retrieves the value of current day from the system calendar. 
   parameters:
   c: An object of Calendar class.
   Return Type:String
   */
   public String CalculateDay(Calendar c) 
   {
      /*Get the current value of day*/
      int day = c.get(Calendar.DAY_OF_WEEK);
      String day1 = null;
      /* 
      Convert the current day to its string value
      */
      if(day==1) 
      day1 = "Sunday";
      else if(day==2) 
      day1 = "Monday";
      else if(day==3) 
      day1 = "Tuesday";
      else if(day==4) 
      day1 = "Wednesday";
      else if(day==5) 
      day1 = "Thursday";
      else if(day==6) 
      day1 = "Friday";
      else if(day==7) 
      day1 = "Saturday";
      /*Return current day*/
      return day1;
   }
   /*
   CalculateDate(): Returns the current date
   Parameters:
   c: An object of Calendar class.
   Return Type: String
   */
   public String CalculateDate(Calendar c)
   {
      /*Get the current date*/
      int date=c.get(Calendar.DAY_OF_MONTH);
      /*Get the current month*/
      int mnt=c.get(Calendar.MONTH);
      /*Get the current year*/
      int year=c.get(Calendar.YEAR);
      String date1;
      /*
      Convert the current date, month, and year to the corresponding string values
      */
      k1=String.valueOf(year);
      if(mnt<10)
      {
         String val="0";
         k2=val+String.valueOf(mnt+1);
      }
      k3=String.valueOf(date);
      date1=k3+"/"+k2+"/"+k1;
      /* Return current date*/
      return date1;
   }
   /*
   CalculateTime(): Returns the value of current time from the system calendar.
   Parameters:
   c: An object of Calendar class.
   Return Type: String.
   */
   public String CalculateTime(Calendar c)
   {
      /*Get the value of present hour*/
      int hour=c.get(Calendar.HOUR);
      /*Get the value of present minutes*/
      int minute=c.get(Calendar.MINUTE);
      /*Get the value of present seconds*/
      int second=c.get(Calendar.SECOND);
      int a_p=c.get(Calendar.AM_PM); 
      /*
      Convert the present hour, minutes, and seconds to string
      */
      String hr,min,sec;
      String tempTime="0";
      String timeTot="12:00:00 AM";
      if(hour<9)
      hr=tempTime+String.valueOf(hour);
      else
      hr=String.valueOf(hour);            
      if(minute<10)
      min=tempTime+String.valueOf(minute);
      else
      min=String.valueOf(minute);
      if(second<10)
      sec=tempTime+String.valueOf(second);
      else
      sec=String.valueOf(second);
      if (a_p==0)
      {
         timeTot=hr+":"+min+":"+sec+" AM";
      }
      else
      {
         timeTot=hr+":"+min+":"+sec+" PM";
      }
      /*Return the present time*/
      return timeTot;
   }
   /* 
   actionPerformed(): Defines the operation to be performed when the user clicks a button.
   Parameters:
   ev: An object of ActionEvent.
   Return Type: NA
   */
   public void actionPerformed(ActionEvent ev)
   {
      /*
      This part of the code executes if the end user clicks the Cancel button in any pane of the
      JTabbedPane
      */
      if((ev.getSource()==dateCancel)||(ev.getSource()==dayCancel)||(ev.getSource()==timeCancel))
      {
         System.exit(0);
      }
      else
      /*
      This part of code executes if the user clicks the Select date format button
      */
      if(ev.getSource()==selFormat)
      {
         sf.setVisible(true);
      }
      else 
      {      
         /* 
         This part of code executes when the user clicks the Speak button in the Day pane of the
         JTabbedPane
         */
         if(ev.getSource()==daySpeak)
         {
         speak="No entry specified";
         /*
         Checks if the JRadioButton Current Day is selected
         */
         if(cDay.isSelected())
         {
            speak=dayValue;
         }
         else
         /* 
         Checks if the JRadioButton, Enter Date is selected
         */
         if(selDay.isSelected())
         {
            if((dayText.getText().compareToIgnoreCase("Monday")!=0)&&
            (dayText.getText().compareToIgnoreCase("Tuesday")!=0)&&
            (dayText.getText().compareToIgnoreCase("Wednesday")!=0)&&
            (dayText.getText().compareToIgnoreCase("Thursday")!=0)&&
            (dayText.getText().compareToIgnoreCase("Friday")!=0)&&
            (dayText.getText().compareToIgnoreCase("Saturday")!=0)&&
            (dayText.getText().compareToIgnoreCase("Sunday")!=0))
            {
               /* Prompts that the day entered is wrong*/
               speak="You have entered an invalid entry for day, Enter again";
               dayText.setText("");
               dayText.grabFocus();
            }
            else
            /*Speaks the day entered by the user*/
            speak=dayText.getText();
         } 
      }
      /*
      This part of code executes when the user clicks the Speak button in the Date pane
      */
      else if(ev.getSource()==dateSpeak)
      {
         speak="No entry specified";
         /*Checks if the JRadioButton, Current Date is selected*/
         if(cDate.isSelected())
         {
            /*Initialize an object of StringTokenizer class*/
            StringTokenizer st=new StringTokenizer(date.getText(),"/");
            /*Checks if there    exists any tokens in the StringTokenizer class*/
            while(st.hasMoreTokens())
            {
               format=selDate.getLabel().substring(12,22);
               String val1=st.nextToken();
               String val2=st.nextToken();
               String val3=st.nextToken();
               String temp;
               /*Checks if the selected format is yyyy/mm/dd or yy/mm/dd*/
               if (format.equals("yyyy/mm/dd")||format.equals("yy/mm/dd"))
               {
                  temp=getYear(Integer.parseInt(val1));
                  speak=temp+" "+getMntName(val2)+" "+val3;
                  System.out.println(speak);
               }
               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"))
               {
                  temp=getYear(Integer.parseInt(val3));
                  speak=getMntName(val1)+" "+val2+" "+temp;
                  System.out.println(speak);
               }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -