2017-03-17 63 views
-1

私のプログラムでは、Resume Producersボタンが押されたときにメッセージをJTextFieldに出力する必要があり、Resume Consumersを押すとメッセージを削除する必要があります。JButton(JAVA)を押したときにJTextFieldからテキストを削除する方法

問題は、[Resume Consumers]を押したときにメッセージを削除する方法がわからないことです。

The problem

コード:

package buffer; 

    import java.util.ArrayList; 
    import javax.swing.JTextField; 

    public class Buffer 
    { 
     private String message; 
     private boolean full=false; 
     Topass pass = new Topass(); 
     public ArrayList<String> list = new ArrayList<String>(); 
     JTextField tf; 
     String content=""; 

     public Buffer(JTextField t1){ 
      tf=t1; 
     } 

     public synchronized void put(String message) 
     { 
      list.add(message); 

     } 

     public synchronized void take(String message) 
     { 
      list.remove(message); 

     } 

     public synchronized void sendMessage(String msg) 
     { 
      while(full && list.size()>=30) 
       { 
       try 
       { 
        wait(); 
       } catch (InterruptedException ex) { } 
       } 
       pass.look(); 
       full=true; 
       message=msg; 
       list.add(message); 
       for(int i=0; i<list.size(); i++){ 
        content=content+list.get(i); 
       } 
       tf.setText(content); 
       notifyAll(); 

      } 




     public synchronized String receivesMessage() 
     {  

       while(!full) 
      { 
       try 
       { 
        wait(); 
       } catch (InterruptedException ex) { } 
      } 
      pass.look(); 
      full=false; 
      notifyAll(); 
      return message;   
      } 

    } 


package buffer; 

import static java.lang.Thread.sleep; 
import javax.swing.JButton; 

public class Consumer extends Thread 
{ 

    private int numMessages; 
    private Buffer miBuffer; 
    private String readers; 

    public Consumer(String reader, Buffer miBuffer) 
    { 

     this.numMessages=numMessages; 
     this.miBuffer=miBuffer; 
     this.readers=reader; 
    } 


    @Override 
    public void run() 
    { 
     while(true){ 
      try 
      { 
       sleep((int)(300+400*Math.random())); 
      } catch(InterruptedException e){ } 
      System.out.println(readers + " Has read " + miBuffer.receivesMessage()); 

     } 





    } 
} 


    package buffer; 

import static java.lang.Thread.sleep; 
import java.util.ArrayList; 

public class Producer extends Thread 
{ 
    private String prefix; 
    private int numMessages; 
    private Buffer miBuffer; 



    public Producer(String prefix, int n, Buffer buffer) 
    { 
     this.prefix=prefix; 
     numMessages=n; 
     miBuffer=buffer; 
    } 



    public void run() 
    { 
     numMessages = 99; 
     for(int i=1; i<=numMessages; i++) 
     { 
      try 
      { 
       sleep((int)(500+400*Math.random())); 
      } catch(InterruptedException e){ } 
     miBuffer.sendMessage(prefix+i); 



      //miBuffer.meter(prefijo+i); 

     } 
    } 
} 





    package buffer; 


public class Topass 
{ 
    private boolean closed=false; 

    public synchronized void look() 
    { 
     while(closed) 
     { 
      try 
      { 
       wait(); 
      } catch(InterruptedException ie){ } 
     } 
    } 

    public synchronized void open() 
    { 
     closed=false; 
     notifyAll(); 
    } 

    public synchronized void closed() 
    { 
     closed=true; 
    } 
} 




    package buffer; 

import java.awt.Container; 
import java.util.Arrays; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import static jdk.nashorn.internal.objects.ArrayBufferView.buffer; 

/** 
* 
* @author Bogdan 
*/ 
public class DigitalBuffer extends javax.swing.JFrame 
{ 
    Buffer buffer; 
    Producer p; 
    Consumer c; 
    Topass paso = new Topass(); 


    /** 
    * Creates new form BufferGrafico 
    */ 
    public DigitalBuffer() { 
     initComponents(); 
     buffer = new Buffer(t1); 
     Producer A = new Producer("A",99,buffer); 
     Producer B = new Producer("B",99,buffer); 
     Producer C = new Producer("C",99,buffer); 
     Producer D = new Producer("D",99,buffer); 
     Consumer Luis = new Consumer("LUIS", buffer); 
     Consumer Juan = new Consumer("JUAN", buffer); 
     Consumer Maria = new Consumer("MARIA", buffer); 
     Consumer Ana = new Consumer ("ANA", buffer); 
     A.start(); 
     B.start(); 
     C.start(); 
     D.start(); 
     Luis.start(); 
     Juan.start(); 
     Maria.start(); 
     Ana.start(); 

    } 



