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");
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;

	for(int j=0;j<i;j++){

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

btn[x]=new JButton(randomInts[x]+"");



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

Edited by peter_budo: Please use code tags, do not put email addresses inside post

Attachments Rotation.png 24.19 KB
6 Years
Discussion Span
Last Post by gabz16

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..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.