简体   繁体   English

每次更新都会移动JLabel的位置

[英]Moving a JLabel's position for every update

I'm learning Swing and I have the following code: 我正在学习Swing,我有以下代码:

public class SimView {

private JFrame frame;
private JLabel background;
private JPanel car_panel;

public SimView() {

    frame = new JFrame("GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 500);
    frame.getContentPane().setLayout(null);

    background = new JLabel("");
    background.setIcon(new ImageIcon(
            "C:\\Users\\D__\\Desktop\\background.png"));
    background.setBounds(0, 0, 384, 462);
    frame.getContentPane().add(background);
    frame.setVisible(true);

    car_panel = new JPanel();
    car_panel.setBounds(175, 430, 16, 21);
    car_panel.setVisible(true);
    car_panel.setBackground(Color.BLACK);
    background.add(car_panel);

    MoveCarRunnable carMover = new MoveCarRunnable(car_panel);

}

private static class MoveCarRunnable implements Runnable {

    private JPanel car;

    MoveCarRunnable(final JPanel car) {

        this.car = car;

    }

    @Override
    public void run() {
        // Should I call rePaint() on the car_panel here then ? 

    }

}

What I want to do is to move the JLabel called "car" 'sy coordinates for every update to get the effect of it moving by itself ie no user interaction. 我想做的是为每次更新移动名为“ car”的sy坐标的JLabel,以获取其自身移动即没有用户交互的效果。 I'm not quite sure how to do this, I suppose I need to have some sort of repaint() method redrawing the position of the JLabel for every update. 我不太确定如何执行此操作,我想我需要某种repaint()方法来为每次更新重绘JLabel的位置。 But how do I get this class to know that it needs to update the position? 但是,如何使此类知道需要更新职位?

Any pointers (links/code) would be appreciated. 任何指针(链接/代码)将不胜感激。 I want to get the concept of how Swing and its components work in this case, rather than just employing a solution but of course I am interested in a solution so I can study it closer. 我想了解在这种情况下Swing及其组件如何工作的概念,而不仅仅是采用解决方案,但是我当然对解决方案感兴趣,因此我可以对其进行深入研究。 Thanks 谢谢

EDIT: Please see my edit code above 编辑:请参阅上方的我的编辑代码

You will have to create a separate Thread that would modify the location of your label and then call validate() and repaint() on the container ( frame.getContentPane() ). 您将必须创建一个单独的Thread来修改标签的位置,然后在容器( frame.getContentPane() )上调用validate()repaint() )。 Don't forget to put some sleep() value inside the thread. 不要忘记在线程中放置一些sleep()值。

However, there would be a better approach to create a separate JPanel . 但是,有一种更好的方法来创建单独的JPanel Inside it you would override the paintComponent method or the paint method and there you would draw an image instead of moving JLabel s around. 在其中,您将覆盖paintComponent方法或paint方法,并在那里绘制图像而不是四处移动JLabel

You should better add a JPanel instead of the label to your layout. 您最好将JPanel而不是标签添加到布局中。 Choose the dimensions of the JPanel so that it can contain your car in every phase. 选择JPanel的尺寸,以便它可以在每个阶段容纳您的汽车。

Then overwrite the paint method of that panel to position the image on it. 然后覆盖该面板的paint方法以在其上放置图像。

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

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