Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JPanel上使用JLabel实现计时器?_Java_Multithreading_User Interface_Timer - Fatal编程技术网

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

Java 如何在JPanel上使用JLabel实现计时器?,java,multithreading,user-interface,timer,Java,Multithreading,User Interface,Timer,我想获取objectinst的set变量,然后将其设置为JLabel,并将其设置为面板上已经存在的当前JLabel。但是我想让对象'inst'的变量(inst.Time(表示秒))递减1,删除当前的JLabel,并将更新的、递减的inst.Time添加到面板中 我想减少1秒作为倒计时计时器,直到它达到0并从WaitAndInterentWorkArea方法出来。有人能告诉我如何使用给定的代码吗?请帮忙。多谢各位 时间是一个整数 private int WaitAndEnterIntoWorkA

我想获取objectinst的set变量,然后将其设置为JLabel,并将其设置为面板上已经存在的当前JLabel。但是我想让对象'inst'的变量(inst.Time(表示秒))递减1,删除当前的JLabel,并将更新的、递减的inst.Time添加到面板中

我想减少1秒作为倒计时计时器,直到它达到0并从WaitAndInterentWorkArea方法出来。有人能告诉我如何使用给定的代码吗?请帮忙。多谢各位

时间是一个整数

 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;
                    }
                }
            }
        }

如果我没有弄错的话,只需调用
busy.setText(String.valueOf(inst.Time)+“s”)应该是您所需要的全部。但和往常一样,我强烈建议使用JavaFX而不是Swing

至于时间,试试看

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

但是,我如何确保它改变JLabel,每次递减1秒?我相信我需要睡眠(1000),但我应该在哪里或如何使用它?@MadProgrammer你能帮忙吗?