且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用TimerTask移动JPanels

更新时间:2023-12-05 23:11:52

保留颜色模型(如果用户看到的是"Red JPanel",则为文本)会容易得多.只需更改现有面板的BG颜色,使其适合于用作这些color(/text)组合数组的索引的计数器即可.

It would be far simpler to keep a model of colors (and text, if 'Red JPanel' is part of what the user sees) & simply change the BG colors of the existing panels appropriate to a counter used as an index to an array of those color(/text) combos.

如@MadProgrammer所述,Swing Timer更合​​适,因为Swing计时器可确保在EDT上进行更新.或更确切地说,是两个计时器.第一个将是单发计时器,以延迟两秒钟.第二个将循环颜色.

As mentioned by @MadProgrammer, A Swing Timer would be more appropriate, as the Swing timer ensures updates are made on the EDT. Or rather two timers. The 1st would be a single shot timer to delay two seconds. The 2nd would cycle the colors.

喜欢这个(调整颜色和数字以适应需要):

Like this (adjust colors and numbers to need):

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

public class ColorCycler {

    private JComponent ui = null;
    Color[] colors = {
        Color.RED,
        Color.ORANGE,
        Color.YELLOW,
        Color.GREEN,
        Color.CYAN.darker(),
        Color.MAGENTA.darker(),
        Color.MAGENTA.darker().darker()
    };
    int counter = 0;
    JPanel[] panels = new JPanel[colors.length];

    ColorCycler() {
        initUI();
    }

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

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        ui.setBackground(Color.CYAN);

        ui.add(new JLabel(
                "Clock", SwingConstants.CENTER), BorderLayout.PAGE_START);
        ui.add(new JLabel(
                "Progress Bars", SwingConstants.CENTER), BorderLayout.PAGE_END);

        JPanel colorPanel = new JPanel(new GridLayout(0, 1));
        Border border = new EmptyBorder(new Insets(10, 200, 10, 200));
        for (int ii=0; ii<colors.length; ii++) {
            JPanel p = new JPanel();
            p.setBorder(border);
            panels[ii] = p;
            colorPanel.add(p);
        }
        ui.add(colorPanel, BorderLayout.CENTER);

        ActionListener colorListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                counter++;
                setColors();
            }
        };
        final Timer colorCycleTimer = new Timer(50, colorListener);

        ActionListener delayListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                colorCycleTimer.start();
            }
        };
        Timer delayTimer = new Timer(2000, delayListener);
        delayTimer.setRepeats(false);
        delayTimer.start();

        setColors();
    }

    private void setColors() {
        for (int ii=0; ii<colors.length; ii++) {
            panels[(counter+ii)%colors.length].setBackground(colors[ii]);
        }
    }

    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) {
                }
                ColorCycler o = new ColorCycler();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                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);
    }
}