我用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");
}
});
}
}
最好發佈一個你已經嘗試過的小例子。見http://sscce.org/ 另外,看看http://docs.oracle。com/javase/tutorial/uiswing/concurrency/dispatch.html,因爲這可能會回答有關在Swing UI中進行線程處理的大部分問題。 –
我使用代碼編輯問題。 T – user650034