📄 the selectvoice.java file.txt
字号:
/* Import required swing classes */
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.event.*;
/* Import required awt classes */
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/* Import required util classes */
import java.util.Enumeration;
import java.util.Locale;
import java.util.Hashtable;
/* Import required speech classes */
import javax.speech.Central;
import javax.speech.Engine;
import javax.speech.EngineList;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.SynthesizerProperties;
import javax.speech.synthesis.Voice;
/*
Class: SelectVoice-Selects the voice in which the synthesizer speaks the text.
Fields:
retValue: Represents the voice that is currently selected by the user.
Methods:
getVoices():Retrieves all the voices in the general locale that are available on the user's
computer.
actionPerformed(): Determines the operations to be performed when the user clicks any button.
*/
public class SelectVoice extends JFrame implements ActionListener
{
/* Declare variables */
JLabel pageTitle;
JRadioButton rb;
ButtonGroup bg;
JButton ok,cancel;
JPanel voicePanel;
JPanel buttonPanel;
JPanel mainPanel;
Font f,f1;
Hashtable voicehash;
TextPad textpad = null;
/*
Set the default value for the selected voice
*/
public static String retValue = "kevin16";
public static String retkey=null;
/*
SelectVoice: Define the default constructor to create the user interface for the SelectVoice.
Parameters:
textpad: An object of TextPad.java class
Return Type: NA
*/
public SelectVoice(TextPad textpad)
{
this.textpad=textpad;
this.setSize(150,200);
this.setResizable(false);
/* Initialize the variables */
pageTitle=new JLabel("Select the voice");
voicePanel=new JPanel();
buttonPanel=new JPanel();
mainPanel=new JPanel();
bg=new ButtonGroup();
/* Set layout for the panels */
buttonPanel.setLayout(new GridLayout(1,2));
mainPanel.setLayout(new BorderLayout());
/* Create objects of Font class */
f=new Font("Verdana",Font.BOLD,12);
pageTitle.setFont(f);
f1=new Font("Verdana",Font.BOLD,10);
/* Initialize the OK button */
ok=new JButton("OK");
ok.setFont(f1);
ok.addActionListener(this);
/* Initialize the cancel button */
cancel=new JButton("Cancel");
cancel.setFont(f1);
cancel.addActionListener(this);
/*
Invoke the getVoices() method to retrieve all available voices.
*/
getVoices("general");
/* Add buttons to buttonPanel */
buttonPanel.add(ok);
buttonPanel.add(cancel);
/* Add contents to the mainPanel */
mainPanel.add(pageTitle,BorderLayout.NORTH);
mainPanel.add(voicePanel,BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
/* Add mainPanel to the main window */
getContentPane().add(mainPanel);
/* Add window listener to this window */
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
}
});
}
/*
getVoices():Retrieves all the voices available in the general locale of user computer.
Parameters:
modename: String representing the selected mode.
Return Type: NA
*/
public void getVoices(String modename)
{
/*
Create an object of SynthesizerModeDesc class
*/
SynthesizerModeDesc required = new SynthesizerModeDesc(
null,
modename,
Locale.US,
null,
null);
/*
Retrieve available synthesizers in an object of EngineList class
*/
EngineList engineList = Central.availableSynthesizers(required);
/*
Run a loop from 1 to the size of EngineList object
*/
voicehash=new Hashtable();
for (int i = 0; i < engineList.size(); i++)
{
/*
Initialize an object of SynthesizerModeDesc class
*/
SynthesizerModeDesc desc = (SynthesizerModeDesc) engineList.get(i);
/*
Retrieve the voices available in the current synthesizer
*/
Voice[] voices = desc.getVoices();
/* Set layout of the voicePanel */
voicePanel.setLayout(new GridLayout(voices.length,1));
/*
Add all the available voices to the voicePanel
*/
for (int j = 0; j < voices.length; j++)
{
String mp=voices[j].getName().toString();
int x=j+1;
String voiceno="Voice "+x;
voicehash.put(voiceno,temp);
System.out.println(temp);
/*
Initialize an object of JRadioButton class
*/
rb=new JRadioButton(voiceno);
/*
Set kelvin16 as the default selected voice
*/
if(temp.equals("kevin16"))
rb.setSelected(true);
/*
Add JRadioButton to a buttopngroup
*/
bg.add(rb);
/*
Add JRadioButton to voicePanel
*/
voicePanel.add(rb);
}
}
}
/*
actionPerformed(): Defines the operations to be performed when the user clicks any button.
Parameters:
ev: An object of ActionEvent class
Return Type:NA
*/
public void actionPerformed(ActionEvent ev)
{
/*
This code executes when the user clicks the OK button.
*/
if(ev.getSource()==ok)
{
/*
Retrieve all the elements in the button group in an enumeration
*/
Enumeration enum=bg.getElements();
/*
Run until the enumeration has any element
*/
while(enum.hasMoreElements())
{
JRadioButton button=(JRadioButton)enum.nextElement();
/*
Retrieve the label of the JRadioButton that is currently selected.
*/
if (button.isSelected())
{
retkey=button.getText();
retValue=(String)voicehash.get(retkey);
this.setVisible(false);
textpad.setvoice(retValue);
}
}
}
else
/*
This part of code executes when the user clicks cancel button.
*/
if(ev.getSource()==cancel)
{
/*
Hide the user interface of SelectVoice.java file
*/
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -