添加延迟/ Thread.sleep

时间:2017-01-18 05:31:55

标签: java

所以我在java中制作一个基本的点击游戏。到目前为止,我有两个升级和一个你必须战斗的老板的形象。我想要它,所以一旦你达到一定数量的分数,老板的形象将在5秒后消失,JLabel会出现“你打败了”老板的名字“。但你听到了一些接近的事情”。我遇到的问题是,一旦达到设定的点数,老板的形象就不会消失。这是我的代码

    // The "Clicker" class.
    import java.util.concurrent.TimeUnit;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Insets;
    import javax.swing.*;
    import java.awt.event.*;


    public class Clicker extends JFrame implements ActionListener
    {
    static int multiply = 1;//Multiplier
    static boolean x2 = false;//Multiplier bought or not boolean
    static boolean add10 = false;//+10 bought or not boolean
    static int score = 950;//Startingn points 0
    static JFrame j = new JFrame ("Clicker");
    JLabel l = new JLabel ("Points: " + String.valueOf (score));//Points Label
    static JLabel name = new JLabel ();//Name Label
    JTextField jt = new JTextField ("Name");//Name Text Box 
    JButton click = new JButton ();//+1 Button
    public static void main (String[] args)
    {
        Clicker run = new Clicker ();//Object of class

    } // main method


    public Clicker () 
    {   
        JLabel boss1 = new JLabel ();//Boss Label

        JButton multiplier = new JButton ("x2(5000)");//X2 upgrade button

        JButton plus10 = new JButton ("+10(100 Points)");//+10 upgrade button

        JButton enter = new JButton ("Enter");//Enter name

        JPanel p = new JPanel ();//Main Pane

        JButton reset = new JButton ("Reset");//Reset Button

        multiplier.addActionListener (new ActionListener ()//X2 MULTIPLIER ACTION PERFORMED
                {
                    public void actionPerformed (ActionEvent e){
                        if (score > 5000 || score == 5000)// If you have enough to buy upgrade
                        {
                            if (x2 == false)// If you did not previously but upgrade
                            {
                                score = score - 5000;//Subtract 5000 points
                                l.setText ("Points: " + score);//Redisplay Points
                                click.setVisible (false);
                                x2 = true;//Set true to show you bought upgrade
                            }

                        }
                        if (x2 == true)//If bought upgrade
                        {
                            multiply = 2;//Set multiplier by 2
                            multiplier.setVisible(false);//set boolean to true so they can't rebuy
                        }
                        else if (score < 5000)//If you dont have enough points
                        {
                            JOptionPane.showMessageDialog (null, "You do not have enough points.", null, JOptionPane.INFORMATION_MESSAGE);//Dont have enough points message


                        }

                    }

                });

        plus10.addActionListener (new ActionListener ()//PLUS10 BUTTON ACTION PERFORM
        {
            public void actionPerformed (ActionEvent e)
            {
                if (score > 100 || score == 100)//If you have more than 100 points
                {
                    if (add10 == false)//For first time buy
                    {
                        score = score - 100;
                        l.setText ("Points: " + score);
                        click.setVisible (false);//Remove +1 button and replace with +10
                        Insets insets = p.getInsets ();

                        plus10.setBounds (75+ insets.left, 250 + insets.top, 150, 100);//Move +10 button to where the +1 button was
                        add10 = true;//Set true so it doesn't take 100 points off again
                    }

                }
                if (add10 == true)
                {
                    score = score + 10 * (multiply);//Adding 10
                    l.setText ("Points: " + score);//Redisplay score

                }
                else if (score < 100)//If score is less than 100
                {
                    JOptionPane.showMessageDialog (null, "You do not have enough points.", null, JOptionPane.INFORMATION_MESSAGE);//Dont have enough points for upgrade


                }

            }

        }
        );

        enter.addActionListener (new ActionListener ()//ENTER NAME ACTION PERFORMED
        {
            public void actionPerformed (ActionEvent e)
            {
                String input = jt.getText ();
                name.setText (input);
            }

        }
        );
        jt.addActionListener (new ActionListener ()//NAME TEXTBOX ACTION PERFORMED
        {
            public void actionPerformed (ActionEvent e)
            {
                String input = jt.getText ();
                name.setText (input);
            }

        }
        );
        reset.addActionListener (new ActionListener ()//RESET BUTTON ACTION PERFORMED
        {
            public void actionPerformed (ActionEvent e)
            {
                score = 0;
                l.setText ("Points: " + score);
            }

        }
        );

        /////////////////////////////////////////////////BOSS ATTACK
        int boss1health;
        boss1health = 1000;

        if(score >= boss1health){

            try{
                boss1.setVisible(false);
                Thread.sleep(5000);
            }
            catch(InterruptedException e){
                Thread.currentThread().interrupt();
            }

            JLabel boss1defeat = new JLabel ("You defeating MewTwo. But you see another threat approuching.");

        }

        /////////////////////////////////////////////////BOSS ATTACK

        ImageIcon boss1image = new ImageIcon(getClass().getResource("mewtwospirte.png") );
        boss1.setIcon(boss1image);//SETTING BOSS1 IMAGE TO MEWTWO

        ImageIcon cimage = new ImageIcon(getClass().getResource("plus1.png") );//PLUS ONE IMAGE
        click.setIcon(cimage);//SETTING PLUS ONE IMAGE TO +1 CLICKER


      //Change Color
        name.setBackground (Color.WHITE);
        jt.setBackground (Color.WHITE);
        p.setBackground (Color.WHITE);
        p.setBackground(Color.WHITE);
        click.setBackground(Color.WHITE);

        j.getContentPane ().add (p);//ADD PANE TO JFRAME

        p.setLayout (null);//Set Pane to Absolute Layout

        Insets insets = p.getInsets ();//Basically Coordinates/Pixels of JFrame

        Dimension size = jt.getPreferredSize ();//Gets Size based off font, etc
        jt.setBounds (0 + insets.left, 5 + insets.top, 75, size.height);//Placing the function

        size = enter.getPreferredSize ();//Gets Size based off font, etc
        enter.setBounds (75 + insets.left, 5 + insets.top, size.width, size.height);//Placing the function

        size = click.getPreferredSize ();//Gets Size based off font, etc
        click.setBounds (75+ insets.left, 250 + insets.top, 150, 100);//Placing the function

        size = reset.getPreferredSize ();//Gets Size based off font, etc
        reset.setBounds (0 + insets.left, 370 + insets.top, size.width, size.height);//Placing the function

        size = name.getPreferredSize ();//Gets Size based off font, etc
        name.setBounds (0 + insets.left, 20 + insets.top, 50, 50);//Placing the function

        size = l.getPreferredSize ();//Gets Size based off font, etc
        l.setBounds (175 + insets.left, 30 + insets.top, 100, size.height);//Placing the function

        size = plus10.getPreferredSize ();//Gets Size based off font, etc
        plus10.setBounds (165 + insets.left, 370 + insets.top, size.width, size.height);//Placing the function

        size = multiplier.getPreferredSize();//Gets Size based off font, etc
        multiplier.setBounds(170 + insets.left, 5 + insets.top, size.width, size.height);//Placing the function

        boss1.setBounds(75 + insets.left, 75 + insets.top, 175, 175);//Placing the function

        //ADDING ALL THE COMPONENETS/FUNCTIONS
        p.add(boss1);
        p.add(multiplier);
        p.add (plus10);
        p.add (jt);
        p.add (name);
        p.add (reset);
        p.add (l);
        p.add (click);
        p.add (enter);

        click.addActionListener (this);

        j.setResizable (false);//Set the JFrame to not be resizable
        j.setSize (300, 450);//Set JFrame Size
        j.setVisible (true);//Set JFrame to be visible
    }


    public void actionPerformed (ActionEvent e)//Action performed when clicking the +1 button
    {
        score = score + 1 * (multiply);
        l.setText ("Points: " + score);
    }        
}

我知道在你击败老板之后我还没有添加说明这条消息的JLabel。我想先解决老板形象变得隐形的问题。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

试试这个:

if(score >= boss1health){
    try{
        new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            boss1.setVisible(false);
        }
    }, 5000);
    }
    catch(InterruptedException e){
        Thread.currentThread().interrupt();
    }

    JLabel boss1defeat = new JLabel ("You defeating MewTwo. But you see another threat approuching.");

}