为什么TimeUnit.SECONDS.sleep(10);在它前面的代码之前被调用?

时间:2018-02-27 18:14:35

标签: java user-interface jframe wait

    package me.tykutcher.text.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.concurrent.TimeUnit;

import javax.swing.JPanel;

public class TestPane extends JPanel {


    public boolean sleeper = false;
    public boolean sleeper2 = false;
    public boolean sleeper3 = false;
    public boolean sleeper4 = false;
    public boolean sleeper5 = false;
    public boolean sleeper6 = false;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Loading", 10, 23);
        g.drawRect(60, 13, 175, 10);
        g.setColor(Color.GREEN);
        g.fillRect(61, 14, 25, 9);
        sleeper = true;
        if(sleeper == true){ //why do all of the delays run before adding the string, adding the rectangle, and filling the rectangle
            try {
                //delay for 10 seconds
                TimeUnit.SECONDS.sleep(10);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }}
        sleeper = false;
        g.fillRect(87, 14, 25, 9);
        sleeper2 = true;
        if(sleeper2 == true){
            try {
                //delay for 10 seconds
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        sleeper2 = false;
    }
      public Dimension getPreferredSize() {
            return new Dimension(250, 37);
        }

    }

以上是我想要10秒延迟(仅测试)的课程。 下面是使我在

中运行上述代码的JFrame GUI的类
package me.tykutcher.text.gui;

导入javax.swing.JFrame;

公共类textGui {

public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setTitle("Loading... ");  
    frame.add(new TestPane());
    frame.pack();
    frame.setVisible(true);
}
}

如果我注释掉等待命令,代码全部立即运行,最终,我会有一个通知或布尔方式来完成这个,因为我的gui实际上会做的事情

2 个答案:

答案 0 :(得分:1)

你在睡眠之前画了形状但是,只有当paintComponent方法完成时才会看到它们,而这只是在睡眠之后。

如果你想绘制一些形状,然后有时间通过​​,然后再画一些,你需要让时间超出paintComponent方法(可能是javax.swing.Timer)并决定画什么每次调用paintComponent

答案 1 :(得分:1)

thread.sleep在这里不起作用,因为它阻止了Swing执行其必要的功能。因此,Swing无法完成paintComponent并输出加载条,直到两个睡眠定时器都用完为止。正如Jonathan上面提到的,最简单的解决方案是使用Javax.swing.Timer并在需要更新GUI时调用repaint()。

我使用计时器功能修正了你的加载栏:

import java.awt.Color;  
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JPanel;

public class TestPane extends JPanel {

private static final long serialVersionUID = 1L;

    private int xCoord = 63; // the x-coordinate value
    private int totalWidth = 26; //total width of all the bars and spaces so far

    public TestPane() {
        //Below can be rewritten to be a lambda function if you want
        Timer timer = new Timer(2500, new ActionListener() { //create a timer and actionListener - wait 2.5 seconds between each bar addition 
            @Override
            public void actionPerformed(ActionEvent e) {
                xCoord += 26; //add the length of each bar
                if (xCoord >= 167) { //make sure we don't go past the initial rectangle width
                    ((Timer)e.getSource()).stop();
                }
                repaint(); //repaint the GUI after the timer ends
            }
        });
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Loading", 10, 23);
        g.drawRect(62, 13, 131, 10);
        g.setColor(Color.GREEN);
        int temp = xCoord; //temp variable that let's us iterate over the length of the bar without affecting the x-coordinate
        while(temp >= 63){
            g.fillRect(temp, 14, totalWidth-1, 9);
            temp -=26;
        }
    }

   public Dimension getPreferredSize() {
       return new Dimension(250, 37);
   }
}