2015-01-13 88 views
0

我正在嘗試爲線程的不同運行實例之間的通信創建應用程序。我有一個jFrame,它有一個jTextField和一個jButton。在jTextField中,輸入我想要運行的線程數,然後按下jButton線程運行。每個線程都包含一個帶有jButton的jFrame。因此,如果我在jTextField中輸入3,然後按下OK,則會彈出三個不同的jFrames,它們具有自己的jButton。如果我在線程的某個jFrame中按下jButton,則jButton將被設置爲禁用(setEnabled(false))。這應該發生在按下時線程內的jFrames的每個jButton,但是最後一個jFrame仍然沒有按下。從線程的運行實例中的jFrame訪問元素

這是線程窗口類:

public class Window extends JFrame implements Runnable { 
    JFrame jr; 
    JButton bt; 

    public void run() { 
     jr=new JFrame(); 
     bt=new jButton(); 
     bt.setTitle("Press Me"); 
     jr.setLayout(new FlowLayout()); 
     jr.add(bt); 
     bt.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e){ 
       bt.setEnabled(false); 
      } 
     }); 
     jr.setVisible(true); 
    } 
} 

現在,我這是怎麼運行這個線程的多個實例。 i是從JTextField中所採取的線程實例數:

( int i=Integer.parseInt(jTextField1.gettext()) ) 

for (int a=0;a<i;a++) { 
    Runnable thr=new Window(a); 
    executor.execute(thr); 
} 

這就是我想做的事:當我按下JButton的每JFrame的是線程情境中,它被設置爲setEnabled(false)我得到彈出的最後一個jFrame,它的jButton仍然未被按下。當我按下最後一個JButton時,我希望每個jFrame上的所有JButton都被設置爲setEnabled(true)。我怎樣才能做到這一點?

回答

0

這是現在它主要工作的主類!

 import java.awt.Color; 
    import java.awt.Image; 
    import java.io.IOException; 
    import java.util.Random; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 
    import javax.imageio.ImageIO; 
    import javax.swing.ImageIcon; 

    /** 
    * 
    * @author oliver 
    */ 
    public class Hauptklasse extends javax.swing.JFrame { 
    static int i = 0; 
    static Random r; 
    static boolean OkApasat=false; 
    static int c; 
    public static Fereastra[] thread; 


    public Hauptklasse() throws IOException { 
     initComponents(); 
     Image logo; 
     logo = ImageIO.read(getClass().getResource("resurse/logo.png")); 
     jLabel4.setIcon(new ImageIcon(logo)); 
     jLabel4.setVisible(true); 
     setLocation(200,100); 
     jPanel1.setBackground(Color.cyan); 
     jTextField1.setEditable(false); 
     jButton2.setEnabled(false); 
     r=new Random(); 




    } 


    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 
     jButton1 = new javax.swing.JButton(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jLabel1 = new javax.swing.JLabel(); 
     jTextField1 = new javax.swing.JTextField(); 
     jButton2 = new javax.swing.JButton(); 
     jLabel4 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setPreferredSize(new java.awt.Dimension(549, 448)); 

     jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
     jButton1.setText("Pornire"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jTextArea1.setBackground(new java.awt.Color(0, 0, 0)); 
     jTextArea1.setColumns(20); 
     jTextArea1.setFont(new java.awt.Font("Andale Mono", 0, 11)); // NOI18N 
     jTextArea1.setForeground(new java.awt.Color(255, 255, 255)); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N 
     jLabel1.setText("Numar maxim de ferestre"); 

     jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
     jButton2.setText("OK"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addContainerGap() 
         .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(28, 28, 28) 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addGap(44, 44, 44) 
         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addGap(18, 18, 18) 
         .addComponent(jLabel1) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(18, 18, 18) 
         .addComponent(jButton2))) 
       .addContainerGap(31, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addComponent(jButton1) 
         .addGap(0, 0, Short.MAX_VALUE)) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(1, 1, 1) 
         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
          .addComponent(jLabel1) 
          .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jButton2)) 
         .addGap(22, 22, 22) 
         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))) 
       .addContainerGap()) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     jTextField1.setEditable(true); 
     jButton1.setEnabled(false); 
     jButton2.setEnabled(true); 
     jTextArea1.append("\n Welcome! :)"); 
     jTextArea1.append("\n The following app tests the communication between threads"); 
     jTextArea1.append("\n Please enter maximum number of windows to be opened"); 



    }           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           

     OkApasat=true; 


    }           

    /** 
    * @param args the command line arguments 
    * @throws java.io.IOException 
    */ 
    public static void main(String args[]) throws IOException { 


     final Hauptklasse main1 = new Hauptklasse(); 


     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       main1.setVisible(true); 

      } 
     }); 

     while (true){ 
      if (OkApasat==true){ 
       if (Integer.parseInt(jTextField1.getText())>=3){ 
        i= (Integer.parseInt(jTextField1.getText())); 
        main1.jTextArea1.append("\n"+i+" windows opened"); 
        Fereastra[] thr=new Fereastra[i]; 

        for (c = 0; c < i; c++) { 
         thr[c]=new Fereastra(thr); 
         thr[c].run(); 

        } 

        OkApasat=false; 
        main1.jButton2.setEnabled(false); 
        main1.jTextField1.setEditable(false); 


      } 
       else jTextArea1.append("\n Wrong maximum number of windows-must be at least 3"); 

       OkApasat=false; 
     } 

      try { 
       Thread.sleep(10); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(Hauptklasse.class.getName()).log(Level.SEVERE, null, ex); 
      } 



    } 

    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JScrollPane jScrollPane1; 
    public static javax.swing.JTextArea jTextArea1; 
    public static javax.swing.JTextField jTextField1; 
    // End of variables declaration     

    } 

