Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail From: "Michael Dunn" Subject: Re: Help with code Message-ID: <463711bd@dnews.tpgi.com.au> X-Comment-To: comp.lang.java.gui Newsgroups: comp.lang.java.gui In-Reply-To: <1178010090.513628.105970@p77g2000hsh.googlegroups.com> References: <1178010090.513628.105970@p77g2000hsh.googlegroups.com> Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92] Lines: 63 Date: Wed, 27 Apr 2011 15:34:01 GMT NNTP-Posting-Host: 96.60.20.240 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1303918441 96.60.20.240 (Wed, 27 Apr 2011 10:34:01 CDT) NNTP-Posting-Date: Wed, 27 Apr 2011 10:34:01 CDT Organization: TDS.net Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.gui:1560 To: comp.lang.java.gui "Daz01" 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. 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