簡體   English   中英

使用Timer在新位置重新粉刷JComponent

[英]Repaint JComponent at new location using Timer

每當我的GUI的坐標通過Swing計時器更改時,如何重新繪制?

這是我的代碼片段:

    t = new Timer(500,new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            for(int i=0;i<=100;i++) {
                panel.setBounds(i,100,550,336);
                panel.repaint();
            }
            t.stop();
        }
    });
    t.start();

循環完成后,我的面板僅重新繪制,因此沒有顯示我想要看到的過渡效果。

循環完成后,我的面板僅重新繪制,因此沒有顯示我想要看到的過渡效果。

這是一個僅使用Swing Timer成功移動組件的示例。 我認為問題出在上面未顯示的代碼中。

在此處輸入圖片說明

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;

public class BouncingLabel {

    private JComponent ui = null;
    int xD = 1;
    int yD = 1;
    int l = 101;
    int r = 100;
    int t = 50;
    int b = 50;

    BouncingLabel() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new CompoundBorder(
                new EmptyBorder(4, 4, 4, 4),
                new LineBorder(Color.BLACK)));

        final JLabel label = new JLabel(new ImageIcon(
                new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB)));
        ui.add(label);
        EmptyBorder emptyBorder = new EmptyBorder(t, l, b, r);
        label.setBorder(emptyBorder);
        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Border border = label.getBorder();
                Insets insets = border.getBorderInsets(label);
                if (l == 0 | r == 0) {
                    xD = -xD;
                }
                if (t == 0 || b == 0) {
                    yD = -yD;
                }
                l = l + xD;
                r = r - xD;
                t = t + yD;
                b = b - yD;
                label.setBorder(new EmptyBorder(t, l, b, r));
            }
        };
        Timer timer = new Timer(15, listener);
        timer.start();
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                BouncingLabel o = new BouncingLabel();

                JFrame f = new JFrame("Bouncing Square");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM