1

我正在用 java swing 做一个应用程序。在应用程序的一个按钮中,我需要每隔 x 分钟做一些事情。我认为我必须用一个新线程来做,但我有两个问题。首先是我必须向这些线程传递一个参数。我用一个扩展线程和构造函数的类解决了它。我认为这些方式是正确的,不是吗?我无法解决的第二件事是我需要在线程运行时更新 jtextpane,但是如果我尝试更新 JTextPane 属性 Eclipse 说我无法解决。我认为问题在于这些线程不是主线程。但是……有办法解决吗?非常感谢和对不起我的英语!

代码是:

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");

              }
          });

    }

}
4

2 回答 2

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

          }
      }

    }

希洛.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 上完成。

于 2012-11-20T16:29:23.997 回答
1

使用摇摆计时器。它非常像在给定间隔内定期按下的隐形按钮。它将调用您actionPerformed已经在 Swing 线程中的您可以操作组件的位置(与 相同JButton ActionListener)。因此,您很可能不需要为此任务运行自己的线程。

于 2012-11-20T15:59:03.970 回答