java Swing计时器一个接一个地执行几个任务

时间:2015-02-09 21:17:34

标签: java swing loops timer

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

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();
                }
            }

        }
    });

1 个答案:

答案 0 :(得分:3)

我不认为有两个Timer个实例是可行的。 Swing Timer因为随着时间的推移“漂移”离开完美的节拍而臭名昭着。

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

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

enter image description here

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
    };
}