如何在函数java之后设置延迟

时间:2014-01-29 13:54:26

标签: java swing delayed-execution

我正在用java开发一个记忆卡游戏。在SelectedTile.hideFace();

之后我需要延迟

Control.java

package control;

public class Control extends JFrame {

    private static final long serialVersionUID = 1L;
    public static Control CurrentWindow = null;
    private ImageIcon background1,background2,background3,background4,background5,background6,background7,background8; 
    private final String title ="Remembory";

    private Tile SelectedTile = null;
    private int points = 0;
    private int fails = 0;
    private int failsleft = 5;
    private JLabel score = new JLabel("Score:0");
    private JLabel pogingen = new JLabel("Pogingen:5");

    public Control() {

        setSize(334,464);
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.BLACK);
        setUpGame();
        add(score,BorderLayout.SOUTH);
        score.setForeground(new Color(1,21,118));
        score.setFont(score.getFont().deriveFont(18.0f));
        add(pogingen,BorderLayout.SOUTH);
        pogingen.setForeground(new Color(1,21,118));
        pogingen.setFont(pogingen.getFont().deriveFont(18.0f));
        setVisible(true);

    }

    public void setUpGame()
    {
        background1 = new ImageIcon("src//card.png");
        background2 =  new ImageIcon("src//card1.png");
        background3 =  new ImageIcon("src//card2.png");
        background4 =  new ImageIcon("src//card3.png");
        background5 =  new ImageIcon("src//card4.png");
        background6 =  new ImageIcon("src//card5.png");
        background7 =  new ImageIcon("src//card6.png");
        background8 =  new ImageIcon("src//card7.png");

        getContentPane().setLayout(new FlowLayout());

        getContentPane().add(new Tile(background1));
        getContentPane().add(new Tile(background1));
        getContentPane().add(new Tile(background2));
        getContentPane().add(new Tile(background2));
        getContentPane().add(new Tile(background3));
        getContentPane().add(new Tile(background3));
        getContentPane().add(new Tile(background4));
        getContentPane().add(new Tile(background4));
        getContentPane().add(new Tile(background5));
        getContentPane().add(new Tile(background5));
        getContentPane().add(new Tile(background6));
        getContentPane().add(new Tile(background6));
        getContentPane().add(new Tile(background7));
        getContentPane().add(new Tile(background7));
        getContentPane().add(new Tile(background8));
        getContentPane().add(new Tile(background8));

        }

    private void Addfails() {
        fails++;
        failsleft--;
        pogingen.setText("Pogingen:" + failsleft);
        repaint();
        System.out.println( "Poging " + fails + " u heeft nog " + failsleft + " poging over" );
        }



    private void AddPoint() {
        points++;
        score.setText("Score:" + points);
        repaint();
        System.out.println(" + " + points + "Punten");
    }


    public void TileClicked (Tile tile){
        if (SelectedTile == null) {
            tile.showFace();
            SelectedTile = tile;
            return;
        }
        if (SelectedTile == tile) {
            tile.hideFace();
            SelectedTile = null;
            return;
        }

        if (points < 8){
            tile.showFace();    
        }

        if (fails > 3){
            JOptionPane.showMessageDialog(null, "Helaas u hebt geen pogingen meer");
            System.exit(0);
        }

        if (points == 7){
            JOptionPane.showMessageDialog(null, "Gefeliciteerd! Jou score is : " + points);
            System.exit(0);
        }

        if (SelectedTile.getFaceColor().equals(tile.getFaceColor())) {
            AddPoint();
            SelectedTile = null;
            return;

        }


        SelectedTile.hideFace();
        //delay here
        tile.hideFace();
        Addfails();
        System.out.println("Probeer het nogmaals");
        SelectedTile = null;

    }







    public static void main(String[] args){
        CurrentWindow = new Control();
    }
}

Tile.java

package Tiles;

public class Tile extends JLabel implements MouseListener{

    private static final long serialVersionUID = 1L;

    private ImageIcon faceColor = new ImageIcon("src//background.png"); // standaard image (back)
    private final static Dimension size = new Dimension(71,96);

    public Tile(ImageIcon kleur)
    {
        setFaceColor(kleur);
        setMinimumSize(size);
        setMaximumSize(size);
        setPreferredSize(size);
        setOpaque(true);
        setIcon(new ImageIcon("src//background.png"));
        addMouseListener(this);
    }


    public void showFace()
    {
        //setBackground(faceColor);
        setIcon(faceColor);
    }
    public void hideFace()
    {
        setIcon(new ImageIcon("src//background.png"));
        //setBackground(new Color(213,86,31));
    }

    protected void setFaceColor(ImageIcon c)
    {
        this.faceColor = c;
    }

    public ImageIcon getFaceColor()
    {
        return this.faceColor;

    }


    public void mouseClicked(MouseEvent arg0) {
        control.Control.CurrentWindow.TileClicked(this);
    }


    public void mouseEntered(MouseEvent arg0) {

    }


    public void mouseExited(MouseEvent arg0) {

    }


    public void mousePressed(MouseEvent arg0) {

    }


    public void mouseReleased(MouseEvent arg0) {


    }
}

2 个答案:

答案 0 :(得分:0)

“延迟”是什么意思?通常情况下,Thread.sleep(毫秒)可以完成这项工作,但此时程序将完全停止!

答案 1 :(得分:0)

如果您希望延迟操作(例如在500毫秒后隐藏卡片),您可以按照以下方式安排:

import java.util.Timer;
import java.util.TimerTask;

// ...
SelectedTile.hideFace();

new Timer().schedule(new TimerTask() {
    public void run() {
        tile.hideFace();
    }
}, 500); // hide face after 500 ms
// ...