我用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