简体   繁体   English

如何在没有最终String的情况下通过不同的线程更改JLabel?

[英]How can i make the JLabel change through different threads without a final String?

I know this seems like a question asked and answered time and again, But even after combing through the stack Overflow for hours i couldn't solve the problem. 我知道这似乎是一个问题,一遍又一遍地回答,但是即使经过堆栈溢出数小时后,我也无法解决问题。 Sorry in advance if I'm missing something obvious. 抱歉,如果我缺少明显的内容。

I need to change a jLable's text each time a Thread starts, and again when that thread finishes. 每当线程启动时,以及该线程结束时,我都需要更改jLable的文本。 Simply, I'm trying to show the number of threads that are currently running. 简而言之,我试图显示当前正在运行的线程数。

jobQueueView is a static and final jLabel. jobQueueView是静态且最终的jLabel。 Main is the jFrame which has the jLabel. Main是具有jLabel的jFrame。 jobQueue is a static int. jobQueue是一个静态整数。

At the start of each thread: 在每个线程的开始处:

jobQueue += 1; refreshQueue();

At the end of each thread: 在每个线程的末尾:

jobQueue -= 1;refreshQueue();

And finally 最后

public void refreshQueue() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().jobQueueView.setText(Integer.toString(jobQueue));
            }
        });

    }

This doesn't work. 这行不通。 any ideas? 有任何想法吗? Thanks 谢谢

Edit : As per the instructions of Andrew Thompson Swing JLabel text change on the running application : on a button click event 编辑:根据Andrew Thompson Swing JLabel文本的说明在运行的应用程序上进行更改 :在按钮单击事件上

Can I change the text of a label from a Thread in Java? 我可以通过Java中的线程更改标签的文本吗? : has to make the string final. :必须将字符串定为final。 I cant. 我不能

Update JLabel from another thread : Uses Timers, I need the thread count 从另一个线程更新JLabel :使用计时器,我需要线程计数

JLabel on JPanel doesn't update when setText from another method : Tried the given solutions. 当通过另一种方法setText时,JPanel上的JLabel不会更新 :尝试了给定的解决方案。 Didn't work 没工作

Thread and JLabel in Swing- Not working properly : More button clicks but different solutions. Swing中的Thread和JLabel-无法正常工作 :更多的按钮单击,但解决方案不同。 Still didnt work 还是没用

It seems you are creating a new Frame every time. 似乎您每次都在创建一个新的框架。

new Main().jobQueueView.setText(Integer.toString(jobQueueCount )); 新的Main()。jobQueueView.setText(Integer.toString(jobQueueCount));

So you have multiple frames but one static label. 因此,您有多个框架,但只有一个静态标签。 This may cause the problem. 这可能会导致问题。 Access the jobQueueView through a static way like below. 通过如下所示的静态方式访问jobQueueView。

Main.jobQueueView.setText(Integer.toString(jobQueueCount )); Main.jobQueueView.setText(Integer.toString(jobQueueCount));

So your problem is, Only final variables are accessible in Anonymous Inner classes from outer classes 所以你的问题是,外部类的匿名内部类中只能访问最终变量

So in order to make your code work 因此,为了使您的代码正常工作

   public void refreshQueue(int jobQueue) {
        final int jobQueueCount = jobQueue;
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().jobQueueView.setText(Integer.toString(jobQueueCount ));
            }
        });

    }

And use it by 并使用它

jobQueue += 1; refreshQueue(jobQueue);

And

jobQueue -= 1; refreshQueue(jobQueue);

Hope this helps. 希望这可以帮助。

The two answers given solved the problem. 给出的两个答案解决了这个问题。 Here's the final code. 这是最终代码。

public void refreshQueue(int jobQueue) {
        final int jobQueueCount = jobQueue;
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
               Main.jobQueueView.setText(Integer.toString(jobQueueCount ));
            }
        });

    }

At the start of the thread 在线程开始时

jobQueue += 1; refreshQueue(jobQueue);

And at the end of it 最后

jobQueue -= 1; refreshQueue(jobQueue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM