您好我正在使用Java Applet。
我有在while
循环中侦听点击事件的要求。
但是,在进入while
循环后,退出按钮没有响应。
所以我的要求是在点击退出按钮后,小程序将被关闭。
package app;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class test extends Applet implements ActionListener{
Image img;
Button b;
Button b1;
boolean quit = false;
//String actionMessage="";
public void init(){
b=new Button("Start");
b1=new Button("Quit");
add(b);
add(b1);
b.addActionListener(this);
b1.addActionListener(this);
}
public void paint(Graphics g){
b.setLocation(70, 350);
b1.setLocation(180, 350);
g.drawImage(img,10,20,300,300,this);
this.setSize(322,380);
//g.drawString(actionMessage, 10, 50);
}
Button source =null;
public void actionPerformed(ActionEvent ae){
OuterLoop:
source = (Button)ae.getSource();
if(source.getLabel()=="Start"){
img = getImage(getCodeBase(), "images/S2T.png");
repaint();
System.out.println("HI");
quit = true;
test t1 = new test ();
t1.voce();
}
if(source.getLabel()=="Quit"){
System.out.println("Hello");
quit = false;
test t1 = new test ();
t1.voce();
}
/* if(ae.getSource()==b){
img = getImage(getCodeBase(), "images/S2T.png");
repaint();
quit = true;
test t1 = new test ();
t1.voce();
//actionMessage="Speak now";
}
if(ae.getSource()==b1){
img = getImage(getCodeBase(), "images/S2T1.png");
//actionMessage="Thanks for using this Software";
repaint();
quit = false;
test t1 = new test ();
t1.voce();
System.exit(0);
}*/
}
public void voce(){
voce.SpeechInterface.init("D:/Project/VoiceRecognization/src/", false, true,
"file:D:/Project/VoiceRecognization/src/", "digits");
System.out.println("This is a speech recognition test. "
+ "Speak digits from 0-9 into the microphone. "
+ "Speak 'quit' to quit.");
while (!quit)
{
// Normally, applications would do application-specific things
// here. For this sample, we'll just sleep for a little bit.
try
{
Thread.sleep(200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
while (voce.SpeechInterface.getRecognizerQueueSize() > 0)
{
String s = voce.SpeechInterface.popRecognizedString();
if(s.equals("san")){
System.out.println("DONE IT !!!!!!!!!!!!!" );
Runtime rs = Runtime.getRuntime();
try{
/*rs.exec("notepad");*/
//rs.exec("explorer.exe");
rs.exec("explorer d:\\Project\\VoiceRecognization");
//Desktop.getDesktop().open(new File("C:\\folder"));
}catch(Exception ie){
ie.printStackTrace();
}
System.out.println("Done");
}else if(s.equals("hello")){
System.out.println("DONE IT !!!!!!!!!!!!!" );
Runtime rs = Runtime.getRuntime();
try{
/*rs.exec("notepad");*/
rs.exec("explorer =");
/*rs.exec("explorer d:\\Project\\VoiceRecognization");*/
//Desktop.getDesktop().open(new File("C:\\folder"));
}catch(Exception ie){
ie.printStackTrace();
}
System.out.println("Done");
}else{
System.out.println("In the else part");
}
// Check if the string contains 'quit'.
if (-1 != s.indexOf("quit"))
{
quit = true;
System.out.println("NOT DONE IT +++++++++++ " );
}
System.out.println("You said: " + s);
//voce.SpeechInterface.synthesize(s);
}
continue OuterLoop;
}
}
}