2012-11-20 51 views
1

我用java swing製作應用程序。在應用程序的按鈕中,我需要每x分鐘做一些事情。 我認爲我必須用新線程來做,但我有兩個問題。首先是我必須將參數傳遞給這些線程。我用一個擴展了Thread和構造函數的類來解決它。我認爲這種方式是正確的嗎? 我無法解決的第二件事是,我需要在線程運行時更新jtextpane,但如果我嘗試更新JTextPane propierties,Eclipse說我無法解析。我認爲問題在於這些線程不是主線程。但是......有一些方法可以解決它? 非常感謝和抱歉我的英語!用java swing更新jtextpane

的代碼是:

btnIniciar.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       //String file = "D:\\prueba.torrent"; 

     // while (true) { 
       Hilo ejecutar = new Hilo(listaBuscar); 

       ejecutar.run(); 



public class Hilo extends Thread { 



    public Hilo(List<String> aBuscar){    
    } 
    public void run(){ 
     System.out.println("Trabajo por hacer dentro de MiHilo"); 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       lblNewLabel.setText("hola"); 

      } 
     }); 
    } 


} 

它說我lblNewLabel不能得到解決。

任何幫助? 感謝

我現在這些代碼試圖和犯規的工作原理:

public class Hilo implements Runnable { 

    private JLabel etiqueta; 

    public Hilo (List <String> aBuscar, JLabel label){ 

     System.out.println("Hemos entrado en el puto hilo"); 
     etiqueta = label; 


    } 
    @Override 
    public void run() { 

      etiqueta.setText("hola"); 
      System.out.println("vamos a coneseguirlo"); 

     // TODO Auto-generated method stub 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
       etiqueta.setText("hola"); 
       System.out.println("vamos a coneseguirlo"); 

       } 
      }); 

    } 

} 
+1

最好發佈一個你已經嘗試過的小例子。見http://sscce.org/ 另外,看看http://docs.oracle。com/javase/tutorial/uiswing/concurrency/dispatch.html,因爲這可能會回答有關在Swing UI中進行線程處理的大部分問題。 –

+0

我使用代碼編輯問題。 T – user650034

回答

1

使用Swing timer。它非常像在給定的時間間隔內按下不可見的按鈕。它會調用您的actionPerformed已經在一個Swing線程中,您可以在其中操作組件(與JButtonActionListener相同)。因此,您很可能不需要爲此任務運行自己的線程。

1
  • 您在問題標題中提及JTextPane,但僅提及JLabel

雖然你有我看到的主要問題是,你還沒有宣佈JLabel您的線程的範圍內,你可以通過它有一個方法來獲得你的線程的JLabel參考您的JLabel實例通過一個構造函數,因此它有一個對JLabel的引用,現在它沒有。

  • 另外我推薦使用SwingUtilities而不是EventQueue
  • ,且都沒有Thread類(除非添加自定義的功能),而implement一個Runnable

喜歡的東西:

GUI .java:

public class GUI { 

     private JFrame frame; 
     private JLabel label; 
     private JButton btnIniciar; 

     public void getJLabel() { 
      return label; 
     } 

     public void initComponents() { 
     //create UI and components here 

     btnIniciar.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       //String file = "D:\\prueba.torrent"; 
       Hilo ejecutar = new Hilo(listaBuscar,Gui.this);//pass reference of to our class 

      } 
     } 

    } 

Hilo.java:

public class Hilo implements Runnable { 

     private Thread t; 
     private final GUI gui; 

     public Hilo(List<String> aBuscar, GUI ui){  
      this.gui=ui; 
      t=new Thread(this); 
       startThread(); 
     } 

     @Override 
     public void run(){ 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        gui.getJLabel().setText("hola"); 
       } 
      }); 
     } 
     //so we can start thread from other class 
     public void startThread() { 
      if(!t.isAlive()) //if the thread is not started alreade 
      t.start(); 
     } 
    } 

雖然這取決於你正在做一個Swing Timer什麼可能是你所需要的,它可以讓你運行代碼,間隔/延遲而這一切是在做已經有了EDT。

+0

@Daniweb謝謝大衛。我如何看待你是一位專家。我希望你回答我這些問題。爲什麼如果我編寫這些代碼,JLabel donest會放置代碼的文本?我在第一個問題中編寫代碼,在buttom上。 – user650034

+0

@ user650034請參閱更新 –