在这种情况下,我如何使用Swing Timer?

时间:2018-05-20 15:15:30

标签: java swing timer actionlistener

我的问题是如何在这里使用Swing计时器,以便每次玩家生活时,我希望代码暂停几秒钟,并将label2的ImageIcon设置为用户可以看到的图像。此代码适用于俄罗斯轮盘赌游戏顺便说一句。我花了大约一天尝试我可以找到的随机方法,但我要么没有正确地执行它们(thread.sleep),否则它们会导致某种错误。

public void actionPerformed(ActionEvent e) {

    PanelCasino panel = (PanelCasino) component;
    frame = new JFrame();

    for(int x = 1; x < 2; x++)
    {
        int num = (int)(Math.random() * 6+1) ;     
        int num1 = (int)(Math.random() * 6+1);

        if( num == num1) //first cycle
        {
            label2.setIcon(new ImageIcon("gun3.jpg")); 
            label1.setText("The other player got sprayed up!");
            panel.total += panel.bet;
            break;
        }

        if(num!= num1)
        {
            label2.setIcon(new ImageIcon("gun1.jpg"));
            label1.setText("The other player lives..."); 
            //this is an example of a player living 
            //and here I want "gun1.jpg" to be visible 
            //for a few seconds before the code moves on.
        }

        int num2 = (int)(Math.random() * 5+1);   //second cycle   

        if( num == num2) 
        {
            label2.setIcon(new ImageIcon("gun4.jpg"));
            label1.setText("You got sprayed up! Rip!");
            panel.total -= panel.bet;
            break;
        }

        if(num!= num2)
        {
            label2.setIcon(new ImageIcon("gun2.jpg"));
            label1.setText("You live...");
        }

        int num3 = (int)(Math.random() * 4+1); //third cycle

        if( num == num3) 
        {
            label2.setIcon(new ImageIcon("gun3.jpg"));
            label1.setText("The other player got sprayed up!");
            panel.total += panel.bet;
            break;
        }

        if(num!= num3)
        {
            label2.setIcon(new ImageIcon("gun1.jpg"));
            label1.setText("The other player lives...");
        }

        int num4 = (int)(Math.random() * 3+1);  //Fourth cycle

        if( num == num4) 
        {
            label2.setIcon(new ImageIcon("gun4.jpg"));
            label1.setText("You got sprayed up! Rip!");
            panel.total -= panel.bet;
            break;
        }

        if(num!= num4)
        {
            label2.setIcon(new ImageIcon("gun2.jpg"));
            label1.setText("You live...");
        }


        int num5 = (int)(Math.random() * 2+1); //Fifth cycle

        if( num == num5) 
        {
            label2.setIcon(new ImageIcon("gun3.jpg"));
            label1.setText("The other player got sprayed up!");
            panel.total += panel.bet;
            break;
        }

        if(num!= num5)
        {
            label2.setIcon(new ImageIcon("gun1.jpg"));
            label1.setText("The other player lives...");
        }

        //Last(6) cycle
        label2.setIcon(new ImageIcon("gun4.jpg"));
        label1.setText("You got sprayed up! Rip!");
        panel.total -= panel.bet;
    }

    panel.label3.setText(""+panel.total);
    panel.revalidate();
}

1 个答案:

答案 0 :(得分:0)

以下代码演示如何使用TimerJLabel之后从DURATION删除图标。
这也可以用作您期望发布的mcve的演示:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TimerTest extends JPanel implements ActionListener{

    private static final int DURATION = 4000;
    private JLabel label;
    private ImageIcon icon;
    private String imagePath = "https://upload.wikimedia.org/wikipedia/commons/3/3f/Crystal_Project_bug.png";
    private Timer timer;

    public TimerTest() throws IOException {

        setLayout(new BorderLayout(5,5));

        JButton btn = new JButton("Fire");
        btn.addActionListener(this);
        add(btn, BorderLayout.SOUTH);

        URL url = new URL(imagePath);
        BufferedImage image = ImageIO.read(url);
        icon = new ImageIcon(image);

        label = new JLabel(); //create a blank lable
        add(label, BorderLayout.NORTH);

        //setup timer to remove label's icon
        timer = new Timer(DURATION, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                label.setIcon(null);
            }
        });

        //note you could initialize using a lambda expression
        // timer = new Timer(DURATION, (e) -> label.setIcon(null) ) ;
        timer.setRepeats(false); //timer should run once
    }

    public static void main(String[] args) throws IOException {

        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(new Dimension(300,350));
        window.add(new TimerTest());
        window.setVisible(true); //make it visible at the end
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        timer.stop(); //in case it is running
        label.setIcon(icon); // add icon
        timer.start();  //fire to remove icon
    }
}

根据需要,请不要犹豫要求澄清。