Adding code to buttons

D

Daz01

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
 
A

Andrew Thompson

Daz01 wrote:
..
....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

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
F

Faton Berisha

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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top