如何关闭线程内的框架

时间:2017-12-08 16:02:29

标签: java

我有这个问题,我从服务器接收消息后无法关闭当前表单,我已经实现了一个线程类,因为我需要从服务器交换消息,我现在面临的问题是我无法关闭当前从服务器收到启动消息后打开表单,打开新表单工作正常,但是当我要关闭当前表单时它不能正常工作!!我不知道如何解决这个问题所以我需要帮助

    public class StartForm extends javax.swing.JFrame {
    // needed info about the current user 
    public static StartForm form= new StartForm() ;
    private String name;
    private String currentUsers;
    private int id ;
    private static int time ;
    public static Socket socket;
    public boolean firstFlag;
    private boolean close=false;
    public static PrintWriter printWriter;
    public static BufferedReader bufferedReader;
    public static Scanner reader;
    public static java.lang.Thread thread2;




    public void setName (String name){
    this.name=name;}

    public String getName (){
    return this.name;}
     public void setCurrentUsers (String currentUsers){
    this.currentUsers=currentUsers;}

    public String getCurrentUsers (){
    return this.currentUsers;}

     public void setId (int id){
    this.id=id;}

    public int getId (){
    return this.id;}

    public void setTime (int time){
    this.time=time;}

    public int getTime (){
    return this.time;}

    public void setSocket (Socket socket){
    this.socket=socket;

       }

    public Socket getSocket (){
    return this.socket;}

    public void setFirstFlag (boolean firstFlag){
    this.firstFlag=firstFlag;}

    public boolean getFirstFlag (){
    return this.firstFlag;}

    /**
     * Creates new form StartForm
     */
    public StartForm() {

        initComponents();

    }


    // constructor to initialize                             


    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      



 if (testInput(jTextField1.getText())){

    // PrintWriter printWriter;
     // read the inserted time and falidate it if it number send it to the server to notify the other users 
            printWriter.println("time:"+jTextField1.getText());
            jButton2.setEnabled(false);
            jTextField1.setEnabled(false);
            jButton1.setEnabled(true);





}

 else {
 //if time inserted is not number unaccepted 
                 JOptionPane.showMessageDialog(null, "only insert numbers");
                 jTextField1.setText("");

 }
       // TODO add your handling code here:
    }                                     


    private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     

    // initiate the needed reader and writer objects 


        try {
            printWriter = new PrintWriter(socket.getOutputStream(),true);
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));    

            // starting the thread 
             Thread thread = new Thread (this.getId(),form);

                thread2 = new java.lang.Thread(thread);
                 thread2.start();


    } catch (IOException ex) {
            Logger.getLogger(StartForm.class.getName()).log(Level.SEVERE, null, ex);
        }




    }                                    



    private void formWindowClosed(java.awt.event.WindowEvent evt) {                                  
        // close the frame
        this.dispose();
        System.exit(0); }                                                               

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      

        // TODO add your handling code here:
        printWriter.println("ready"+id);
        jLabel8.show();
        this.disable();
    }                                     




    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>



        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                form.setVisible(true);
            }
        });




    }




    public class Thread implements Runnable {
     private int idd;
     private StartForm form3;

       public Thread(int id, StartForm form2){
       idd=id;
       form3=form;
       }

    public void run()
    {

try {

            String message="";

            while((message=bufferedReader.readLine())!=null){


            { 


             else if (message.substring(0,5).equals("start")){
                         GameForm gameFrame= new GameForm();

             int trunid =Integer.parseInt(message.substring(5));
                  System.out.println("$$$$$ on the thread "+ trunid);

             if (id==trunid)
               gameFrame.setTurn(true);
             else gameFrame.setTurn(false);
               gameFrame.setId(idd);
             form3.setVisible(false);
             gameFrame.setVisible(true);



             }


             else {
                 jTextArea2.append(message); }


            }

         }


         } catch (IOException ex) {
             Logger.getLogger(StartForm.class.getName()).log(Level.SEVERE, null, ex);
         }





    }



     }



    // Variables declaration - do not modify                     

}

0 个答案:

没有答案