這是窗口類,WORKS 100%:

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

/** 
* 
* @author oliver 
*/ 
public class Fereastra extends JFrame implements Runnable { 

public JFrame jr; 
public JButton bt; 
public JLabel l; 
public JLabel l2; 
public Fereastra[] thread; 
public boolean deblocat = true; 

public Fereastra(Fereastra[] thread) { 
    this.thread = thread; 
    jr = new JFrame(); 
    jr.setSize(250, 250); 
    jr.setLayout(new FlowLayout()); 
    bt = new JButton(); 
    jr.add(bt); 
    bt.setText("OK"); 
    bt.setBackground(Color.cyan); 
    l2 = new JLabel(); 
    l2.setText("Buton activ"); 
    jr.add(l2); 
    jr.setVisible(true); 

} 

public void run() { 

    bt.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      bt.setEnabled(false); 

      deblocat = true; 

      for (int c = 0; c < Hauptklasse.i; c++) { 

       if (thread[c] != null && thread[c].bt.isEnabled()) { 
        deblocat = false; 
        break; 
       } 

      } 

      if (deblocat == true) { 
       for (int c = 0; c < Hauptklasse.i; c++) { 
        if (thread[c] != null) { 
         thread[c].bt.setEnabled(true); 
        } 
       } 
      } 

     } 
    }); 

} 

} 
0

我的類:

1)Window類(Fereastra裝置窗口,deblocat裝置解鎖時,布通ACTIV裝置按鈕是啓用)

package concurenta; 

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

/** 
* 
* @author oliver 
*/ 
public class Fereastra extends JFrame implements Runnable { 

public static JFrame jr; 
public static JButton bt; 
public static JLabel l; 
public static JLabel l2; 
public static Fereastra[] thread; 
public boolean deblocat = true; 

public Fereastra(Fereastra[] thread) { 
    this.thread = thread; 
    jr = new JFrame(); 
    jr.setSize(250, 250); 
    jr.setLayout(new FlowLayout()); 
    bt = new JButton(); 
    jr.add(bt); 
    bt.setText("OK"); 
    bt.setBackground(Color.cyan); 
    l2 = new JLabel(); 
    l2.setText("Buton activ"); 
    jr.add(l2); 
    jr.setVisible(true); 

} 

public void run() { 

    bt.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      bt.setEnabled(false); 

      deblocat = true; 

      for (int c = 0; c < Hauptklasse.i; c++) { 

       if (thread[c] != null && thread[c].bt.isEnabled()) { 
        deblocat = false; 
        break; 
       } 

      } 

      if (deblocat == true) { 
       for (int c = 0; c < Hauptklasse.i; c++) { 
        if (thread[c] != null) { 
         thread[c].bt.setEnabled(true); 
        } 
       } 
      } 

     } 
    }); 

} 

}

