Hi wonder if anyone can help me. I've designed a GUI which has 16
buttons on it. The idea of the game is under 1 button is treasure and
the user has to find the treasure in 6 go's. So the user randomly
clicks on buttons until they find the treasure or their number of go;s
run out.
I've got the GUI and the buttons all set up, at the moment there is no
code for any of them. So they just appear in the applet. I'm not sure
what code to write to get the buttons to do what I want them to do?
Any help would be appreciated.
Thanks
Andrew Thompson - 18 Apr 2007 09:50 GMT
..
>....So they just appear in the applet.
Applets are a bad place to start learning Java.
They have devlopment quirks and deployment
challenges that go beyond the knowledge needed
to write applications.
>...I'm not sure
>what code to write to get the buttons to do what I want them to do?
Add an ActionListener.
<http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html>
Note also that there is a group more suited to GUI's
<http://www.physci.org/codes/javafaq.html#g>
And another group better suited to people who
are beginning to learn Java.
<http://www.physci.org/codes/javafaq.html#h>
HTH

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Faton Berisha - 19 Apr 2007 09:05 GMT
> Hi wonder if anyone can help me. I've designed a GUI which has 16
> buttons on it. The idea of the game is under 1 button is treasure and
[quoted text clipped - 6 lines]
> what code to write to get the buttons to do what I want them to do?
> Any help would be appreciated.
The simplest solution (although not the best design) would be to have
your view ("GUI", which I guess is a JFrame) be an ActionListener as
well. Bellow follows a sketch of the idea.
I hope it helps,
Faton Berisha
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame implements ActionListener
{ private JButton[] button; // the 16 buttons
//...
public MyFrame()
{
// ... Here you probably instantiate your buttons,
// initialize you fields,
// set visible the frame
// ...
}
public void actionPerformed(ActionEvent e)
{
if ( e.getSource() == button[1] )
{
// ... Do whatever you want button[1] to do when pressed
}
else if ( e.getSource() == button[2] )
{
// ... button[2] action listener ...
}
// else if ...
}
}