Java 在while循环中侦听clcik事件

Java 在while循环中侦听clcik事件,java,swing,applet,actionlistener,Java,Swing,Applet,Actionlistener,你好,我正在开发一个Java小程序 我需要在循环中侦听单击事件。 但是,在输入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.eve

你好,我正在开发一个Java小程序

我需要在
循环中侦听单击事件。
但是,在输入
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;
}
                }
       }

您的问题可能与并发性有关,您正在阻止事件调度线程,这将阻止UI响应新事件,直到while循环退出1)为什么要编写小程序?如果是老师指定的,请参考。2) 为什么要使用AWT?请参阅以获得许多放弃AWT使用组件而支持Swing的好理由。您能给我推荐一些例子吗,这样我就可以在单独的线程上运行我的循环了。