Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1560

Re: Help with code

From "Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this>
Subject Re: Help with code
Message-ID <463711bd@dnews.tpgi.com.au> (permalink)
Newsgroups comp.lang.java.gui
References <1178010090.513628.105970@p77g2000hsh.googlegroups.com>
Date 2011-04-27 15:34 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui

"Daz01" <dazzaf15@hotmail.com> wrote in message 
news:1178010090.513628.105970@p77g2000hsh.googlegroups.com...
> Hi need some help please! I have designed a GUI that contains 12
> JButtons. At the moment when you click on one of the buttons a message
> box comes up saying which one you clicked.
>
> What I want to do is one of the buttons will contain treasure e.g.
> Button 3. The user will then have 6 goes to find the treasure by
> clicking on each button. If they click on the wrong button a message
> will appear saying "No treasure". When they find the right button a
> message will appear saying "Treasure found".
>
> Then once the treasure has been found, i want to randomly hide it
> again ready for another go.
<code snipped>

something to play around with

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonFrame extends JFrame
{
  JButton[] btns = new JButton[12];
  int treasureAt = (int)(Math.random()*btns.length);
  public ButtonFrame()
  {
    super ("Treasure Map");
    setLayout( new FlowLayout() );
    ButtonHandler handler = new ButtonHandler();
    for(int x= 0, y = btns.length; x < y; x++)
    {
      btns[x] = new JButton(""+(x+1));
      btns[x].addActionListener(handler);
      add(btns[x]);
    }
  }
  class ButtonHandler implements ActionListener
  {
    public void actionPerformed( ActionEvent event )
    {
      String msg = "No treasure here: %s";
      int x = 0, y = btns.length;
      for(x = 0; x < y; x++) if(btns[x] == event.getSource()) break;
      if(x == treasureAt) msg = "Bingo!! at: %s";
      JOptionPane.showMessageDialog(ButtonFrame.this,String.format(msg,event.getActionCommand()));
    }
  }
  public static void main(String args[])
  {
    ButtonFrame buttonFrame = new ButtonFrame();
    buttonFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    buttonFrame.setSize( 300, 275 );
    buttonFrame.setVisible( true );
  }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Help with code "Daz01" <daz01@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
  Re: Help with code "Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
    Re: Help with code "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
      Re: Help with code "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000

csiph-web