Here is my code :
I want to use GUI timer to make a delay , but i have tried many times , failed .Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JOptionPane; import java.util.Random; public class Game extends JPanel { private JFrame frame; private JButton buttons[]; private Random random; private String labels[] = { "1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8"}; private int toggleButton; private int pairMatched; private int firstEvent; private int secondEvent; public Game() { super(new GridLayout(4 , 4 , 5, 5)); frame = new JFrame("Gamex"); buttons = new JButton[16]; random = new Random(); toggleButton = 1; pairMatched = 0; firstEvent = 0; secondEvent = 0; ActionEventHandler handler = new ActionEventHandler(); for(int i = 0 ; i < labels.length ; i++) { int x = random.nextInt(16); String temp = labels[i]; labels[i] = labels[x]; labels[x] = temp; buttons[i] = new JButton("*"); buttons[i].addActionListener(handler); this.add(buttons[i]); } frame.setContentPane(this); frame.setSize(400 , 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class ActionEventHandler implements ActionListener { public void actionPerformed(ActionEvent event) { if(toggleButton == 1) { Object source = event.getSource(); for(int i = 0 ; i < buttons.length ; i++) { if(source == buttons[i]) { firstEvent = i; } } buttons[firstEvent].setText(labels[firstEvent]); buttons[firstEvent].setEnabled(false); } if(toggleButton == 2) { Object source = event.getSource(); for(int i = 0 ; i < buttons.length ; i++) { if(source == buttons[i]) { secondEvent = i; } } buttons[secondEvent].setText(labels[secondEvent]); buttons[secondEvent].setEnabled(false); toggleButton = 0; if(labels[firstEvent].equals(labels[secondEvent])) { buttons[firstEvent].setEnabled(false); buttons[secondEvent].setEnabled(false); pairMatched++; } else { buttons[firstEvent].setEnabled(true); buttons[secondEvent].setEnabled(true); buttons[firstEvent].setText("*"); buttons[secondEvent].setText("*"); } if(pairMatched == 8 ) { JOptionPane.showMessageDialog(null , "Congratulation !\nYou have won!" , "Message" , JOptionPane.PLAIN_MESSAGE); } } toggleButton++; } } public static void main(String args[]) { Game xx = new Game(); } }
Can anyone help ?
where delay will be inserted and for what?
The delay is for the second button .
I want some kind of pause to make the button to freeze for 1 second so that the user can see what is the text on the button.
Why not use sleep(1000)?
if i use Thread.sleep(1000) , where am i going to place the exception ? java.lang.InterruptedException ?
Here is my code :
The Thread.sleep(1000) is working but not what i wanted .Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JOptionPane; import java.util.Random; import java.lang.InterruptedException; public class Game extends JPanel { private JFrame frame; private JButton buttons[]; private Random random; private String labels[] = { "1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8"}; private int toggleButton; private int pairMatched; private int firstEvent; private int secondEvent; public Game() { super(new GridLayout(4 , 4 , 5, 5)); frame = new JFrame("Gamex"); buttons = new JButton[16]; random = new Random(); toggleButton = 1; pairMatched = 0; firstEvent = 0; secondEvent = 0; ActionEventHandler handler = new ActionEventHandler(); for(int i = 0 ; i < labels.length ; i++) { int x = random.nextInt(16); String temp = labels[i]; labels[i] = labels[x]; labels[x] = temp; buttons[i] = new JButton("*"); buttons[i].addActionListener(handler); this.add(buttons[i]); } frame.setContentPane(this); frame.setSize(400 , 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class ActionEventHandler implements ActionListener { public void actionPerformed(ActionEvent event) { if(toggleButton == 1) { Object source = event.getSource(); for(int i = 0 ; i < buttons.length ; i++) { if(source == buttons[i]) { firstEvent = i; } } buttons[firstEvent].setText(labels[firstEvent]); buttons[firstEvent].setEnabled(false); } if(toggleButton == 2) { Object source = event.getSource(); for(int i = 0 ; i < buttons.length ; i++) { if(source == buttons[i]) { secondEvent = i; } } try { buttons[secondEvent].setText(labels[secondEvent]); Thread.sleep(1000); buttons[secondEvent].setEnabled(false); } catch(InterruptedException e) { System.exit(1); } toggleButton = 0; if(labels[firstEvent].equals(labels[secondEvent])) { buttons[firstEvent].setEnabled(false); buttons[secondEvent].setEnabled(false); pairMatched++; } else { buttons[firstEvent].setEnabled(true); buttons[secondEvent].setEnabled(true); buttons[firstEvent].setText("*"); buttons[secondEvent].setText("*"); } if(pairMatched == 8 ) { JOptionPane.showMessageDialog(null , "Congratulation !\nYou have won!" , "Message" , JOptionPane.PLAIN_MESSAGE); } } toggleButton++; } } public static void main(String args[]) { Game xx = new Game(); } }
When the user clicks on the second button , the button doesn't change the text but it does pause for 1 second .
Still got problem ..~ help !
Ya, that is caused that thread.sleep(1000) is run directly when you click on the second button. Although you write it under buttons[secondEvent].setText(labels[secondEvent]).When the user clicks on the second button , the button doesn't change the text but it does pause for 1 second .
So buttons[secondEvent].setText(labels[secondEvent]) is done after sleep(1000).
That is thread.
then any suggestion ? i tried it with others method as well but all ended up with the same thing ..~
You have to put the sleep(1000) call exactly where you want execution to pause. Try moving it around within the method.
But it pause right after we click the second button, before system do buttons[secondEvent].setText(labels[secondEvent]. Although buttons[secondEvent].setText(labels[secondEvent] is written before thread.sleep().
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks