📄 the textpad.java file.txt
字号:
/* Import required awt classes */
import java.awt.BorderLayout;
import java.awt.GridLayout;
/* Import required awt event classes */
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
/* Import required swing classes */
import javax.swing.JOptionPane;
import javax.swing.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
/* Import required io classes */
import java.io.File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileWriter;
/*
Class: TextPad-Creates the user interface for the textpad
Methods:
actionPerformed():Defines the actions to be performed , when user clicks any button or menu item.
open():Opens the file selected by the end user in the text area of the text pad.
setvoice():Sets the voice of the synthesizer that speaks the text to the selected voice.
main():Creates the main window of the application.
*/
public class TextPad extends JFrame implements ActionListener,Runnable,KeyListener
{
/* Declare variables to create the menu */
JMenuBar menubar;
JMenu fileMenu;
JMenu voiceMenu;
JMenuItem open,newFile, save,exit,selVoice,speak,pause,stop,resume;
/* Declare variables */
JButton speakText,stopText;
JPanel buttonPanel, textPanel;
JScrollPane scrollpane;
JFileChooser filechooser;
JTextArea textarea;
File file;
String str;
String selectedVoice;
FileInputStream fin;
Thread t;
String speakstring;
/* Create an object of SelectVoice class */
SelectVoice sv=null;
/* Create an object of SpeakText class */
SpeakText st=null;
/*
TextPad():Defines the default constructor for the TextPad application.
Parameters:NA
Return Value:NA
*/
public TextPad()
{
setTitle("Voice Synthesizer Application");
/*
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);
}
/* Set the size of user interface */
setSize(400,400);
/*
Set the resizable property of JFrame class to false
*/
setResizable(false);
/*
Initialize the JPanels for the application
*/
sv=new SelectVoice(this);
/* Initialize the JPanels */
buttonPanel=new JPanel();
textPanel=new JPanel();
/* Set the Layout for JPanels */
buttonPanel.setLayout(new GridLayout(1,2));
textPanel.setLayout(new BorderLayout());
/* Initialize and set the JMenuBar */
menubar = new JMenuBar();
setJMenuBar(menubar);
/*
Initialize and create File and Voice menu
*/
fileMenu = new JMenu("File");
voiceMenu = new JMenu("Voice");
/* Add menus to JMenuBar */
menubar.add(fileMenu);
menubar.add(voiceMenu);
/* Initialize new JMenuItems */
newFile=new JMenuItem("New (Alt+F+N)");
open = new JMenuItem("Open Alt+F+O)");
save=new JMenuItem("Save (Alt+F+S)");
exit=new JMenuItem("Exit (Alt+F+E)");
selVoice=new JMenuItem("Select Voice (Alt+V+I)");
speak=new JMenuItem("Speak (Alt+V+P)");
stop=new JMenuItem("Stop (Alt+V+T)");
pause=new JMenuItem("Pause (Alt+V+A)");
resume=new JMenuItem("Resume (Alt+V+R)");
/* Set Mnemonics for the JMenuItems */
fileMenu.setMnemonic('F');
voiceMenu.setMnemonic('V');
open.setMnemonic('O');
save.setMnemonic('S');
exit.setMnemonic('E');
selVoice.setMnemonic('I');
speak.setMnemonic('P');
stop.setMnemonic('T');
pause.setMnemonic('A');
resume.setMnemonic('R');
newFile.setMnemonic('N');
/* Add menu items to the file menu */
fileMenu.add(newFile);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.addSeparator();
fileMenu.add(exit);
/* Add menu items to the voice menu */
voiceMenu.add(selVoice);
voiceMenu.add(speak);
voiceMenu.addSeparator();
voiceMenu.add(stop);
voiceMenu.add(pause);
voiceMenu.add(resume);
/*
Add action listener to the menu items
*/
newFile.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
selVoice.addActionListener(this);
speak.addActionListener(this);
stop.addActionListener(this);
pause.addActionListener(this);
resume.addActionListener(this);
save.setEnabled(false);
/* Initialize new JButtons */
speakText=new JButton("Speak");
stopText=new JButton("Stop");
/*
Set the enables property of JButton and menu items to false
*/
stopText.setEnabled(false);
stop.setEnabled(false);
pause.setEnabled(false);
resume.setEnabled(false);
/* Add action listener to JButtons */
speakText.addActionListener(this);
stopText.addActionListener(this);
/* Add buttons to button panel */
buttonPanel.add(speakText);
buttonPanel.add(stopText);
/* Initialize new text area */
textarea=new JTextArea();
/* Set the properties of text area */
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
textarea.addKeyListener(this);
/*
Initialize new JScrollpane and add text area to scroll pane
*/
scrollpane=new JScrollPane(textarea);
/*
Add button panel and scroll pane to text panel
*/
textPanel.add(scrollpane,BorderLayout.CENTER);
textPanel.add(buttonPanel,BorderLayout.NORTH);
/*
Set the default value of selected voice to kevin16
*/
selectedVoice=new String("kevin16");
/* Add textpanel to the main window. */
getContentPane().add(textPanel);
/*
Add new window listener to the main window
*/
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
/*
Handle the key typed event from the text field.
*/
public void keyTyped(KeyEvent e)
{
}
/*
Handle the key pressed event from the text field.
*/
public void keyPressed(KeyEvent e)
{
}
/*
Handle the key released event from the text field.
*/
public void keyReleased(KeyEvent e)
{
if (e.getSource()==textarea)
{
if ((textarea.getText()).length()==0)
{
save.setEnabled(false);
}
else
{
save.setEnabled(true);
}
}
}
/*
actionPerformed(): Defines the operations to be performed, when the user clicks a button or
selects a menu item.
Parameters:
ev: An object of ActionEvent class.
Return Type:NA
*/
public void actionPerformed(ActionEvent ev)
{
/*
Executes when the user clicks the speak button or speak menu item.
*/
if((ev.getSource()==speakText)||(ev.getSource()==speak))
{
/*
Set the value of the selSpeak to the selected text
*/
String selSpeak=textarea.getSelectedText();
/* Check if no text is selected */
if(selSpeak==null)
{
speakstring=textarea.getText();
/*
Check if no text is available in the textarea of the text pad.
*/
if(speakstring.equals(""))
speakstring="Empty File, Please open a file and then click the Speak button";
}
else
speakstring=selSpeak;
stopText.setEnabled(true);
speakText.setEnabled(false);
pause.setEnabled(true);
resume.setEnabled(true);
stop.setEnabled(true);
t=new Thread(this,"Button action");
t.start();
}
else
/*
Executes when the user clicks the stop button or stop menu item.
*/
if((ev.getSource()==stopText)||(ev.getSource()==stop))
{
/*
Invoke the closeSynthesizer() method of SpeakText class
*/
t.stop();
st.closeSynthesizer();
stop.setEnabled(false);
pause.setEnabled(false);
resume.setEnabled(false);
speakText.setEnabled(true);
stopText.setEnabled(false);
}
else
/*
Executes when the user clicks the New menu item
*/
if(ev.getSource()==newFile)
{
textarea.setText("");
textarea.setEditable(true);
}
else
/*
Executes when the user clicks the Open menu item
*/
if(ev.getSource()==open)
{
try
{
/*
Initialize a new JFileChooser
*/
filechooser=new JFileChooser();
/*
Create and set a new FileFilter for the JFileChooser
*/
filechooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
if(f.isDirectory())
{
return true;
}
String name=f.getName();
/*
Allows only text and java files to be opened in the user interface of text pad
*/
if ((name.endsWith(".txt"))||(name.endsWith(".java")))
{
return true;
}
return false;
}
public String getDescription()
{
return "Text Files";
}
});
/*
Open the JFileChooser
*/
filechooser.showOpenDialog(this);
file = filechooser.getSelectedFile();
if(file==null)
{
System.out.println("No file Selected");
}
else
{
/*
Check if the selected file is a valid file
*/
if(file.isFile())
{
str = file.getAbsolutePath();
open();
}
else
{
/*
Print the error message if the selected file is not a valid file
*/
JOptionPane.showMessageDialog(null,"The selected file is not a valid File","Select
another File",JOptionPane.WARNING_MESSAGE);
}
}
}
catch(Exception e)
{
System.out.println("Error" + e);
}
}
else
/*
Executes if the user clicks the resume menu item
*/
if(ev.getSource()==resume)
{
/*
Invoke the resumeSynthesizer() method of the SpeakText class
*/
t.resume();
st.resumeSynthesizer();
stop.setEnabled(true);
stopText.setEnabled(true);
pause.setEnabled(true);
}
else
/*
Executes if the user clicks the pause button
*/
if(ev.getSource()==pause)
{
/*
Invoke the pauseSynthesizer() method of the SpeakText class
*/
t.suspend();
st.pauseSynthesizer();
resume.setEnabled(true);
}
else
/*
Executes if the user clicks the exit button
*/
if(ev.getSource()==exit)
{
System.exit(0);
}
else
/*
Executes if the user clicks the Select voice menu item
*/
if(ev.getSource()==selVoice)
{
sv.setVisible(true);
}
else
/*
Executes if the user clicks the save menu item
*/
if(ev.getSource()==save)
{
if(textarea.getText().equals(""))
JOptionPane.showMessageDialog(null,"No file open or empty
file","Warning",JOptionPane.WARNING_MESSAGE);
else
{
/* Displays the save dialog box */
if(filechooser.showSaveDialog(TextPad.this) == JFileChooser.APPROVE_OPTION)
{
File fSelected = filechooser.getSelectedFile();
try
{
FileWriter out = new FileWriter(fSelected);
textarea.write(out);
out.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
}
/*
run(): Runs new thread.
Parameters: NA
Return Type:NA
*/
public void run()
{
/*
Initialize a new object of SpeakText class
*/
st=new SpeakText(speakstring,selectedVoice);
/*
Invoke the speakSelText() method of SpeakText class
*/
st.speakSelText(this);
/*
Enable the stop button, pause and resume menu items.
*/
}
/*
Executes if the user clicks the Open menu item
*/
public void open()
{
try
{
/* Initialize a new FileInputStream */
fin = new FileInputStream(filechooser.getSelectedFile().getAbsolutePath());
/*
Initialize a new object of BufferedReader class
*/
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
String readLine = "";
textarea.setText("");
while ((readLine = br.readLine())!=null)
{
textarea.setText(textarea.getText()+ "\n" +readLine);
}
if(textarea.getText()==null)
{
JOptionPane.showMessageDialog(null,"The file has no content","Select
another File",JOptionPane.WARNING_MESSAGE);
}
fin.close();
}
catch(IOException ioe)
{
System.err.println("I/O Error on Open");
}
}
/*
setvoice():retrieves the voice selected by the end user.
Parameters:
str:String representing the voice selected by the end user
Return Type:NA
*/
public void setvoice(String str)
{
selectedVoice=str;
System.out.println(selectedVoice);
}
/*
main():Creates an object of TextPad.java class.
Parameters:
str[]: String array to store the command line parameters.
Return Type:NA
*/
public static void main(String str[])
{
TextPad tp=new TextPad();
tp.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -