简体   繁体   English

如何在JPanel上使用JLabel实现计时器?

[英]How do I implement timer using JLabel on JPanel?

I would like to get a set variable of the object inst, then set it to a JLabel and set that to the current JLabel that's already on the panel. 我想获取对象inst的set变量,然后将其设置为JLabel并将其设置为面板上已经存在的当前JLabel。 But I would like for the variable (inst.Time (which represents seconds)) of the object 'inst' to decrement by one and remove the current JLabel and add to the panel the updated, decremented inst.Time. 但是我希望对象“ inst”的变量(inst.Time(代表秒))减一,并删除当前的JLabel,并将更新的,减后的inst.Time添加到面板中。

I would like to decrement by 1 second as countdown timer until it reaches 0 and come out of the method WaitAndEnterIntoWorkArea. 我想递减1秒作为倒数计时器,直到达到0并从方法WaitAndEnterIntoWorkArea中退出。 Could someone please show how to do so with the given code? 有人可以告诉我如何使用给定的代码吗? Help please. 请帮助。 Thank you 谢谢

int.Time is an integer int.Time是一个整数

 private int WaitAndEnterIntoWorkArea(Instruction inst) // 'inst' is an object of Instruction arrayList
    {
        int index = 0;
        int nFirstYDirectionToMove = 1;

        JLabel busy = new JLabel(String.valueOf(inst.Time) + "s"); //inst.Time is a time that was set for that particular 'inst' and set the busy JLabel to show time + s, e.g. 5s or 3s (s represents seconds)
        busy.setFont(font3);
        busy.setForeground(Color.RED);

         if(inst.WorkArea.startsWith("anvil")) //If inst.WorkArea is set to "anvil" Go into the block of code
        {
            nFirstYDirectionToMove = 1; //Move over one (Not important)

                                                                      //I would like to decrement inst.Time 1 at a time to 0 and add to JPanel every second 
            while(true)
            {
                synchronized("row1" + String.valueOf(index)){
                    if(row1[index].getText().equalsIgnoreCase("Open")) //If JLabel row1[index].getText() is already set as text "Open" execute the if statement;
                    {
                                                                                                                              //I would like to remove the current JLabel (row1[index]) and add the 1 second decremented JLabel to the panel
                        panel.remove(row1[index]); //Remove the JLabel of row1[indexOfArea]  (row1 is an array of JLabels) from the panel
                        row1[index] = busy; //Set JLabel text of row1[index] to the busy JLabel //Set the 1 second decremented JLabel to the current JLabel
                        panel.add(row1[index]); //Add the JLabel with the new text label with something like e.g. 5s or 4s, etc. to the panel
                        revalidate();
                        repaint();
                        break;
                    }
                }
            }
        }

If I'm not mistaken, simply calling busy.setText(String.valueOf(inst.Time) + "s"); 如果我没记错的话,只需调用busy.setText(String.valueOf(inst.Time) + "s"); should be all you need. 应该是您所需要的。 But as always, I would highly recommend using JavaFX instead of Swing. 但是与往常一样,我强烈建议使用JavaFX而不是Swing。

As for the timing try 至于时机尝试

Timer timer = new Timer();
timer.scheduleAtFixedRate(()-> busy.setText(...), 0, 1000);

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

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