2)的主類(a.k.a Hauptklasse,okApasat裝置確定被按下):

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package concurenta; 

import java.awt.Color; 
import java.awt.Image; 
import java.io.IOException; 
import java.util.Random; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 

/** 
* 
* @author oliver 
*/ 
public class Hauptklasse extends javax.swing.JFrame { 
static int i = 0; 
static Random r; 
static boolean OkApasat=false; 
static int c; 
public static Fereastra[] thread; 


public Hauptklasse() throws IOException { 
    initComponents(); 
    Image logo; 
    logo = ImageIO.read(getClass().getResource("resurse/logo.png")); 
    jLabel4.setIcon(new ImageIcon(logo)); 
    jLabel4.setVisible(true); 
    setLocation(200,100); 
    jPanel1.setBackground(Color.cyan); 
    jTextField1.setEditable(false); 
    jButton2.setEnabled(false); 
    r=new Random(); 




} 


/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 
    jButton1 = new javax.swing.JButton(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    jTextArea1 = new javax.swing.JTextArea(); 
    jLabel1 = new javax.swing.JLabel(); 
    jTextField1 = new javax.swing.JTextField(); 
    jButton2 = new javax.swing.JButton(); 
    jLabel4 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jPanel1.setPreferredSize(new java.awt.Dimension(549, 448)); 

    jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
    jButton1.setText("Pornire"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jTextArea1.setBackground(new java.awt.Color(0, 0, 0)); 
    jTextArea1.setColumns(20); 
    jTextArea1.setFont(new java.awt.Font("Andale Mono", 0, 11)); // NOI18N 
    jTextArea1.setForeground(new java.awt.Color(255, 255, 255)); 
    jTextArea1.setRows(5); 
    jScrollPane1.setViewportView(jTextArea1); 

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N 
    jLabel1.setText("Numar maxim de ferestre"); 

    jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
    jButton2.setText("OK"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addContainerGap() 
        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(28, 28, 28) 
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGap(44, 44, 44) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGap(18, 18, 18) 
        .addComponent(jLabel1) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(18, 18, 18) 
        .addComponent(jButton2))) 
      .addContainerGap(31, Short.MAX_VALUE)) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addComponent(jButton1) 
        .addGap(0, 0, Short.MAX_VALUE)) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(1, 1, 1) 
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jLabel1) 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jButton2)) 
        .addGap(22, 22, 22) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))) 
      .addContainerGap()) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE) 
    ); 

    pack(); 
}// </editor-fold>       

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    jTextField1.setEditable(true); 
    jButton1.setEnabled(false); 
    jButton2.setEnabled(true); 
    jTextArea1.append("\n Welcome! :)"); 
    jTextArea1.append("\n The following app tests the communication between threads"); 
    jTextArea1.append("\n Please enter maximum number of windows to be opened"); 



}           

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           

    OkApasat=true; 


}           

/** 
* @param args the command line arguments 
* @throws java.io.IOException 
*/ 
public static void main(String args[]) throws IOException { 


    final Hauptklasse main1 = new Hauptklasse(); 


    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      main1.setVisible(true); 

     } 
    }); 

    while (true){ 
     if (OkApasat==true){ 
      if (Integer.parseInt(jTextField1.getText())>=3){ 
       i= (int) (Math.random() * (Integer.parseInt(jTextField1.getText()) - 3)) + 3; 

       main1.jTextArea1.append("\n"+i+" windows opened"); 
       Fereastra[] thr=new Fereastra[i]; 

       for (c = 0; c < i; c++) { 
        thr[c]=new Fereastra(thr); 
        thr[c].run(); 

       } 

       OkApasat=false; 
       main1.jButton2.setEnabled(false); 
       main1.jTextField1.setEditable(false); 


     } 
      else jTextArea1.append("\n Wrong maximum number of windows-must be at least 3"); 

      OkApasat=false; 
    } 

     try { 
      Thread.sleep(10); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(Hauptklasse.class.getName()).log(Level.SEVERE, null, ex); 
     } 



} 

} 

// Variables declaration - do not modify      
private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel4; 
private javax.swing.JPanel jPanel1; 
private javax.swing.JScrollPane jScrollPane1; 
public static javax.swing.JTextArea jTextArea1; 
public static javax.swing.JTextField jTextField1; 
// End of variables declaration     

}