2013-03-28 142 views
2

我有兩個Java(.java) files。一個有JButtonJTextField,另一個有Thread。在第一個Java file中,我添加了一個ActionListenerJButton,這樣當按下按鈕時,一個線程(第二個.java文件的對象在已創建的線程中啓動)運行,它連續修改整型變量。如何在第一個.java文件的JTextField(第二個.java文件)中顯示該整數變量的值?如何在變量值更改時更新JTextField?

Detection.java

package sample; 
public class Detection implements Runnable 
{ 
    public String viewers; 
    public int count; 
    public void run() 
    {       
     try 
     { 
      while (true) 
      { 
       // i have written code for displaying video. 
       // and it say how many no. of people in the video 
       // the no of people is stored in a variable "count" 

       viewers=""+count; //storing count as string so as to display in the JTextField 
      }   
     }     
     catch (Exception e) 
     { 
      System.out.println("Exception: "+e); 
     } 
    } 
} 

UsrInterfac.java

//生成使用的WindowBuilder日食JUNO

package sample; 
import java.awt.EventQueue;  
import javax.swing.JFrame; 
import javax.swing.JButton;  
import javax.swing.JTextField;  
import java.awt.event.ActionListener;  
import java.awt.event.ActionEvent;  

public class UsrInterfac 
{  
    private JFrame frame; 
    private JTextField textField; 
    Detection dd = new Detection(); 
    Thread th = new Thread(dd); 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       try 
       { 
        UsrInterfac window = new UsrInterfac(); 
        window.frame.setVisible(true); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public UsrInterfac() 
    { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() 
    { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JButton btnStartThread = new JButton("Start Thread"); 
     btnStartThread.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent arg0) 
      {    
       th.start();     
      } 
     }); 
     btnStartThread.setBounds(59, 133, 117, 23); 
     frame.getContentPane().add(btnStartThread); 

     textField = new JTextField(); 
     textField.setBounds(270, 134, 104, 20); 
     frame.getContentPane().add(textField); 
     textField.setColumns(10); 
    } 
} 
+1

這是很難理解你想要什麼,嘗試用一些樣本解釋代碼,比如'Class Frame'具有'JButton'和'JTextField'。 '類處理器'想訪問一些數據,像這樣的 – Apurv 2013-03-28 06:58:31

+1

我已經附上了代碼。我想將Detection.java中的變量「查看者」的值顯示在UsrInterfac.java的「textField」中,並告訴我如何執行此操作。另外,當我們在線程中傳輸視頻時,變量「觀衆」不斷變化。 – 2013-03-28 07:35:17

回答

3

從基礎開始,在使用Swing時,始終最好使用LayoutManagers,與使用Absolute Positioning相比,這可以使您的工作更加輕鬆。 無論何時需要從其他線程更改View中的某些內容,始終建議使用EventQueue.invokeLater(...)/EventQueue.invokeAndWait(...)來完成此操作。

這個小樣本程序,也許能幫助你得到一個想法,如何完成你願意的話:-)

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ThreadCounter 
{ 
    private CustomThread cThread; 
    private JTextField tField; 
    private JButton button; 
    private int counter; 

    public ThreadCounter() 
    { 
     counter = 0; 
    } 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Thread Counter Example"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     tField = new JTextField(10); 
     tField.setText("0"); 
     button = new JButton("Start"); 
     button.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent ae) 
      { 
       if (counter == 0) 
       { 
        cThread = new CustomThread(tField); 
        cThread.setFlagValue(true); 
        cThread.start(); 
        counter = 1; 
        button.setText("Stop"); 
       } 
       else 
       { 
        try 
        { 
         cThread.setFlagValue(false); 
         cThread.join(); 
        } 
        catch(InterruptedException ie) 
        { 
         ie.printStackTrace(); 
        } 
        counter = 0; 
        button.setText("Start"); 
       } 
      } 
     }); 

     contentPane.add(tField); 
     contentPane.add(button); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new ThreadCounter().displayGUI(); 
      } 
     }); 
    } 
} 

class CustomThread extends Thread 
{ 
    private int changingVariable; 
    private JTextField tField; 
    private boolean flag = true; 

    public CustomThread(JTextField tf) 
    { 
     changingVariable = 0; 
     tField = tf; 
    } 

    public void setFlagValue(boolean flag) 
    { 
     this.flag = flag; 
    } 

    @Override 
    public void run() 
    { 
     while (flag) 
     { 
      EventQueue.invokeLater(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        tField.setText(
         Integer.toString(
          ++changingVariable)); 
       } 
      }); 

      try 
      { 
       Thread.sleep(1000); 
      } 
      catch(InterruptedException ie) 
      { 
       ie.printStackTrace(); 
      } 
     } 
     System.out.println("I am OUT of WHILE"); 
    }  
} 
1

理想情況下,你應該發佈您的代碼。無論如何,在調用線程代碼時,要麼傳遞第一個類(對象)的實例或JTextField的實例,以便線程可以在文本字段中設置新值。