Java – update swing components correctly?

I'm a novice. Thank you for any help

In this code, I'm flopping. If it turns out that they don't match, I want them to face down again

What is happening now: 1 When clicked, the first card turns over 2 When clicking on the second card, either of the two things happens (a) if they are the same, they will stay up late, which is what I want (b) if they are different, I can't see the second card at all, because it immediately redisplays the back of the card (and the back of the previous card defined in my method)

I think putting in a sleep timer may make the second card display for a period of time and then turn back, but this is not the case

I tried to use contentpane revalidate(); & Ampere; contentPane. repaint(); But it didn't change anything

I have entered some console output:

Console output:
Card: 0 set
Card: 6 set
Sleeping Now
Card: 6 unset
Card: 0 unset

The above is the console output generated when two mismatched cards are clicked

@Override
public void actionPerformed(ActionEvent e) 
{
    String buttonPressed = e.getActionCommand();
    int pos = Integer.valueOf(buttonPressed);
    action = Control.model.ReceiveCardsTurned(pos);

    keypadArray[pos].setIcon(myIcons[pos]);     
    System.out.println("Card: "+pos+" set");
    currentTime.setText("" + Control.model.time);
    currentscore.setText("" + Control.model.score);

    //contentPane.revalidate();
    //contentPane.repaint();        

    if(Control.model.twoCardsTurned == false)
    {
        if (action == "unturn") 
        {
            System.out.println("Sleeping Now");

            try 
            {
                Thread.sleep(1000);
            }

            catch (InterruptedException e1) 
            {
                e1.printStackTrace();
            }

            keypadArray[pos].setIcon(back);
            keypadArray[Control.model.lastCard].setIcon(back);
            System.out.println("Card: "+pos+" unset");
            System.out.println("Card: "+Control.model.lastCard+" unset");
        }
    }
}

Solution

You seem to lack many important concepts

>Swing is an event - driven environment This means that there is no way (or at least very few) you can "wait" for user input. Usually, you only need to respond to their interaction. > Swing is driven by a single thread, called aka EDT This thread is responsible for dispatching / processing events entering the application to the corresponding parts of the application so that they can take action. > The redraw manager publishes his update request to EDT

Any action you take to prevent EDT from performing this work will make your application look suspended

You can't perform any time-consuming operations on EDT (such as I / O, loop or thread #sleep). Doing so will "pause" your application, which is never good

Read concurrency in swing for more information

Now, you have many choices You can use threads to "wait" in the background and turn back the card, or you can use swing worker or javax swing. Timer.

Another problem you encounter is that you should not update any UI components from any thread other than EDT This means that if you use thread, you will be responsible for resynchronizing the thread with EDT It's not difficult, but it gets messy

Swingworker and javax swing. Timer has the ability to make this easier

Threads and swing worker are very suitable for background processing, and they are just overkill for this problem Instead, javax swing. Timer is perfect for here

if (!Control.model.twoCardsTurned)
    {
        if ("unturn".equals(action)) 
        {
            new Timer(1000,new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    keypadArray[pos].setIcon(back);
                    keypadArray[Control.model.lastCard].setIcon(back);
                    System.out.println("Card: "+pos+" unset");
                    System.out.println("Card: "+Control.model.lastCard+" unset");
                }
            }).start();
        }
    }

This is a very simple example You may want to place controls to prevent users from clicking anything before the timer triggers, for example;)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>