【问题标题】:update jtextpane with java swing使用 java swing 更新 jtextpane
【发布时间】:2012-11-08 17:33:31
【问题描述】:

我正在用 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");

              }
          });

    }

}

【问题讨论】:

标签: java swing jtextpane


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    • 您在问题标题中提及JTextPane,但仅提及JLabel

    我看到的主要问题是您没有在线程范围内声明JLabel,您可以传递您的JLabel 实例,该实例具有获取对JLabel 的引用的方法通过构造函数到您的线程,因此它具有对JLabel 的引用,现在它没有。

    • 另外我建议使用SwingUtilities 而不是EventQueue
    • 并且不要扩展 Thread 类(除非添加自定义功能)而是 implement a 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 上完成。

    【讨论】:

    • @Daniweb 谢谢大卫。我怎么看你是专家。我希望你回答我这些问题。为什么如果我写这些代码,JLabel 却没有放代码的文本?我在第一个问题的按钮上写了代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    相关资源
    最近更新 更多