    /** 
    * 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() { 

     t1 = new javax.swing.JTextField(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     JButton1 = new javax.swing.JButton(); 
     JButton2 = new javax.swing.JButton(); 
     JButton3 = new javax.swing.JButton(); 
     JButton4 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     t1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       t1ActionPerformed(evt); 
      } 
     }); 

     jLabel2.setText("Buffer content"); 

     JButton1.setText("Stop Producers"); 
     JButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       JButton1ActionPerformed(evt); 
      } 
     }); 

     JButton2.setText("Resume Producers"); 
     JButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       JButton2ActionPerformed(evt); 
      } 
     }); 

     JButton3.setText("Stop Consumers"); 
     JButton3.addAncestorListener(new javax.swing.event.AncestorListener() { 
      public void ancestorAdded(javax.swing.event.AncestorEvent evt) { 
       JButton3AncestorAdded(evt); 
      } 
      public void ancestorRemoved(javax.swing.event.AncestorEvent evt) { 
       JButton3AncestorRemoved(evt); 
      } 
      public void ancestorMoved(javax.swing.event.AncestorEvent evt) { 
      } 
     }); 
     JButton3.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       JButton3ActionPerformed(evt); 
      } 
     }); 

     JButton4.setText("Resume Consumers"); 
     JButton4.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       JButton4ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addGroup(layout.createSequentialGroup() 
         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
         .addComponent(jLabel2) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
         .addGap(91, 91, 91) 
         .addComponent(JButton1) 
         .addGap(80, 80, 80) 
         .addComponent(JButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) 
       .addGap(102, 102, 102)) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(58, 58, 58) 
         .addComponent(jLabel1)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(104, 104, 104) 
         .addComponent(JButton3) 
         .addGap(77, 77, 77) 
         .addComponent(JButton4))) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(70, 70, 70) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel2)) 
        .addComponent(jLabel1)) 
       .addGap(60, 60, 60) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(JButton1) 
        .addComponent(JButton2)) 
       .addGap(34, 34, 34) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(JButton3) 
        .addComponent(JButton4)) 
       .addContainerGap(64, Short.MAX_VALUE)) 
     ); 

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

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

    }         

    private void JButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     buffer.pass.closed(); 

    }           

    private void JButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
      buffer.pass.open(); 

    }           

    private void JButton3ActionPerformed(java.awt.event.ActionEvent evt) {           
     buffer.pass.closed(); 
    }           

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


    }           

    private void JButton3AncestorAdded(javax.swing.event.AncestorEvent evt) {          
     // TODO add your handling code here: 
    }          

    private void JButton3AncestorRemoved(javax.swing.event.AncestorEvent evt) {           
     // TODO add your handling code here: 
    }           

    /** 
    * @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(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 

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

    // Variables declaration - do not modify      
    private javax.swing.JButton JButton1; 
    private javax.swing.JButton JButton2; 
    private javax.swing.JButton JButton3; 
    private javax.swing.JButton JButton4; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JTextField t1; 
    // End of variables declaration     
} 
+0

1)すぐに役立つようにするには、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。この問題を表現するために、5つの公立クラスにまたがって400行以上を渡すべきではありません! 2)ソースコード内の空白の空白行が1つ必要です。 '{'の前または '}'の前の空白行も通常は冗長です。 –

+0

'私のコードが厄介であれば申し訳ありません。私の大学2年生です.' - Javaを使って15分後に書式設定されたコードを書くことができるはずです。例えば、コードブロックのインデントは一貫していなければならず、スペースのために単一の空白行が使用されます。これはどんなプログラミングスキルを取ることはありません。 – camickr

答えて

0

事は、私が再開消費者が

を押したときは、ActionListenerを追加メッセージを削除する方法がわからないということですボタンに移動します。 ActionListenerでは、テキストをリセットします。詳細と例については、How to Us ButtonsのSwingチュートリアルのセクションを参照してください。または、How to Write an ActionListenerに関するセクションもあります。

あなたが使用してテキストフィールド内のテキストを置き換えることができます。

textField.setText(...); 
0

例#1(Javaの8+が必要です):

​​

例#2:

button.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     textField.setText(""); 
    } 
}); 

どちらの場合も、textFieldはの最終と宣言されている必要があります。

関連する問題