Please help me add actionlistener ect. to my puzzle program so that i can move the tiles into ascending order..Here is my code..

import javax.swing.*;
import java.awt.*; 
import java.util.Random;

public class Rand {
private JButton btn[]=new JButton[16];
public static void main(String args[])
{
Rand app=new Rand();
}
public Rand(){
JFrame frame=new JFrame("Rotaion ver 1.2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel(new GridLayout(4,4));

	int minValue=1;
	int maxValue=15;
	int numInts=15;

	int range=maxValue-minValue;
	Random r=new Random();
	int[] randomInts=new int[numInts];
	int nextRandom;
	for(int i=0;i<numInts;i++)
	{
	nextRandom=r.nextInt(range + 1) + minValue;
	randomInts[i]=nextRandom;

	for(int j=0;j<i;j++){
	if(nextRandom==randomInts[j]){
		i--;	
		j=i;
		}
	}
}

for(int x=0;x<15;x++){

btn[x]=new JButton(randomInts[x]+"");
panel.add(btn[x]);
if(x==15){

btn[x].setVisible(false);
}
}


frame.add(panel);
frame.setSize(500,500);
frame.setResizable(false);
frame.setVisible(true);
	}
}

here is my output..pls help..email me @:..Thanks

You need to add an actionlistener to your buttons. Check the api of the JButton and use the addActionListener method. Create a class that implements the ActionListener interface and pass that class as parameter.

In that class you will need to implement only one method:

public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        
        if (source instanceof JButton) {
            JButton buttonClicked = (JButton)source;
            
        }
    }

The 'source' object is a reference to the component that was clicked. So if you add the above class to other components besides a JButton the 'source' would be that component.
In your case you will add it only to your buttons, so the 'if' wouldn't be necessary.

But I don't know how to move the buttons. Maybe you shouldn't use buttons but something else. Remember you don't have to use buttons. You can use JLabels. You can add a MouseListener in a similar way to the labels and implement the MouseListener interface.
When the mouse is clicked the appropriate method of the MouseListener will be automatically called. And you can change the style of the label to make look like it was moved.

Check the APIs for the above classes

1st of all, what must happen when u click on what? do u want it like the game? to click and the block moves over to the open slot?

or just 1 click to sort it all??

1st of all, what must happen when u click on what? do u want it like the game? to click and the block moves over to the open slot?

or just 1 click to sort it all??

Thanks..yes..like the sliding puzzle game,when you click a button it looks like it moves and replaces with the blank tile until you solve the puzzle in ascending order..please help

I mean it looks like the buttons move when you click them..Its like if you click a button next to the blank tile,it exchanges the number of the button clicked to the blank tile,so it looks like the button moves..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.