Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Adding code to buttons

Reply
Thread Tools

Adding code to buttons

 
 
Daz01
Guest
Posts: n/a
 
      04-18-2007
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

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      04-18-2007
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.asp...neral/200704/1

 
Reply With Quote
 
 
 
 
Faton Berisha
Guest
Posts: n/a
 
      04-19-2007
On Apr 18, 10:36 am, Daz01 <dazza...@hotmail.com> wrote:
> 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 ...
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Created Radio Buttons displaying as if they were conventional buttons Dr. Leff Javascript 3 10-15-2007 09:47 PM
The Z axis and simple menu buttons/image buttons Jonathan N. Little HTML 3 04-02-2007 09:57 AM
Datagrid Nav buttons and numeric buttons Jeremy Jones ASP .Net 1 03-22-2007 08:00 AM
Image Buttons/Buttons not responding =?Utf-8?B?QmVu?= ASP .Net 1 07-06-2005 07:34 AM
2 buttons but want enter key in textbox to execute one buttons' click event? Roger ASP .Net 1 05-20-2005 09:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57