JPanel的平滑运动并同时更新JLabel [英] Smooth motion for JPanel and update JLabel at same time

查看:82
本文介绍了JPanel的平滑运动并同时更新JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何平滑移动JPanel并同时更新JLabel?

How to move with smooth motion for JPanel and update JLabel at same time?

我想在JFrame上显示当前时间,所以我创建了一个新的java.util.Timer并每秒更新一次标签.

I want to show current time on a JFrame so I created a new java.util.Timer and update to label every one second.

我还创建了另一个Java线程来移动面板组件.

I created another Java thread to as well, move the panel component.

但是在移动面板并在框架上显示(更新)时间时,面板会刷新以形成原始位置.

But while moving the panel and showing (updating) time on the frame, panel refreshing to form original position.

所以我在Google中搜索了该问题,但找不到解决方法.

So I search that problem in Google and can't find the solution.

//Code to move jPanel smoothly
        Thread t = new Thread(){
            int i = 0 ;
            public void run(){
                while(i<150){
                    i++;
                    jPanel2.setLocation(i, jPanel2.getY());
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        };
        t.start();

// Code to show Time
       Timer t = new javax.swing.Timer(1, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jLabel1.setText(new Date()+"");
            }
        });
        t.start();

推荐答案

这里是一个小示例,说明如何为组件提供动画和更新.

Here is a small example, how to provide animation and update for a component.

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;

/**
 * <code>MovedClock</code>.
 */
public class MovedClock {

    private final JLabel clock = new JLabel();
    private final DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm:ss");

    private void startUI() {
        JFrame frame = new JFrame("Moved clock");
        frame.setLayout(null); // usually it's a bad idea, but for animation we need this.
        clock.setBounds(0, 50, 50, 20);
        frame.add(clock);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(500, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        updateClock();
        Timer clockTimer = new Timer(1000, e -> updateClock());
        clockTimer.start();
        // 15 milliseconds for about 60fps
        Timer moveTimer = new Timer(15, new ActionListener() {

            private int count = 1;

            private int increment = 1;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (count == 435 || count == 0) {
                    increment = -increment;
                }
                Point loc = clock.getLocation();
                loc.x += increment;
                clock.setLocation(loc);
                count += increment;
            }
        });
        moveTimer.start();
    }

    private void updateClock() {
        clock.setText(LocalTime.now().format(format));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MovedClock()::startUI);
    }
}

这篇关于JPanel的平滑运动并同时更新JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