Java -如何显示JLabel文本更新每X秒,而另一个动作到位



抱歉,如果代码不整洁。但无论如何,我是一个凌乱的代码写手。我需要知道为什么当我这样做时,JLabel文本不弹出。我怎样才能让短信弹出你。

Frame.java

package rpg.tec;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Frame extends JFrame {
    private static final long serialVersionUID = 1L;
    //Speed of car
    int speed = 3;        
    ImageIcon image = new ImageIcon("C:\Users\elliotbewey\Desktop\DarkArcaneIMGS\download.png");
    JLabel location = new JLabel("Hello");
    JLabel imageLabel = new JLabel(image); 
    public void run() {       
        setLayout(null);
        imageLabel.setBounds(10, 10, 400, 400);
        imageLabel.setVisible(true);
        imageLabel.setLayout(null);
        location.setVisible(true);
        location.setText("Hello");
        add(imageLabel);
        add(location);        
        int loc = 1;        
        for(int i = 0; i< 500; i++) {           
            try {
                //sending the actual Thread of execution to sleep X milliseconds
                Thread.sleep(50);
            } catch(InterruptedException ie) {}            
            loc = loc + speed;            
            imageLabel.setLocation(loc, 0);
            location.setLocation(2, 10);
            add(location);
        }
    }
    void messy() {          
    }
}

Main.java

package rpg.main;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import rpg.tec.Frame;
public class Main {
    public static void main(String[] args) {
        Frame frame = new Frame();
        String Version = "0.0.1";
        frame.setTitle(Version);
        frame.setVisible(true);
        frame.setSize(600, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.run();
    }
}

没有错误发生

结果是没有错误。只是没有文本出现我们在屏幕上有一辆移动的汽车,但没有文本。我该如何解决这个问题?我如何使文本出现(JLabel)你好?

首先,你为什么有:

location.setLocation(2, 10);
add(location);

在你的for循环?没有值在改变。(除非你还在做)

其次,您还需要为JLabel设置setBounds以使其显示。我只输入:

location.setBounds(100,100,100,100);

它出现了

如果你改变

location.setLocation(2, 10);
第一部分

location.setLocation(loc, 10);

最新更新