Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Adding code to buttons

Thread view: 
Daz01 - 18 Apr 2007 09:36 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
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 ...
 }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.