java Swing计时器一个接一个地执行几个任务 [英] java Swing timer to perform few tasks one after another

查看:105
本文介绍了java Swing计时器一个接一个地执行几个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全清楚很少有类似的问题。我试图实施提供的解决方案 - 徒劳无功。 ...我面临的问题是在另一个之后闪烁按钮。我可以做一个,但是当按顺序闪烁时 - 一切都会中断。任何对Java新人的帮助都表示赞赏。附:我不允许使用Threads。我现在拥有的是:

I am perfectly aware very similar questions few asked before. I have tried to implement solutions offered - in vain. ...The problem i am facing is to blink buttons ONE AFTER ANOTHER. I can do it for one, but when put the order of blinking in a loop - everything breaks. Any help to a new person to Java is appreciated. P.S. I am not allowed to use Threads. What i am having now is:

Timer colorButton = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            for (int i = 0; i < pcArray.length; i++) {
                playSquare = pcArray[i];
                System.out.println("PlaySquare " + playSquare);

                if (playSquare == 1) {
                    if (alreadyColoredRed) {
                        colorBack.start();
                        colorButton.stop();
                    } else {
                        red.setBackground(Color.red);
                        alreadyColoredRed = true;
                        System.out.println("RED DONE");
                    }
                } else if (playSquare == 2) {
                    if (alreadyColoredGreen) {
                        colorBack.start();
                        colorButton.stop();
                    } else {
                        green.setBackground(Color.green);
                        alreadyColoredGreen = true;
                        System.out.println("GREEN DONE");
                    }

                }
            }

        }
    });
    Timer colorBack = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            for (int i = 0; i < pcArray.length; i++) {
                playSquare = pcArray[i];
                System.out.println("PlaySquare " + playSquare);

                if (playSquare == 1) {
                    red.setBackground(Color.gray);
                    alreadyColoredRed = false;
                    System.out.println("RED PAINTED BACK");
                    colorBack.stop();
                } else if (playSquare == 2) {
                    green.setBackground(Color.gray);
                    alreadyColoredGreen = false;
                    System.out.println("GREEN PAINTED BACK");
                    colorBack.stop();
                }
            }

        }
    });


推荐答案

我不认为有两个计时器实例是要走的路。 Swing 计时器因漂移远离完美节拍而臭名昭着。

I don't think that having two Timer instances is the way to go. The Swing Timer is notorious for 'drifting' away from the perfect beat over time.

最好使用控制所有操作所需的逻辑来创建单个计时器。

Better to create a single timer with the logic needed to control all actions.

E.G。显示国际象棋骑士允许的移动。

E.G. Showing the allowable moves for a Chess Knight.

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

public class KnightMove {

    KnightMove() {
        initUI();
    }

    ActionListener animationListener = new ActionListener() {

        int blinkingState = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            final int i = blinkingState % 4;
            chessSquares[7][1].setText("");
            chessSquares[5][0].setText("");
            chessSquares[5][2].setText("");
            switch (i) {
                case 0:
                    setPiece(chessSquares[5][0], WHITE + KNIGHT);
                    break;
                case 1:
                case 3:
                    setPiece(chessSquares[7][1], WHITE + KNIGHT);
                    break;
                case 2:
                    setPiece(chessSquares[5][2], WHITE + KNIGHT);
            }
            blinkingState++;
        }
    };

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

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

        boolean black = false;
        for (int r = 0; r < 8; r++) {
            for (int c = 0; c < 8; c++) {
                JLabel l = getColoredLabel(black);
                chessSquares[r][c] = l;
                ui.add(l);
                black = !black;
            }
            black = !black;
        }
        for (int c = 0; c < 8; c++) {
            setPiece(chessSquares[0][c], BLACK + STARTING_ROW[c]);
            setPiece(chessSquares[1][c], BLACK + PAWN);
            setPiece(chessSquares[6][c], WHITE + PAWN);
            setPiece(chessSquares[7][c], WHITE + STARTING_ROW[c]);
        }

        Timer timer = new Timer(750, animationListener);
        timer.start();
    }

    private void setPiece(JLabel l, int piece) {
        l.setText("<html><body style='font-size: 60px;'>&#" + piece + ";");
    }

    private final JLabel getColoredLabel(boolean black) {
        JLabel l = new JLabel();
        l.setBorder(new LineBorder(Color.DARK_GRAY));
        l.setOpaque(true);
        if (black) {
            l.setBackground(Color.GRAY);
        } else {
            l.setBackground(Color.WHITE);
        }
        return l;
    }

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

                JFrame f = new JFrame("Knight Moves");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.setResizable(false);
                f.pack();

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

    private JComponent ui = null;
    JLabel[][] chessSquares = new JLabel[8][8];
    public static final int WHITE = 9812, BLACK = 9818;
    public static final int KING = 0, QUEEN = 1,
            ROOK = 2, KNIGHT = 4, BISHOP = 3, PAWN = 5;
    public static final int[] STARTING_ROW = {
        ROOK, KNIGHT, BISHOP, KING, QUEEN, BISHOP, KNIGHT, ROOK
    };
}

这篇关于java Swing计时器一个接一个地执行几个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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