我试图让我现有的基于控制台的Java程序是使用语音识别项目狮身人面像 ,到基于GUI的程序。 我试图改变代码,但如果我改变它现有的程序不运行。
我现有的用于识别(代码完整的代码 ),它完美的作品没有GUI:
public class HelloWorld {
/**
* Main method for running the HelloWorld demo.
*/
static int i=1;
static String resultText;public static void main(String[] args) {
try {
URL url;
if (args.length > 0) {
url = new File(args[0]).toURI().toURL();
} else {
url = HelloWorld.class.getResource("helloworld.config.xml");
}
System.out.println("Loading...");
ConfigurationManager cm = new ConfigurationManager(url);
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
Microphone microphone = (Microphone) cm.lookup("microphone");
/* allocate the resource necessary for the recognizer */
recognizer.allocate();
/* the microphone will keep recording until the program exits */
if (microphone.startRecording())
{
System.out.println("Say: (Command | Program| Browser | Bluetooth | Device Manager |Power Options |Cal | Control | Player |task manager | Windows Security Center)");
System.out.println("Say: ( open word | open phot oshop|open Access|start Excel|start nero |start fire wall| open Pad |open Paint)");
while (true)
{
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
/*
* This method will return when the end of speech
* is reached. Note that the endpointer will determine
* the end of speech.
*/
Result result = recognizer.recognize();
if (result != null)
{
System.out.println("Enter your choise"+ "\n");
resultText = result.getBestFinalResultNoFiller();
System.out.println("You said: " + resultText + "\n");
// Applications*********************************************
if(resultText.equalsIgnoreCase("Command Prompt"))
{
try{
Runtime.getRuntime().exec("cmd /c start cmd");
}
catch(Exception er){
}
}
// Simulate action commands by importing the robot class above
if(resultText.equalsIgnoreCase("scroll up"))
{
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_UP);
r.delay(500);
r.keyPress(KeyEvent.VK_UP);
r.delay(500);
r.keyPress(KeyEvent.VK_UP);
} catch (AWTException e) {
e.printStackTrace();
}
}
// Program Action Command ABOUT
else if(resultText.equalsIgnoreCase("recognition stop"))
{
try{
//recognizer.wait();
System.out.println("See you later!");
System.exit(0);}
catch(Exception estop ){}
}
else
{
System.out.println("I can't hear what you said.\n");
}
}
}
else
{
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}
} catch (IOException e) {
System.err.println("Problem when loading HelloWorld: " + e);
e.printStackTrace();
} catch (PropertyException e) {
System.err.println("Problem configuring HelloWorld: " + e);
e.printStackTrace();
} catch (InstantiationException e) {
System.err.println("Problem creating HelloWorld: " + e);
e.printStackTrace();
}
}
}
这是贵,我想现有的程序来启动(代码完整的代码 ):
JButton btnNewButton = new JButton("Start Recognizing");
btnNewButton.setBackground(UIManager.getColor("Button.background"));
btnNewButton.setForeground(new Color(34, 139, 34));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
state.setText("Listening");
System.out.println("Started Listening");
state.setBackground(new Color(51, 204, 0));
// Start recognizing from the existing program
}
});
而在这里我要暂停的部分/停止录音:
JButton btnNewButton_1 = new JButton("Stop Recognizing");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
state.setText("Not listening");
state.setBackground(new Color(204, 0, 51));
System.out.println("Stopped Listening");
// Pause/Stop recognition
}
})
我该怎么做?