Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating swing components correctly?
    text
    copied!<p>I'm new to swing, any help appreciated.</p> <p>In this piece of code I'm turning a card over face up, if it turns out that they don't match I want them to turn back face down again.</p> <p>At the moment what is happening: 1. when clicked the first card turns over 2. when a second card is clicked either of two things happen (a) if they are the same they both stay up which is what I want (b) if they are not the same I never see the 2nd card at all as it immediately re-displays the back of the card (and the back of the previous card also as defined in my method).</p> <p>I thought putting in the sleep timer might keep the 2nd card displayed for a period of time before turning back over but it does not.</p> <p>I attempted to use contentPane.revalidate(); &amp; contentPane.repaint(); but it doesn't change anything.</p> <p>I have put in some console outputs:</p> <pre><code>Console output: Card: 0 set Card: 6 set Sleeping now Card: 6 unset Card: 0 unset </code></pre> <p>Above is the resulting console output when clicking two card which do not match</p> <pre><code>@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"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload